| 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 java.io.IOException; |
| 20 |
|
|
| 21 |
|
import org.apache.lucene.index.IndexReader; |
| 22 |
|
import org.apache.lucene.index.Term; |
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
@version |
| 32 |
|
|
|
|
|
| 96.7% |
Uncovered Elements: 3 (92) |
Complexity: 25 |
Complexity Density: 0.45 |
|
| 33 |
|
public class WildcardTermEnum extends FilteredTermEnum { |
| 34 |
|
Term searchTerm; |
| 35 |
|
String field = ""; |
| 36 |
|
String text = ""; |
| 37 |
|
String pre = ""; |
| 38 |
|
int preLen = 0; |
| 39 |
|
boolean endEnum = false; |
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
@link |
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
|
|
|
| 89.5% |
Uncovered Elements: 2 (19) |
Complexity: 3 |
Complexity Density: 0.2 |
|
| 49 |
17
|
public WildcardTermEnum(IndexReader reader, Term term) throws IOException {... |
| 50 |
17
|
super(); |
| 51 |
17
|
searchTerm = term; |
| 52 |
17
|
field = searchTerm.field(); |
| 53 |
17
|
text = searchTerm.text(); |
| 54 |
|
|
| 55 |
17
|
int sidx = text.indexOf(WILDCARD_STRING); |
| 56 |
17
|
int cidx = text.indexOf(WILDCARD_CHAR); |
| 57 |
17
|
int idx = sidx; |
| 58 |
17
|
if (idx == -1) { |
| 59 |
6
|
idx = cidx; |
| 60 |
|
} |
| 61 |
11
|
else if (cidx >= 0) { |
| 62 |
0
|
idx = Math.min(idx, cidx); |
| 63 |
|
} |
| 64 |
|
|
| 65 |
17
|
pre = searchTerm.text().substring(0,idx); |
| 66 |
17
|
preLen = pre.length(); |
| 67 |
17
|
text = text.substring(preLen); |
| 68 |
17
|
setEnum(reader.terms(new Term(searchTerm.field(), pre))); |
| 69 |
|
} |
| 70 |
|
|
|
|
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
| 71 |
38
|
protected final boolean termCompare(Term term) {... |
| 72 |
38
|
if (field == term.field()) { |
| 73 |
38
|
String searchText = term.text(); |
| 74 |
38
|
if (searchText.startsWith(pre)) { |
| 75 |
33
|
return wildcardEquals(text, 0, searchText, preLen); |
| 76 |
|
} |
| 77 |
|
} |
| 78 |
5
|
endEnum = true; |
| 79 |
5
|
return false; |
| 80 |
|
} |
| 81 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 82 |
18
|
public final float difference() {... |
| 83 |
18
|
return 1.0f; |
| 84 |
|
} |
| 85 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 86 |
42
|
public final boolean endEnum() {... |
| 87 |
42
|
return endEnum; |
| 88 |
|
} |
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
| 94 |
|
public static final char WILDCARD_STRING = '*'; |
| 95 |
|
public static final char WILDCARD_CHAR = '?'; |
| 96 |
|
|
| 97 |
|
|
| 98 |
|
|
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (51) |
Complexity: 16 |
Complexity Density: 0.55 |
|
| 102 |
128
|
public static final boolean wildcardEquals(String pattern, int patternIdx,... |
| 103 |
|
String string, int stringIdx) |
| 104 |
|
{ |
| 105 |
128
|
int p = patternIdx; |
| 106 |
|
|
| 107 |
128
|
for (int s = stringIdx; ; ++p, ++s) |
| 108 |
|
{ |
| 109 |
|
|
| 110 |
222
|
boolean sEnd = (s >= string.length()); |
| 111 |
|
|
| 112 |
222
|
boolean pEnd = (p >= pattern.length()); |
| 113 |
|
|
| 114 |
|
|
| 115 |
222
|
if (sEnd) |
| 116 |
|
{ |
| 117 |
|
|
| 118 |
44
|
boolean justWildcardsLeft = true; |
| 119 |
|
|
| 120 |
|
|
| 121 |
44
|
int wildcardSearchPos = p; |
| 122 |
|
|
| 123 |
|
|
| 124 |
70
|
while (wildcardSearchPos < pattern.length() && justWildcardsLeft) |
| 125 |
|
{ |
| 126 |
|
|
| 127 |
29
|
char wildchar = pattern.charAt(wildcardSearchPos); |
| 128 |
|
|
| 129 |
|
|
| 130 |
|
|
| 131 |
29
|
if (wildchar != WILDCARD_CHAR && wildchar != WILDCARD_STRING) |
| 132 |
|
{ |
| 133 |
23
|
justWildcardsLeft = false; |
| 134 |
|
} |
| 135 |
|
else |
| 136 |
|
{ |
| 137 |
|
|
| 138 |
6
|
if (wildchar == WILDCARD_CHAR) { |
| 139 |
3
|
return false; |
| 140 |
|
} |
| 141 |
|
|
| 142 |
|
|
| 143 |
3
|
wildcardSearchPos++; |
| 144 |
|
} |
| 145 |
|
} |
| 146 |
|
|
| 147 |
|
|
| 148 |
|
|
| 149 |
41
|
if (justWildcardsLeft) |
| 150 |
|
{ |
| 151 |
18
|
return true; |
| 152 |
|
} |
| 153 |
|
} |
| 154 |
|
|
| 155 |
|
|
| 156 |
|
|
| 157 |
201
|
if (sEnd || pEnd) |
| 158 |
|
{ |
| 159 |
28
|
break; |
| 160 |
|
} |
| 161 |
|
|
| 162 |
|
|
| 163 |
173
|
if (pattern.charAt(p) == WILDCARD_CHAR) |
| 164 |
|
{ |
| 165 |
16
|
continue; |
| 166 |
|
} |
| 167 |
|
|
| 168 |
|
|
| 169 |
157
|
if (pattern.charAt(p) == WILDCARD_STRING) |
| 170 |
|
{ |
| 171 |
|
|
| 172 |
21
|
++p; |
| 173 |
|
|
| 174 |
103
|
for (int i = string.length(); i >= s; --i) |
| 175 |
|
{ |
| 176 |
95
|
if (wildcardEquals(pattern, p, string, i)) |
| 177 |
|
{ |
| 178 |
13
|
return true; |
| 179 |
|
} |
| 180 |
|
} |
| 181 |
8
|
break; |
| 182 |
|
} |
| 183 |
136
|
if (pattern.charAt(p) != string.charAt(s)) |
| 184 |
|
{ |
| 185 |
58
|
break; |
| 186 |
|
} |
| 187 |
|
} |
| 188 |
94
|
return false; |
| 189 |
|
} |
| 190 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 191 |
17
|
public void close() throws IOException... |
| 192 |
|
{ |
| 193 |
17
|
super.close(); |
| 194 |
17
|
searchTerm = null; |
| 195 |
17
|
field = null; |
| 196 |
17
|
text = null; |
| 197 |
|
} |
| 198 |
|
} |