01 package gate.creole.annic.apache.lucene.search;
02
03 import gate.creole.annic.apache.lucene.index.IndexReader;
04 import gate.creole.annic.apache.lucene.index.Term;
05 import gate.creole.annic.apache.lucene.index.TermDocs;
06 import gate.creole.annic.apache.lucene.index.TermEnum;
07
08 import java.io.IOException;
09 import java.io.Serializable;
10
11 /**
12 * Abstract base class for sorting hits returned by a Query.
13 *
14 * <p>This class should only be used if the other SortField
15 * types (SCORE, DOC, STRING, INT, FLOAT) do not provide an
16 * adequate sorting. It maintains an internal cache of values which
17 * could be quite large. The cache is an array of Comparable,
18 * one for each document in the index. There is a distinct
19 * Comparable for each unique term in the field - if
20 * some documents have the same term in the field, the cache
21 * array will have entries which reference the same Comparable.
22 *
23 * <p>Created: Apr 21, 2004 5:08:38 PM
24 *
25 * @author Tim Jones
26 * @version $Id: SortComparator.java 529 2004-10-05 11:55:26Z niraj $
27 * @since 1.4
28 */
29 public abstract class SortComparator
30 implements SortComparatorSource {
31
32 // inherit javadocs
33 public ScoreDocComparator newComparator (final IndexReader reader, final String fieldname)
34 throws IOException {
35 final String field = fieldname.intern();
36 return new ScoreDocComparator() {
37 protected Comparable[] cachedValues = FieldCache.DEFAULT.getCustom (reader, field, SortComparator.this);
38
39 public int compare (ScoreDoc i, ScoreDoc j) {
40 return cachedValues[i.doc].compareTo (cachedValues[j.doc]);
41 }
42
43 public Comparable sortValue (ScoreDoc i) {
44 return cachedValues[i.doc];
45 }
46
47 public int sortType(){
48 return SortField.CUSTOM;
49 }
50 };
51 }
52
53 /**
54 * Returns an object which, when sorted according to natural order,
55 * will order the Term values in the correct order.
56 * <p>For example, if the Terms contained integer values, this method
57 * would return <code>new Integer(termtext)</code>. Note that this
58 * might not always be the most efficient implementation - for this
59 * particular example, a better implementation might be to make a
60 * ScoreDocLookupComparator that uses an internal lookup table of int.
61 * @param termtext The textual value of the term.
62 * @return An object representing <code>termtext</code> that sorts according to the natural order of <code>termtext</code>.
63 * @see Comparable
64 * @see ScoreDocComparator
65 */
66 protected abstract Comparable getComparable (String termtext);
67
68 }
|