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