|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SortComparator | Line # 25 | 6 | 4 | 100% |
1.0
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (2) | |||
| Result | |||
|
1.0
|
org.apache.lucene.search.TestSort.testRemoteCustomSort
org.apache.lucene.search.TestSort.testRemoteCustomSort
|
1 PASS | |
|
1.0
|
org.apache.lucene.search.TestSort.testCustomSorts
org.apache.lucene.search.TestSort.testCustomSorts
|
1 PASS | |
| 1 | package org.apache.lucene.search; | |
| 2 | ||
| 3 | import org.apache.lucene.index.IndexReader; | |
| 4 | ||
| 5 | import java.io.IOException; | |
| 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 | 3 |
public ScoreDocComparator newComparator (final IndexReader reader, final String fieldname) |
| 30 | throws IOException { | |
| 31 | 3 | final String field = fieldname.intern(); |
| 32 | 3 | final Comparable[] cachedValues = FieldCache.DEFAULT.getCustom (reader, field, SortComparator.this); |
| 33 | ||
| 34 | 3 | return new ScoreDocComparator() { |
| 35 | ||
| 36 | 40 |
public int compare (ScoreDoc i, ScoreDoc j) { |
| 37 | 40 | return cachedValues[i.doc].compareTo (cachedValues[j.doc]); |
| 38 | } | |
| 39 | ||
| 40 | 20 |
public Comparable sortValue (ScoreDoc i) { |
| 41 | 20 | return cachedValues[i.doc]; |
| 42 | } | |
| 43 | ||
| 44 | 8 |
public int sortType(){ |
| 45 | 8 | 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 getComparable (String termtext); | |
| 64 | ||
| 65 | } | |
|
||||||||||