001: package org.apache.lucene.search;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import org.apache.lucene.index.IndexReader;
021: import org.apache.lucene.index.Term;
022: import org.apache.lucene.index.TermDocs;
023: import org.apache.lucene.index.TermEnum;
024:
025: import java.io.IOException;
026: import java.io.Serializable;
027:
028: /**
029: * An example Comparable for use with the custom sort tests.
030: * It implements a comparable for "id" sort of values which
031: * consist of an alphanumeric part and a numeric part, such as:
032: * <p/>
033: * <P>ABC-123, A-1, A-7, A-100, B-99999
034: * <p/>
035: * <p>Such values cannot be sorted as strings, since A-100 needs
036: * to come after A-7.
037: * <p/>
038: * <p>It could be argued that the "ids" should be rewritten as
039: * A-0001, A-0100, etc. so they will sort as strings. That is
040: * a valid alternate way to solve it - but
041: * this is only supposed to be a simple test case.
042: * <p/>
043: * <p>Created: Apr 21, 2004 5:34:47 PM
044: *
045: *
046: * @version $Id: SampleComparable.java 564236 2007-08-09 15:21:19Z gsingers $
047: * @since 1.4
048: */
049: public class SampleComparable implements Comparable, Serializable {
050:
051: String string_part;
052: Integer int_part;
053:
054: public SampleComparable(String s) {
055: int i = s.indexOf("-");
056: string_part = s.substring(0, i);
057: int_part = new Integer(s.substring(i + 1));
058: }
059:
060: public int compareTo(Object o) {
061: SampleComparable otherid = (SampleComparable) o;
062: int i = string_part.compareTo(otherid.string_part);
063: if (i == 0)
064: return int_part.compareTo(otherid.int_part);
065: return i;
066: }
067:
068: public static SortComparatorSource getComparatorSource() {
069: return new SortComparatorSource() {
070: public ScoreDocComparator newComparator(
071: final IndexReader reader, String fieldname)
072: throws IOException {
073: final String field = fieldname.intern();
074: final TermEnum enumerator = reader.terms(new Term(
075: fieldname, ""));
076: try {
077: return new ScoreDocComparator() {
078: protected Comparable[] cachedValues = fillCache(
079: reader, enumerator, field);
080:
081: public int compare(ScoreDoc i, ScoreDoc j) {
082: return cachedValues[i.doc]
083: .compareTo(cachedValues[j.doc]);
084: }
085:
086: public Comparable sortValue(ScoreDoc i) {
087: return cachedValues[i.doc];
088: }
089:
090: public int sortType() {
091: return SortField.CUSTOM;
092: }
093: };
094: } finally {
095: enumerator.close();
096: }
097: }
098:
099: /**
100: * Returns an array of objects which represent that natural order
101: * of the term values in the given field.
102: *
103: * @param reader Terms are in this index.
104: * @param enumerator Use this to get the term values and TermDocs.
105: * @param fieldname Comparables should be for this field.
106: * @return Array of objects representing natural order of terms in field.
107: * @throws IOException If an error occurs reading the index.
108: */
109: protected Comparable[] fillCache(IndexReader reader,
110: TermEnum enumerator, String fieldname)
111: throws IOException {
112: final String field = fieldname.intern();
113: Comparable[] retArray = new Comparable[reader.maxDoc()];
114: if (retArray.length > 0) {
115: TermDocs termDocs = reader.termDocs();
116: try {
117: if (enumerator.term() == null) {
118: throw new RuntimeException(
119: "no terms in field " + field);
120: }
121: do {
122: Term term = enumerator.term();
123: if (term.field() != field)
124: break;
125: Comparable termval = getComparable(term
126: .text());
127: termDocs.seek(enumerator);
128: while (termDocs.next()) {
129: retArray[termDocs.doc()] = termval;
130: }
131: } while (enumerator.next());
132: } finally {
133: termDocs.close();
134: }
135: }
136: return retArray;
137: }
138:
139: Comparable getComparable(String termtext) {
140: return new SampleComparable(termtext);
141: }
142: };
143: }
144:
145: public static SortComparator getComparator() {
146: return new SortComparator() {
147: protected Comparable getComparable(String termtext) {
148: return new SampleComparable(termtext);
149: }
150: };
151: }
152: }
|