KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.lucene.search;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.lucene.index.IndexReader;
20 import org.apache.lucene.index.Term;
21 import org.apache.lucene.index.TermDocs;
22 import org.apache.lucene.index.TermEnum;
23
24 import java.io.IOException JavaDoc;
25 import java.io.Serializable JavaDoc;
26
27 /**
28  * An example Comparable for use with the custom sort tests.
29  * It implements a comparable for "id" sort of values which
30  * consist of an alphanumeric part and a numeric part, such as:
31  * <p/>
32  * <P>ABC-123, A-1, A-7, A-100, B-99999
33  * <p/>
34  * <p>Such values cannot be sorted as strings, since A-100 needs
35  * to come after A-7.
36  * <p/>
37  * <p>It could be argued that the "ids" should be rewritten as
38  * A-0001, A-0100, etc. so they will sort as strings. That is
39  * a valid alternate way to solve it - but
40  * this is only supposed to be a simple test case.
41  * <p/>
42  * <p>Created: Apr 21, 2004 5:34:47 PM
43  *
44  * @author Tim Jones
45  * @version $Id: SampleComparable.java,v 1.3 2004/05/19 23:05:27 tjones Exp $
46  * @since 1.4
47  */

48 public class SampleComparable
49 implements Comparable JavaDoc, Serializable JavaDoc {
50
51   String JavaDoc string_part;
52   Integer JavaDoc int_part;
53
54   public SampleComparable (String JavaDoc s) {
55     int i = s.indexOf ("-");
56     string_part = s.substring (0, i);
57     int_part = new Integer JavaDoc (s.substring (i + 1));
58   }
59
60   public int compareTo (Object JavaDoc o) {
61     SampleComparable otherid = (SampleComparable) o;
62     int i = string_part.compareTo (otherid.string_part);
63     if (i == 0) return int_part.compareTo (otherid.int_part);
64     return i;
65   }
66
67   public static SortComparatorSource getComparatorSource () {
68     return new SortComparatorSource () {
69       public ScoreDocComparator newComparator (final IndexReader reader, String JavaDoc fieldname)
70       throws IOException JavaDoc {
71         final String JavaDoc field = fieldname.intern ();
72         final TermEnum enumerator = reader.terms (new Term (fieldname, ""));
73         try {
74           return new ScoreDocComparator () {
75             protected Comparable JavaDoc[] cachedValues = fillCache (reader, enumerator, field);
76
77             public int compare (ScoreDoc i, ScoreDoc j) {
78               return cachedValues[i.doc].compareTo (cachedValues[j.doc]);
79             }
80
81             public Comparable JavaDoc sortValue (ScoreDoc i) {
82               return cachedValues[i.doc];
83             }
84
85             public int sortType () {
86               return SortField.CUSTOM;
87             }
88           };
89         } finally {
90           enumerator.close ();
91         }
92       }
93
94       /**
95        * Returns an array of objects which represent that natural order
96        * of the term values in the given field.
97        *
98        * @param reader Terms are in this index.
99        * @param enumerator Use this to get the term values and TermDocs.
100        * @param fieldname Comparables should be for this field.
101        * @return Array of objects representing natural order of terms in field.
102        * @throws IOException If an error occurs reading the index.
103        */

104       protected Comparable JavaDoc[] fillCache (IndexReader reader, TermEnum enumerator, String JavaDoc fieldname)
105       throws IOException JavaDoc {
106         final String JavaDoc field = fieldname.intern ();
107         Comparable JavaDoc[] retArray = new Comparable JavaDoc[reader.maxDoc ()];
108         if (retArray.length > 0) {
109           TermDocs termDocs = reader.termDocs ();
110           try {
111             if (enumerator.term () == null) {
112               throw new RuntimeException JavaDoc ("no terms in field " + field);
113             }
114             do {
115               Term term = enumerator.term ();
116               if (term.field () != field) break;
117               Comparable JavaDoc termval = getComparable (term.text ());
118               termDocs.seek (enumerator);
119               while (termDocs.next ()) {
120                 retArray[termDocs.doc ()] = termval;
121               }
122             } while (enumerator.next ());
123           } finally {
124             termDocs.close ();
125           }
126         }
127         return retArray;
128       }
129
130       Comparable JavaDoc getComparable (String JavaDoc termtext) {
131         return new SampleComparable (termtext);
132       }
133     };
134   }
135
136   public static SortComparator getComparator() {
137     return new SortComparator() {
138       protected Comparable JavaDoc getComparable (String JavaDoc termtext) {
139         return new SampleComparable (termtext);
140       }
141     };
142   }
143 }
Popular Tags