2 * Copyright 2004 The Apache Software Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 using IndexReader
= Lucene
.Net
.Index
.IndexReader
;
18 namespace Lucene
.Net
.Search
21 /// <summary> Abstract base class for sorting hits returned by a Query.
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.
32 /// <p>Created: Apr 21, 2004 5:08:38 PM
35 /// <author> Tim Jones
37 /// <version> $Id: SortComparator.cs,v 1.1 2005/01/17 19:54:30 joeshaw Exp $
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
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
;
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.
98 /// <param name="termtext">The textual value of the term.
100 /// <returns> An object representing <code>termtext</code> that sorts according to the natural order of <code>termtext</code>.
102 /// <seealso cref="Comparable">
104 /// <seealso cref="ScoreDocComparator">
106 public /*protected internal*/ abstract System
.IComparable
GetComparable(System
.String termtext
);