1 package org.apache.lucene.search;
2
3 /**
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20 import org.apache.lucene.index.IndexReader;
21
22 import java.io.IOException;
23
24 /**
25 * Abstract base class for sorting hits returned by a Query.
26 *
27 * <p>This class should only be used if the other SortField
28 * types (SCORE, DOC, STRING, INT, FLOAT) do not provide an
29 * adequate sorting. It maintains an internal cache of values which
30 * could be quite large. The cache is an array of Comparable,
31 * one for each document in the index. There is a distinct
32 * Comparable for each unique term in the field - if
33 * some documents have the same term in the field, the cache
34 * array will have entries which reference the same Comparable.
35 *
36 * <p>Created: Apr 21, 2004 5:08:38 PM
37 *
38 *
39 * @version $Id: SortComparator.java 564236 2007-08-09 15:21:19Z gsingers $
40 * @since 1.4
41 */
42 public abstract class SortComparator
43 implements SortComparatorSource {
44
45 // inherit javadocs
46 public ScoreDocComparator newComparator (final IndexReader reader, final String fieldname)
47 throws IOException {
48 final String field = fieldname.intern();
49 final Comparable[] cachedValues = FieldCache.DEFAULT.getCustom (reader, field, SortComparator.this);
50
51 return new ScoreDocComparator() {
52
53 public int compare (ScoreDoc i, ScoreDoc j) {
54 return cachedValues[i.doc].compareTo (cachedValues[j.doc]);
55 }
56
57 public Comparable sortValue (ScoreDoc i) {
58 return cachedValues[i.doc];
59 }
60
61 public int sortType(){
62 return SortField.CUSTOM;
63 }
64 };
65 }
66
67 /**
68 * Returns an object which, when sorted according to natural order,
69 * will order the Term values in the correct order.
70 * <p>For example, if the Terms contained integer values, this method
71 * would return <code>new Integer(termtext)</code>. Note that this
72 * might not always be the most efficient implementation - for this
73 * particular example, a better implementation might be to make a
74 * ScoreDocLookupComparator that uses an internal lookup table of int.
75 * @param termtext The textual value of the term.
76 * @return An object representing <code>termtext</code> that sorts according to the natural order of <code>termtext</code>.
77 * @see Comparable
78 * @see ScoreDocComparator
79 */
80 protected abstract Comparable getComparable (String termtext);
81
82 }