Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
37   143   17   3.36
12   82   0.46   11
11     1.55  
1    
 
  SampleComparable       Line # 48 37 17 93.3% 0.93333334
 
  (2)
 
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;
25    import java.io.Serializable;
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 150348 2004-05-19 23:05:27Z tjones $
46    * @since 1.4
47    */
 
48    public class SampleComparable
49    implements Comparable, Serializable {
50   
51    String string_part;
52    Integer int_part;
53   
 
54  70 toggle public SampleComparable (String s) {
55  70 int i = s.indexOf ("-");
56  70 string_part = s.substring (0, i);
57  70 int_part = new Integer (s.substring (i + 1));
58    }
59   
 
60  128 toggle public int compareTo (Object o) {
61  128 SampleComparable otherid = (SampleComparable) o;
62  128 int i = string_part.compareTo (otherid.string_part);
63  128 if (i == 0) return int_part.compareTo (otherid.int_part);
64  58 return i;
65    }
66   
 
67  4 toggle public static SortComparatorSource getComparatorSource () {
68  4 return new SortComparatorSource () {
 
69  4 toggle public ScoreDocComparator newComparator (final IndexReader reader, String fieldname)
70    throws IOException {
71  4 final String field = fieldname.intern ();
72  4 final TermEnum enumerator = reader.terms (new Term (fieldname, ""));
73  4 try {
74  4 return new ScoreDocComparator () {
75    protected Comparable[] cachedValues = fillCache (reader, enumerator, field);
76   
 
77  40 toggle public int compare (ScoreDoc i, ScoreDoc j) {
78  40 return cachedValues[i.doc].compareTo (cachedValues[j.doc]);
79    }
80   
 
81  20 toggle public Comparable sortValue (ScoreDoc i) {
82  20 return cachedValues[i.doc];
83    }
84   
 
85  8 toggle public int sortType () {
86  8 return SortField.CUSTOM;
87    }
88    };
89    } finally {
90  4 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  4 toggle protected Comparable[] fillCache (IndexReader reader, TermEnum enumerator, String fieldname)
105    throws IOException {
106  4 final String field = fieldname.intern ();
107  4 Comparable[] retArray = new Comparable[reader.maxDoc ()];
108  4 if (retArray.length > 0) {
109  4 TermDocs termDocs = reader.termDocs ();
110  4 try {
111  4 if (enumerator.term () == null) {
112  0 throw new RuntimeException ("no terms in field " + field);
113    }
114  4 do {
115  44 Term term = enumerator.term ();
116  44 if (term.field () != field) break;
117  40 Comparable termval = getComparable (term.text ());
118  40 termDocs.seek (enumerator);
119  80 while (termDocs.next ()) {
120  40 retArray[termDocs.doc ()] = termval;
121    }
122  40 } while (enumerator.next ());
123    } finally {
124  4 termDocs.close ();
125    }
126    }
127  4 return retArray;
128    }
129   
 
130  40 toggle Comparable getComparable (String termtext) {
131  40 return new SampleComparable (termtext);
132    }
133    };
134    }
135   
 
136  2 toggle public static SortComparator getComparator() {
137  2 return new SortComparator() {
 
138  30 toggle protected Comparable getComparable (String termtext) {
139  30 return new SampleComparable (termtext);
140    }
141    };
142    }
143    }