FileDocCategorySizeDatePackage
SampleComparable.javaAPI DocApache Lucene 2.1.04987Wed Feb 14 10:46:36 GMT 2007org.apache.lucene.search

SampleComparable

public class SampleComparable extends Object implements Comparable, Serializable
An example Comparable for use with the custom sort tests. It implements a comparable for "id" sort of values which consist of an alphanumeric part and a numeric part, such as:

ABC-123, A-1, A-7, A-100, B-99999

Such values cannot be sorted as strings, since A-100 needs to come after A-7.

It could be argued that the "ids" should be rewritten as A-0001, A-0100, etc. so they will sort as strings. That is a valid alternate way to solve it - but this is only supposed to be a simple test case.

Created: Apr 21, 2004 5:34:47 PM

author
Tim Jones
version
$Id: SampleComparable.java 472959 2006-11-09 16:21:50Z yonik $
since
1.4

Fields Summary
String
string_part
Integer
int_part
Constructors Summary
public SampleComparable(String s)

    int i = s.indexOf ("-");
    string_part = s.substring (0, i);
    int_part = new Integer (s.substring (i + 1));
  
Methods Summary
public intcompareTo(java.lang.Object o)

    SampleComparable otherid = (SampleComparable) o;
    int i = string_part.compareTo (otherid.string_part);
    if (i == 0) return int_part.compareTo (otherid.int_part);
    return i;
  
public static org.apache.lucene.search.SortComparatorgetComparator()

    return new SortComparator() {
      protected Comparable getComparable (String termtext) {
        return new SampleComparable (termtext);
      }
    };
  
public static org.apache.lucene.search.SortComparatorSourcegetComparatorSource()

    return new SortComparatorSource () {
      public ScoreDocComparator newComparator (final IndexReader reader, String fieldname)
      throws IOException {
        final String field = fieldname.intern ();
        final TermEnum enumerator = reader.terms (new Term (fieldname, ""));
        try {
          return new ScoreDocComparator () {
            protected Comparable[] cachedValues = fillCache (reader, enumerator, field);

            public int compare (ScoreDoc i, ScoreDoc j) {
              return cachedValues[i.doc].compareTo (cachedValues[j.doc]);
            }

            public Comparable sortValue (ScoreDoc i) {
              return cachedValues[i.doc];
            }

            public int sortType () {
              return SortField.CUSTOM;
            }
          };
        } finally {
          enumerator.close ();
        }
      }

      /**
       * Returns an array of objects which represent that natural order
       * of the term values in the given field.
       *
       * @param reader     Terms are in this index.
       * @param enumerator Use this to get the term values and TermDocs.
       * @param fieldname  Comparables should be for this field.
       * @return Array of objects representing natural order of terms in field.
       * @throws IOException If an error occurs reading the index.
       */
      protected Comparable[] fillCache (IndexReader reader, TermEnum enumerator, String fieldname)
      throws IOException {
        final String field = fieldname.intern ();
        Comparable[] retArray = new Comparable[reader.maxDoc ()];
        if (retArray.length > 0) {
          TermDocs termDocs = reader.termDocs ();
          try {
            if (enumerator.term () == null) {
              throw new RuntimeException ("no terms in field " + field);
            }
            do {
              Term term = enumerator.term ();
              if (term.field () != field) break;
              Comparable termval = getComparable (term.text ());
              termDocs.seek (enumerator);
              while (termDocs.next ()) {
                retArray[termDocs.doc ()] = termval;
              }
            } while (enumerator.next ());
          } finally {
            termDocs.close ();
          }
        }
        return retArray;
      }

      Comparable getComparable (String termtext) {
        return new SampleComparable (termtext);
      }
    };