KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > SortComparator


1 package org.apache.lucene.search;
2
3 import org.apache.lucene.index.IndexReader;
4
5 import java.io.IOException JavaDoc;
6
7 /**
8  * Abstract base class for sorting hits returned by a Query.
9  *
10  * <p>This class should only be used if the other SortField
11  * types (SCORE, DOC, STRING, INT, FLOAT) do not provide an
12  * adequate sorting. It maintains an internal cache of values which
13  * could be quite large. The cache is an array of Comparable,
14  * one for each document in the index. There is a distinct
15  * Comparable for each unique term in the field - if
16  * some documents have the same term in the field, the cache
17  * array will have entries which reference the same Comparable.
18  *
19  * <p>Created: Apr 21, 2004 5:08:38 PM
20  *
21  * @author Tim Jones
22  * @version $Id: SortComparator.java 150541 2004-09-29 15:09:02Z goller $
23  * @since 1.4
24  */

25 public abstract class SortComparator
26 implements SortComparatorSource {
27
28   // inherit javadocs
29
public ScoreDocComparator newComparator (final IndexReader reader, final String JavaDoc fieldname)
30   throws IOException JavaDoc {
31     final String JavaDoc field = fieldname.intern();
32     final Comparable JavaDoc[] cachedValues = FieldCache.DEFAULT.getCustom (reader, field, SortComparator.this);
33     
34     return new ScoreDocComparator() {
35
36       public int compare (ScoreDoc i, ScoreDoc j) {
37         return cachedValues[i.doc].compareTo (cachedValues[j.doc]);
38       }
39
40       public Comparable JavaDoc sortValue (ScoreDoc i) {
41         return cachedValues[i.doc];
42       }
43
44       public int sortType(){
45         return SortField.CUSTOM;
46       }
47     };
48   }
49
50   /**
51    * Returns an object which, when sorted according to natural order,
52    * will order the Term values in the correct order.
53    * <p>For example, if the Terms contained integer values, this method
54    * would return <code>new Integer(termtext)</code>. Note that this
55    * might not always be the most efficient implementation - for this
56    * particular example, a better implementation might be to make a
57    * ScoreDocLookupComparator that uses an internal lookup table of int.
58    * @param termtext The textual value of the term.
59    * @return An object representing <code>termtext</code> that sorts according to the natural order of <code>termtext</code>.
60    * @see Comparable
61    * @see ScoreDocComparator
62    */

63   protected abstract Comparable JavaDoc getComparable (String JavaDoc termtext);
64
65 }
Popular Tags