Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / Lucene.Net / Search / SortComparator.cs
blobedc3dfa3c63ab548f5f8432cf8d2577ca1037edd
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 using System;
17 using IndexReader = Lucene.Net.Index.IndexReader;
18 namespace Lucene.Net.Search
21 /// <summary> Abstract base class for sorting hits returned by a Query.
22 ///
23 /// <p>This class should only be used if the other SortField
24 /// types (SCORE, DOC, STRING, INT, FLOAT) do not provide an
25 /// adequate sorting. It maintains an internal cache of values which
26 /// could be quite large. The cache is an array of Comparable,
27 /// one for each document in the index. There is a distinct
28 /// Comparable for each unique term in the Field - if
29 /// some documents have the same term in the Field, the cache
30 /// array will have entries which reference the same Comparable.
31 ///
32 /// <p>Created: Apr 21, 2004 5:08:38 PM
33 ///
34 /// </summary>
35 /// <author> Tim Jones
36 /// </author>
37 /// <version> $Id: SortComparator.cs,v 1.1 2005/01/17 19:54:30 joeshaw Exp $
38 /// </version>
39 /// <since> 1.4
40 /// </since>
41 [Serializable]
42 public abstract class SortComparator : SortComparatorSource
44 private class AnonymousClassScoreDocComparator : ScoreDocComparator
46 public AnonymousClassScoreDocComparator(System.IComparable[] cachedValues, SortComparator enclosingInstance)
48 InitBlock(cachedValues, enclosingInstance);
50 private void InitBlock(System.IComparable[] cachedValues, SortComparator enclosingInstance)
52 this.cachedValues = cachedValues;
53 this.enclosingInstance = enclosingInstance;
55 private System.IComparable[] cachedValues;
56 private SortComparator enclosingInstance;
57 public SortComparator Enclosing_Instance
59 get
61 return enclosingInstance;
66 public virtual int Compare(ScoreDoc i, ScoreDoc j)
68 return cachedValues[i.doc].CompareTo(cachedValues[j.doc]);
71 public virtual System.IComparable SortValue(ScoreDoc i)
73 return cachedValues[i.doc];
76 public virtual int SortType()
78 return SortField.CUSTOM;
82 // inherit javadocs
83 public virtual ScoreDocComparator NewComparator(IndexReader reader, System.String fieldname)
85 System.String field = String.Intern(fieldname);
86 System.IComparable[] cachedValues = Lucene.Net.Search.FieldCache_Fields.DEFAULT.GetCustom(reader, field, this);
87 return new AnonymousClassScoreDocComparator(cachedValues, this);
90 /// <summary> Returns an object which, when sorted according to natural order,
91 /// will order the Term values in the correct order.
92 /// <p>For example, if the Terms contained integer values, this method
93 /// would return <code>new Integer(termtext)</code>. Note that this
94 /// might not always be the most efficient implementation - for this
95 /// particular example, a better implementation might be to make a
96 /// ScoreDocLookupComparator that uses an internal lookup table of int.
97 /// </summary>
98 /// <param name="termtext">The textual value of the term.
99 /// </param>
100 /// <returns> An object representing <code>termtext</code> that sorts according to the natural order of <code>termtext</code>.
101 /// </returns>
102 /// <seealso cref="Comparable">
103 /// </seealso>
104 /// <seealso cref="ScoreDocComparator">
105 /// </seealso>
106 public /*protected internal*/ abstract System.IComparable GetComparable(System.String termtext);