| 1 |
|
package org.apache.lucene.search; |
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 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 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
@author |
| 45 |
|
@version |
| 46 |
|
@since |
| 47 |
|
|
|
|
|
| 93.3% |
Uncovered Elements: 4 (60) |
Complexity: 17 |
Complexity Density: 0.46 |
|
| 48 |
|
public class SampleComparable |
| 49 |
|
implements Comparable, Serializable { |
| 50 |
|
|
| 51 |
|
String string_part; |
| 52 |
|
Integer int_part; |
| 53 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 54 |
70
|
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 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
| 60 |
128
|
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 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 67 |
4
|
public static SortComparatorSource getComparatorSource () {... |
| 68 |
4
|
return new SortComparatorSource () { |
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
| 69 |
4
|
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 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 77 |
40
|
public int compare (ScoreDoc i, ScoreDoc j) {... |
| 78 |
40
|
return cachedValues[i.doc].compareTo (cachedValues[j.doc]); |
| 79 |
|
} |
| 80 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 81 |
20
|
public Comparable sortValue (ScoreDoc i) {... |
| 82 |
20
|
return cachedValues[i.doc]; |
| 83 |
|
} |
| 84 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 85 |
8
|
public int sortType () {... |
| 86 |
8
|
return SortField.CUSTOM; |
| 87 |
|
} |
| 88 |
|
}; |
| 89 |
|
} finally { |
| 90 |
4
|
enumerator.close (); |
| 91 |
|
} |
| 92 |
|
} |
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 96 |
|
|
| 97 |
|
|
| 98 |
|
@param |
| 99 |
|
@param |
| 100 |
|
@param |
| 101 |
|
@return |
| 102 |
|
@throws |
| 103 |
|
|
|
|
|
| 85.2% |
Uncovered Elements: 4 (27) |
Complexity: 6 |
Complexity Density: 0.35 |
|
| 104 |
4
|
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 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 130 |
40
|
Comparable getComparable (String termtext) {... |
| 131 |
40
|
return new SampleComparable (termtext); |
| 132 |
|
} |
| 133 |
|
}; |
| 134 |
|
} |
| 135 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 136 |
2
|
public static SortComparator getComparator() {... |
| 137 |
2
|
return new SortComparator() { |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 138 |
30
|
protected Comparable getComparable (String termtext) {... |
| 139 |
30
|
return new SampleComparable (termtext); |
| 140 |
|
} |
| 141 |
|
}; |
| 142 |
|
} |
| 143 |
|
} |