1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.datamodel.features; |
22 |
|
|
23 |
|
import java.util.Locale; |
24 |
|
|
25 |
|
import jalview.datamodel.SequenceFeature; |
26 |
|
import jalview.util.MessageManager; |
27 |
|
|
28 |
|
import java.util.ArrayList; |
29 |
|
import java.util.List; |
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
@author |
36 |
|
|
|
|
| 34.5% |
Uncovered Elements: 91 (139) |
Complexity: 34 |
Complexity Density: 0.39 |
|
37 |
|
public class FeatureMatcherSet implements FeatureMatcherSetI |
38 |
|
{ |
39 |
|
private static final String OR = "OR"; |
40 |
|
|
41 |
|
private static final String AND = "AND"; |
42 |
|
|
43 |
|
private static final String SPACE = " "; |
44 |
|
|
45 |
|
private static final String CLOSE_BRACKET = ")"; |
46 |
|
|
47 |
|
private static final String OPEN_BRACKET = "("; |
48 |
|
|
49 |
|
private static final String OR_I18N = MessageManager |
50 |
|
.getString("label.or"); |
51 |
|
|
52 |
|
private static final String AND_18N = MessageManager |
53 |
|
.getString("label.and"); |
54 |
|
|
55 |
|
List<FeatureMatcherI> matchConditions; |
56 |
|
|
57 |
|
boolean andConditions; |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
@param |
73 |
|
@return |
74 |
|
@see |
75 |
|
|
|
|
| 0% |
Uncovered Elements: 58 (58) |
Complexity: 11 |
Complexity Density: 0.28 |
|
76 |
0 |
public static FeatureMatcherSet fromString(final String descriptor)... |
77 |
|
{ |
78 |
0 |
String invalid = "Invalid descriptor: " + descriptor; |
79 |
0 |
boolean firstCondition = true; |
80 |
0 |
FeatureMatcherSet result = new FeatureMatcherSet(); |
81 |
|
|
82 |
0 |
String leftToParse = descriptor.trim(); |
83 |
|
|
84 |
0 |
while (leftToParse.length() > 0) |
85 |
|
{ |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
0 |
boolean and = true; |
90 |
0 |
if (!firstCondition) |
91 |
|
{ |
92 |
0 |
int spacePos = leftToParse.indexOf(SPACE); |
93 |
0 |
if (spacePos == -1) |
94 |
|
{ |
95 |
|
|
96 |
0 |
jalview.bin.Console.errPrintln(invalid); |
97 |
0 |
return null; |
98 |
|
} |
99 |
0 |
String conjunction = leftToParse.substring(0, spacePos); |
100 |
0 |
leftToParse = leftToParse.substring(spacePos + 1).trim(); |
101 |
0 |
if (conjunction.equalsIgnoreCase(AND)) |
102 |
|
{ |
103 |
0 |
and = true; |
104 |
|
} |
105 |
0 |
else if (conjunction.equalsIgnoreCase(OR)) |
106 |
|
{ |
107 |
0 |
and = false; |
108 |
|
} |
109 |
|
else |
110 |
|
{ |
111 |
|
|
112 |
0 |
jalview.bin.Console.errPrintln(invalid); |
113 |
0 |
return null; |
114 |
|
} |
115 |
|
} |
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
0 |
String nextCondition = leftToParse; |
121 |
0 |
if (leftToParse.startsWith(OPEN_BRACKET)) |
122 |
|
{ |
123 |
0 |
int closePos = leftToParse.indexOf(CLOSE_BRACKET); |
124 |
0 |
if (closePos == -1) |
125 |
|
{ |
126 |
0 |
jalview.bin.Console.errPrintln(invalid); |
127 |
0 |
return null; |
128 |
|
} |
129 |
0 |
nextCondition = leftToParse.substring(1, closePos); |
130 |
0 |
leftToParse = leftToParse.substring(closePos + 1).trim(); |
131 |
|
} |
132 |
|
else |
133 |
|
{ |
134 |
0 |
leftToParse = ""; |
135 |
|
} |
136 |
|
|
137 |
0 |
FeatureMatcher fm = FeatureMatcher.fromString(nextCondition); |
138 |
0 |
if (fm == null) |
139 |
|
{ |
140 |
0 |
jalview.bin.Console.errPrintln(invalid); |
141 |
0 |
return null; |
142 |
|
} |
143 |
0 |
try |
144 |
|
{ |
145 |
0 |
if (and) |
146 |
|
{ |
147 |
0 |
result.and(fm); |
148 |
|
} |
149 |
|
else |
150 |
|
{ |
151 |
0 |
result.or(fm); |
152 |
|
} |
153 |
0 |
firstCondition = false; |
154 |
|
} catch (IllegalStateException e) |
155 |
|
{ |
156 |
|
|
157 |
0 |
jalview.bin.Console.errPrintln(invalid); |
158 |
0 |
return null; |
159 |
|
} |
160 |
|
|
161 |
|
} |
162 |
0 |
return result; |
163 |
|
} |
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
168 |
13 |
public FeatureMatcherSet()... |
169 |
|
{ |
170 |
13 |
matchConditions = new ArrayList<>(); |
171 |
|
} |
172 |
|
|
|
|
| 52.6% |
Uncovered Elements: 9 (19) |
Complexity: 5 |
Complexity Density: 0.45 |
|
173 |
10 |
@Override... |
174 |
|
public boolean matches(SequenceFeature feature) |
175 |
|
{ |
176 |
|
|
177 |
|
|
178 |
|
|
179 |
10 |
if (matchConditions.isEmpty()) |
180 |
|
{ |
181 |
0 |
return true; |
182 |
|
} |
183 |
|
|
184 |
|
|
185 |
|
|
186 |
|
|
187 |
10 |
if (andConditions) |
188 |
|
{ |
189 |
10 |
for (FeatureMatcherI m : matchConditions) |
190 |
|
{ |
191 |
11 |
if (!m.matches(feature)) |
192 |
|
{ |
193 |
7 |
return false; |
194 |
|
} |
195 |
|
} |
196 |
3 |
return true; |
197 |
|
} |
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
|
202 |
0 |
for (FeatureMatcherI m : matchConditions) |
203 |
|
{ |
204 |
0 |
if (m.matches(feature)) |
205 |
|
{ |
206 |
0 |
return true; |
207 |
|
} |
208 |
|
} |
209 |
0 |
return false; |
210 |
|
} |
211 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
212 |
13 |
@Override... |
213 |
|
public void and(FeatureMatcherI m) |
214 |
|
{ |
215 |
13 |
if (!andConditions && matchConditions.size() > 1) |
216 |
|
{ |
217 |
0 |
throw new IllegalStateException("Can't add an AND to OR conditions"); |
218 |
|
} |
219 |
13 |
matchConditions.add(m); |
220 |
13 |
andConditions = true; |
221 |
|
} |
222 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
223 |
3 |
@Override... |
224 |
|
public void or(FeatureMatcherI m) |
225 |
|
{ |
226 |
3 |
if (andConditions && matchConditions.size() > 1) |
227 |
|
{ |
228 |
0 |
throw new IllegalStateException("Can't add an OR to AND conditions"); |
229 |
|
} |
230 |
3 |
matchConditions.add(m); |
231 |
3 |
andConditions = false; |
232 |
|
} |
233 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
234 |
3 |
@Override... |
235 |
|
public boolean isAnded() |
236 |
|
{ |
237 |
3 |
return andConditions; |
238 |
|
} |
239 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
240 |
3 |
@Override... |
241 |
|
public Iterable<FeatureMatcherI> getMatchers() |
242 |
|
{ |
243 |
3 |
return matchConditions; |
244 |
|
} |
245 |
|
|
246 |
|
|
247 |
|
|
248 |
|
|
249 |
|
|
250 |
|
|
|
|
| 0% |
Uncovered Elements: 18 (18) |
Complexity: 4 |
Complexity Density: 0.33 |
|
251 |
0 |
@Override... |
252 |
|
public String toString() |
253 |
|
{ |
254 |
0 |
StringBuilder sb = new StringBuilder(); |
255 |
0 |
boolean first = true; |
256 |
0 |
boolean multiple = matchConditions.size() > 1; |
257 |
0 |
for (FeatureMatcherI matcher : matchConditions) |
258 |
|
{ |
259 |
0 |
if (!first) |
260 |
|
{ |
261 |
0 |
String joiner = andConditions ? AND_18N : OR_I18N; |
262 |
0 |
sb.append(SPACE).append(joiner.toLowerCase(Locale.ROOT)) |
263 |
|
.append(SPACE); |
264 |
|
} |
265 |
0 |
first = false; |
266 |
0 |
if (multiple) |
267 |
|
{ |
268 |
0 |
sb.append(OPEN_BRACKET).append(matcher.toString()) |
269 |
|
.append(CLOSE_BRACKET); |
270 |
|
} |
271 |
|
else |
272 |
|
{ |
273 |
0 |
sb.append(matcher.toString()); |
274 |
|
} |
275 |
|
} |
276 |
0 |
return sb.toString(); |
277 |
|
} |
278 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
279 |
21 |
@Override... |
280 |
|
public boolean isEmpty() |
281 |
|
{ |
282 |
21 |
return matchConditions == null || matchConditions.isEmpty(); |
283 |
|
} |
284 |
|
|
285 |
|
|
286 |
|
@inheritDoc |
287 |
|
|
288 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (18) |
Complexity: 4 |
Complexity Density: 0.33 |
|
289 |
3 |
@Override... |
290 |
|
public String toStableString() |
291 |
|
{ |
292 |
3 |
StringBuilder sb = new StringBuilder(); |
293 |
3 |
boolean moreThanOne = matchConditions.size() > 1; |
294 |
3 |
boolean first = true; |
295 |
|
|
296 |
3 |
for (FeatureMatcherI matcher : matchConditions) |
297 |
|
{ |
298 |
5 |
if (!first) |
299 |
|
{ |
300 |
2 |
String joiner = andConditions ? AND : OR; |
301 |
2 |
sb.append(SPACE).append(joiner).append(SPACE); |
302 |
|
} |
303 |
5 |
first = false; |
304 |
5 |
if (moreThanOne) |
305 |
|
{ |
306 |
4 |
sb.append(OPEN_BRACKET).append(matcher.toStableString()) |
307 |
|
.append(CLOSE_BRACKET); |
308 |
|
} |
309 |
|
else |
310 |
|
{ |
311 |
1 |
sb.append(matcher.toStableString()); |
312 |
|
} |
313 |
|
} |
314 |
3 |
return sb.toString(); |
315 |
|
} |
316 |
|
|
317 |
|
} |