1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.util.matcher; |
22 |
|
|
23 |
|
import java.util.Locale; |
24 |
|
|
25 |
|
import java.util.Objects; |
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
|
|
| 40.4% |
Uncovered Elements: 96 (161) |
Complexity: 48 |
Complexity Density: 0.38 |
|
30 |
|
public class Matcher implements MatcherI |
31 |
|
{ |
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
32 |
|
public enum PatternType |
33 |
|
{ |
34 |
|
String, Integer, Float |
35 |
|
} |
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
private final Condition condition; |
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
private String pattern; |
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
private final String uppercasePattern; |
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
private float floatValue = 0F; |
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
private long longValue = 0L; |
67 |
|
|
68 |
|
private PatternType patternType; |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
@param |
74 |
|
@param |
75 |
|
@return |
76 |
|
@throws |
77 |
|
|
78 |
|
|
79 |
|
@throws |
80 |
|
|
81 |
|
|
|
|
| 94.1% |
Uncovered Elements: 1 (17) |
Complexity: 4 |
Complexity Density: 0.31 |
|
82 |
17 |
public Matcher(Condition cond, String compareTo)... |
83 |
|
{ |
84 |
17 |
Objects.requireNonNull(cond); |
85 |
17 |
condition = cond; |
86 |
|
|
87 |
17 |
if (cond.isNumeric()) |
88 |
|
{ |
89 |
8 |
try |
90 |
|
{ |
91 |
8 |
longValue = Long.valueOf(compareTo); |
92 |
2 |
pattern = String.valueOf(longValue); |
93 |
2 |
patternType = PatternType.Integer; |
94 |
|
} catch (NumberFormatException e) |
95 |
|
{ |
96 |
6 |
floatValue = Float.valueOf(compareTo); |
97 |
6 |
pattern = String.valueOf(floatValue); |
98 |
6 |
patternType = PatternType.Float; |
99 |
|
} |
100 |
|
} |
101 |
|
else |
102 |
|
{ |
103 |
9 |
pattern = compareTo; |
104 |
9 |
patternType = PatternType.String; |
105 |
|
} |
106 |
|
|
107 |
17 |
uppercasePattern = pattern == null ? null |
108 |
|
: pattern.toUpperCase(Locale.ROOT); |
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
} |
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
@param |
121 |
|
@param |
122 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
123 |
0 |
public Matcher(Condition cond, float compareTo)... |
124 |
|
{ |
125 |
0 |
this(cond, String.valueOf(compareTo)); |
126 |
|
} |
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
@param |
134 |
|
@param |
135 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
136 |
0 |
public Matcher(Condition cond, long compareTo)... |
137 |
|
{ |
138 |
0 |
this(cond, String.valueOf(compareTo)); |
139 |
|
} |
140 |
|
|
141 |
|
|
142 |
|
@inheritDoc |
143 |
|
|
|
|
| 81.2% |
Uncovered Elements: 3 (16) |
Complexity: 4 |
Complexity Density: 0.29 |
|
144 |
11 |
@Override... |
145 |
|
public boolean matches(String compareTo) |
146 |
|
{ |
147 |
11 |
if (compareTo == null) |
148 |
|
{ |
149 |
3 |
return matchesNull(); |
150 |
|
} |
151 |
|
|
152 |
8 |
boolean matched = false; |
153 |
8 |
switch (patternType) |
154 |
|
{ |
155 |
2 |
case Float: |
156 |
2 |
matched = matchesFloat(compareTo, floatValue); |
157 |
2 |
break; |
158 |
0 |
case Integer: |
159 |
0 |
matched = matchesLong(compareTo); |
160 |
0 |
break; |
161 |
6 |
default: |
162 |
6 |
matched = matchesString(compareTo); |
163 |
6 |
break; |
164 |
|
} |
165 |
8 |
return matched; |
166 |
|
} |
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
|
171 |
|
|
172 |
|
@param |
173 |
|
@return |
174 |
|
|
|
|
| 47.6% |
Uncovered Elements: 11 (21) |
Complexity: 6 |
Complexity Density: 0.29 |
|
175 |
6 |
boolean matchesString(String compareTo)... |
176 |
|
{ |
177 |
6 |
boolean matched = false; |
178 |
6 |
String upper = compareTo.toUpperCase(Locale.ROOT).trim(); |
179 |
6 |
switch (condition) |
180 |
|
{ |
181 |
2 |
case Matches: |
182 |
2 |
matched = upper.equals(uppercasePattern); |
183 |
2 |
break; |
184 |
0 |
case NotMatches: |
185 |
0 |
matched = !upper.equals(uppercasePattern); |
186 |
0 |
break; |
187 |
4 |
case Contains: |
188 |
4 |
matched = upper.indexOf(uppercasePattern) > -1; |
189 |
4 |
break; |
190 |
0 |
case NotContains: |
191 |
0 |
matched = upper.indexOf(uppercasePattern) == -1; |
192 |
0 |
break; |
193 |
0 |
case Present: |
194 |
0 |
matched = true; |
195 |
0 |
break; |
196 |
0 |
default: |
197 |
0 |
break; |
198 |
|
} |
199 |
6 |
return matched; |
200 |
|
} |
201 |
|
|
202 |
|
|
203 |
|
|
204 |
|
|
205 |
|
@param |
206 |
|
@param |
207 |
|
@return |
208 |
|
|
|
|
| 45.2% |
Uncovered Elements: 17 (31) |
Complexity: 9 |
Complexity Density: 0.31 |
|
209 |
2 |
boolean matchesFloat(String testee, float compareTo)... |
210 |
|
{ |
211 |
2 |
if (!condition.isNumeric()) |
212 |
|
{ |
213 |
|
|
214 |
0 |
return matches(testee); |
215 |
|
} |
216 |
|
|
217 |
2 |
float f = 0f; |
218 |
2 |
try |
219 |
|
{ |
220 |
2 |
f = Float.valueOf(testee); |
221 |
|
} catch (NumberFormatException e) |
222 |
|
{ |
223 |
0 |
return false; |
224 |
|
} |
225 |
|
|
226 |
2 |
boolean matched = false; |
227 |
2 |
switch (condition) |
228 |
|
{ |
229 |
1 |
case LT: |
230 |
1 |
matched = f < compareTo; |
231 |
1 |
break; |
232 |
1 |
case LE: |
233 |
1 |
matched = f <= compareTo; |
234 |
1 |
break; |
235 |
0 |
case EQ: |
236 |
0 |
matched = f == compareTo; |
237 |
0 |
break; |
238 |
0 |
case NE: |
239 |
0 |
matched = f != compareTo; |
240 |
0 |
break; |
241 |
0 |
case GT: |
242 |
0 |
matched = f > compareTo; |
243 |
0 |
break; |
244 |
0 |
case GE: |
245 |
0 |
matched = f >= compareTo; |
246 |
0 |
break; |
247 |
0 |
default: |
248 |
0 |
break; |
249 |
|
} |
250 |
|
|
251 |
2 |
return matched; |
252 |
|
} |
253 |
|
|
254 |
|
|
255 |
|
|
256 |
|
|
257 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
258 |
0 |
@Override... |
259 |
|
public int hashCode() |
260 |
|
{ |
261 |
0 |
return pattern.hashCode() + condition.hashCode() + (int) floatValue; |
262 |
|
} |
263 |
|
|
264 |
|
|
265 |
|
|
266 |
|
|
267 |
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 7 |
Complexity Density: 0.88 |
|
268 |
0 |
@Override... |
269 |
|
public boolean equals(Object obj) |
270 |
|
{ |
271 |
0 |
if (obj == null || !(obj instanceof Matcher)) |
272 |
|
{ |
273 |
0 |
return false; |
274 |
|
} |
275 |
0 |
Matcher m = (Matcher) obj; |
276 |
0 |
if (condition != m.condition || floatValue != m.floatValue |
277 |
|
|| longValue != m.longValue) |
278 |
|
{ |
279 |
0 |
return false; |
280 |
|
} |
281 |
0 |
if (pattern == null) |
282 |
|
{ |
283 |
0 |
return m.pattern == null; |
284 |
|
} |
285 |
0 |
return uppercasePattern.equals(m.uppercasePattern); |
286 |
|
} |
287 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
288 |
10 |
@Override... |
289 |
|
public Condition getCondition() |
290 |
|
{ |
291 |
10 |
return condition; |
292 |
|
} |
293 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
294 |
10 |
@Override... |
295 |
|
public String getPattern() |
296 |
|
{ |
297 |
10 |
return pattern; |
298 |
|
} |
299 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
300 |
0 |
@Override... |
301 |
|
public String toString() |
302 |
|
{ |
303 |
0 |
StringBuilder sb = new StringBuilder(); |
304 |
0 |
sb.append(condition.toString()).append(" "); |
305 |
0 |
if (condition.isNumeric()) |
306 |
|
{ |
307 |
0 |
sb.append(pattern); |
308 |
|
} |
309 |
|
else |
310 |
|
{ |
311 |
0 |
sb.append("'").append(pattern).append("'"); |
312 |
|
} |
313 |
|
|
314 |
0 |
return sb.toString(); |
315 |
|
} |
316 |
|
|
317 |
|
|
318 |
|
|
319 |
|
|
320 |
|
|
321 |
|
@param |
322 |
|
@return |
323 |
|
|
|
|
| 0% |
Uncovered Elements: 31 (31) |
Complexity: 9 |
Complexity Density: 0.31 |
|
324 |
0 |
boolean matchesLong(String compareTo)... |
325 |
|
{ |
326 |
0 |
if (!condition.isNumeric()) |
327 |
|
{ |
328 |
|
|
329 |
0 |
return matches(String.valueOf(compareTo)); |
330 |
|
} |
331 |
|
|
332 |
0 |
long val = 0L; |
333 |
0 |
try |
334 |
|
{ |
335 |
0 |
val = Long.valueOf(compareTo); |
336 |
|
} catch (NumberFormatException e) |
337 |
|
{ |
338 |
|
|
339 |
|
|
340 |
|
|
341 |
0 |
return matchesFloat(compareTo, longValue); |
342 |
|
} |
343 |
|
|
344 |
0 |
boolean matched = false; |
345 |
0 |
switch (condition) |
346 |
|
{ |
347 |
0 |
case LT: |
348 |
0 |
matched = val < longValue; |
349 |
0 |
break; |
350 |
0 |
case LE: |
351 |
0 |
matched = val <= longValue; |
352 |
0 |
break; |
353 |
0 |
case EQ: |
354 |
0 |
matched = val == longValue; |
355 |
0 |
break; |
356 |
0 |
case NE: |
357 |
0 |
matched = val != longValue; |
358 |
0 |
break; |
359 |
0 |
case GT: |
360 |
0 |
matched = val > longValue; |
361 |
0 |
break; |
362 |
0 |
case GE: |
363 |
0 |
matched = val >= longValue; |
364 |
0 |
break; |
365 |
0 |
default: |
366 |
0 |
break; |
367 |
|
} |
368 |
|
|
369 |
0 |
return matched; |
370 |
|
} |
371 |
|
|
372 |
|
|
373 |
|
|
374 |
|
|
375 |
|
|
376 |
|
|
377 |
|
|
378 |
|
|
379 |
|
|
380 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
381 |
3 |
boolean matchesNull()... |
382 |
|
{ |
383 |
3 |
if (condition.isNumeric()) |
384 |
|
{ |
385 |
0 |
return false; |
386 |
|
} |
387 |
|
else |
388 |
|
{ |
389 |
3 |
return condition == Condition.NotContains |
390 |
|
|| condition == Condition.NotMatches |
391 |
|
|| condition == Condition.NotPresent; |
392 |
|
} |
393 |
|
} |
394 |
|
} |