1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.extension.version.internal; |
21 |
|
|
22 |
|
import java.io.IOException; |
23 |
|
import java.io.ObjectInputStream; |
24 |
|
import java.io.ObjectOutputStream; |
25 |
|
import java.text.MessageFormat; |
26 |
|
import java.util.Objects; |
27 |
|
|
28 |
|
import org.apache.commons.lang3.StringUtils; |
29 |
|
import org.apache.commons.lang3.builder.HashCodeBuilder; |
30 |
|
import org.xwiki.extension.version.InvalidVersionRangeException; |
31 |
|
import org.xwiki.extension.version.Version; |
32 |
|
import org.xwiki.extension.version.VersionRange; |
33 |
|
|
34 |
|
|
35 |
|
@link |
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
@link |
43 |
|
|
44 |
|
@see |
45 |
|
@version |
46 |
|
@since |
47 |
|
|
|
|
| 81.5% |
Uncovered Elements: 44 (238) |
Complexity: 76 |
Complexity Density: 0.62 |
|
48 |
|
public class DefaultVersionRange implements VersionRange |
49 |
|
{ |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
private static final long serialVersionUID = 1L; |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
private static final char RANGE_SEPARATOR = ','; |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
private Version lowerBound; |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
private boolean lowerBoundInclusive; |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
private Version upperBound; |
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
private boolean upperBoundInclusive; |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
private String value; |
84 |
|
|
85 |
|
|
86 |
|
@param |
87 |
|
@throws |
88 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
89 |
70 |
public DefaultVersionRange(String rawRange) throws InvalidVersionRangeException... |
90 |
|
{ |
91 |
70 |
setRange(rawRange); |
92 |
|
} |
93 |
|
|
94 |
|
|
95 |
|
@param |
96 |
|
@param |
97 |
|
@param |
98 |
|
@param |
99 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
100 |
9 |
public DefaultVersionRange(Version lowerBound, boolean lowerBoundInclusive, Version upperBound,... |
101 |
|
boolean upperBoundInclusive) |
102 |
|
{ |
103 |
9 |
this.lowerBound = lowerBound; |
104 |
9 |
this.lowerBoundInclusive = lowerBoundInclusive; |
105 |
9 |
this.upperBound = upperBound; |
106 |
9 |
this.upperBoundInclusive = upperBoundInclusive; |
107 |
|
} |
108 |
|
|
109 |
|
|
110 |
|
@param |
111 |
|
@throws |
112 |
|
|
|
|
| 94.1% |
Uncovered Elements: 2 (34) |
Complexity: 10 |
Complexity Density: 0.5 |
|
113 |
70 |
private void setRange(String rawRange) throws InvalidVersionRangeException... |
114 |
|
{ |
115 |
70 |
this.value = rawRange; |
116 |
|
|
117 |
|
|
118 |
|
|
119 |
70 |
String range = this.value; |
120 |
|
|
121 |
70 |
this.lowerBoundInclusive = findLowerBoundInclusive(range); |
122 |
69 |
this.upperBoundInclusive = findUpperBoundInclusive(range); |
123 |
|
|
124 |
68 |
range = range.substring(1, range.length() - 1); |
125 |
|
|
126 |
68 |
int index = range.indexOf(RANGE_SEPARATOR); |
127 |
|
|
128 |
68 |
if (index < 0) { |
129 |
23 |
if (!this.lowerBoundInclusive || !this.upperBoundInclusive) { |
130 |
0 |
throw new InvalidVersionRangeException(MessageFormat.format( |
131 |
|
"Invalid version range [{0}], single version must be surrounded by []", rawRange)); |
132 |
|
} |
133 |
|
|
134 |
23 |
this.upperBound = new DefaultVersion(range.trim()); |
135 |
23 |
this.lowerBound = this.upperBound; |
136 |
|
} else { |
137 |
45 |
String parsedLowerBound = range.substring(0, index).trim(); |
138 |
45 |
String parsedUpperBound = range.substring(index + 1).trim(); |
139 |
|
|
140 |
|
|
141 |
45 |
if (StringUtils.contains(parsedUpperBound, RANGE_SEPARATOR)) { |
142 |
1 |
throw new InvalidVersionRangeException(MessageFormat.format( |
143 |
|
"Invalid version range [{0}], bounds may not contain additional ','", rawRange)); |
144 |
|
} |
145 |
|
|
146 |
44 |
this.lowerBound = parsedLowerBound.length() > 0 ? new DefaultVersion(parsedLowerBound) : null; |
147 |
44 |
this.upperBound = parsedUpperBound.length() > 0 ? new DefaultVersion(parsedUpperBound) : null; |
148 |
|
|
149 |
44 |
if (this.upperBound != null && this.lowerBound != null) { |
150 |
26 |
if (this.upperBound.compareTo(this.lowerBound) < 0) { |
151 |
1 |
throw new InvalidVersionRangeException(MessageFormat.format( |
152 |
|
"Invalid version range [{0}], lower bound must not be greater than upper bound", rawRange)); |
153 |
|
} |
154 |
|
} |
155 |
|
} |
156 |
|
} |
157 |
|
|
158 |
|
|
159 |
|
@param |
160 |
|
@return |
161 |
|
@throws |
162 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
163 |
70 |
private boolean findLowerBoundInclusive(String range) throws InvalidVersionRangeException... |
164 |
|
{ |
165 |
70 |
if (VersionUtils.startsWith(range, '[')) { |
166 |
59 |
return true; |
167 |
11 |
} else if (VersionUtils.startsWith(range, '(')) { |
168 |
10 |
return false; |
169 |
|
} else { |
170 |
1 |
throw new InvalidVersionRangeException(MessageFormat.format( |
171 |
|
"Invalid version range [{0}], a range must start with either [ or (", range)); |
172 |
|
} |
173 |
|
} |
174 |
|
|
175 |
|
|
176 |
|
@param |
177 |
|
@return |
178 |
|
@throws |
179 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
180 |
69 |
private boolean findUpperBoundInclusive(String range) throws InvalidVersionRangeException... |
181 |
|
{ |
182 |
69 |
if (VersionUtils.endsWith(range, ']')) { |
183 |
45 |
return true; |
184 |
24 |
} else if (VersionUtils.endsWith(range, ')')) { |
185 |
23 |
return false; |
186 |
|
} else { |
187 |
1 |
throw new InvalidVersionRangeException(MessageFormat.format( |
188 |
|
"Invalid version range [{0}], a range must end with either [ or (", range)); |
189 |
|
} |
190 |
|
} |
191 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
192 |
26 |
@Override... |
193 |
|
public boolean containsVersion(Version version) |
194 |
|
{ |
195 |
26 |
if (version instanceof DefaultVersion) { |
196 |
26 |
return containsVersion((DefaultVersion) version); |
197 |
|
} else { |
198 |
0 |
return containsVersion(new DefaultVersion(version.getValue())); |
199 |
|
} |
200 |
|
} |
201 |
|
|
202 |
|
|
203 |
|
@param |
204 |
|
@return |
205 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (25) |
Complexity: 9 |
Complexity Density: 0.69 |
|
206 |
32 |
public boolean containsVersion(DefaultVersion version)... |
207 |
|
{ |
208 |
32 |
if (this.lowerBound != null) { |
209 |
26 |
int comparison = this.lowerBound.compareTo(version); |
210 |
|
|
211 |
26 |
if (comparison == 0 && !this.lowerBoundInclusive) { |
212 |
1 |
return false; |
213 |
|
} |
214 |
25 |
if (comparison > 0) { |
215 |
3 |
return false; |
216 |
|
} |
217 |
|
} |
218 |
|
|
219 |
28 |
if (this.upperBound != null) { |
220 |
14 |
int comparison = this.upperBound.compareTo(version); |
221 |
|
|
222 |
14 |
if (comparison == 0 && !this.upperBoundInclusive) { |
223 |
3 |
return false; |
224 |
|
} |
225 |
11 |
if (comparison < 0) { |
226 |
5 |
return false; |
227 |
|
} |
228 |
|
} |
229 |
|
|
230 |
20 |
return true; |
231 |
|
} |
232 |
|
|
|
|
| 90.5% |
Uncovered Elements: 2 (21) |
Complexity: 6 |
Complexity Density: 0.55 |
|
233 |
9 |
@Override... |
234 |
|
public String getValue() |
235 |
|
{ |
236 |
9 |
if (this.value == null) { |
237 |
9 |
StringBuilder buffer = new StringBuilder(); |
238 |
|
|
239 |
9 |
buffer.append(this.lowerBoundInclusive ? '[' : '('); |
240 |
9 |
if (this.lowerBound != null) { |
241 |
9 |
buffer.append(this.lowerBound); |
242 |
|
} |
243 |
9 |
buffer.append(RANGE_SEPARATOR); |
244 |
9 |
if (this.upperBound != null) { |
245 |
5 |
buffer.append(this.upperBound); |
246 |
|
} |
247 |
9 |
buffer.append(this.upperBoundInclusive ? ']' : ')'); |
248 |
|
|
249 |
9 |
this.value = buffer.toString(); |
250 |
|
} |
251 |
|
|
252 |
9 |
return this.value; |
253 |
|
} |
254 |
|
|
|
|
| 53.8% |
Uncovered Elements: 6 (13) |
Complexity: 4 |
Complexity Density: 0.44 |
|
255 |
2 |
@Override... |
256 |
|
public boolean isCompatible(VersionRange otherRange) |
257 |
|
{ |
258 |
2 |
boolean compatible; |
259 |
|
|
260 |
2 |
if (equals(otherRange)) { |
261 |
0 |
compatible = true; |
262 |
|
} else { |
263 |
2 |
if (otherRange instanceof DefaultVersionRange) { |
264 |
2 |
compatible = isCompatible((DefaultVersionRange) otherRange); |
265 |
|
} else { |
266 |
0 |
try { |
267 |
0 |
compatible = isCompatible(new DefaultVersionRange(otherRange.getValue())); |
268 |
|
} catch (InvalidVersionRangeException e) { |
269 |
0 |
compatible = false; |
270 |
|
} |
271 |
|
} |
272 |
|
} |
273 |
|
|
274 |
2 |
return compatible; |
275 |
|
} |
276 |
|
|
277 |
|
|
278 |
|
|
279 |
|
|
280 |
|
@param |
281 |
|
@return |
282 |
|
|
|
|
| 76.5% |
Uncovered Elements: 4 (17) |
Complexity: 8 |
Complexity Density: 0.89 |
|
283 |
12 |
public boolean isCompatible(DefaultVersionRange otherRange)... |
284 |
|
{ |
285 |
12 |
int lowerCompare = |
286 |
|
compareTo(this.lowerBound, this.lowerBoundInclusive, otherRange.lowerBound, otherRange.lowerBoundInclusive, |
287 |
|
false); |
288 |
12 |
int upperCompare = |
289 |
|
compareTo(this.upperBound, this.upperBoundInclusive, otherRange.upperBound, otherRange.upperBoundInclusive, |
290 |
|
true); |
291 |
|
|
292 |
|
|
293 |
12 |
if (lowerCompare == 0 || upperCompare == 0) { |
294 |
4 |
return true; |
295 |
|
} |
296 |
|
|
297 |
|
|
298 |
8 |
if (lowerCompare > 0 && upperCompare < 0) { |
299 |
0 |
return true; |
300 |
|
} |
301 |
|
|
302 |
|
|
303 |
8 |
if (lowerCompare < 0 && upperCompare > 0) { |
304 |
0 |
return true; |
305 |
|
} |
306 |
|
|
307 |
|
|
308 |
8 |
return lowerCompare < 0 ? isCompatible(this.upperBound, this.upperBoundInclusive, otherRange.lowerBound, |
309 |
|
otherRange.lowerBoundInclusive) : isCompatible(otherRange.upperBound, otherRange.upperBoundInclusive, |
310 |
|
this.lowerBound, this.lowerBoundInclusive); |
311 |
|
} |
312 |
|
|
313 |
|
|
314 |
|
@param |
315 |
|
@param |
316 |
|
@param |
317 |
|
@param |
318 |
|
@param |
319 |
|
@return |
320 |
|
|
321 |
|
|
|
|
| 82.4% |
Uncovered Elements: 3 (17) |
Complexity: 6 |
Complexity Density: 0.86 |
|
322 |
24 |
private int compareTo(Version version1, boolean included1, Version version2, boolean included2, boolean upper)... |
323 |
|
{ |
324 |
24 |
int compare; |
325 |
|
|
326 |
24 |
if (version1 == null) { |
327 |
4 |
compare = version2 == null ? 0 : (upper ? 1 : -1); |
328 |
|
} else { |
329 |
20 |
if (version2 == null) { |
330 |
4 |
compare = upper ? -1 : 1; |
331 |
|
} else { |
332 |
16 |
compare = compareNotNull(version1, included1, version2, included2, upper); |
333 |
|
} |
334 |
|
} |
335 |
|
|
336 |
24 |
return compare; |
337 |
|
} |
338 |
|
|
339 |
|
|
340 |
|
@param |
341 |
|
@param |
342 |
|
@param |
343 |
|
@param |
344 |
|
@param |
345 |
|
@return |
346 |
|
|
347 |
|
|
|
|
| 46.7% |
Uncovered Elements: 8 (15) |
Complexity: 6 |
Complexity Density: 1.2 |
|
348 |
16 |
private int compareNotNull(Version version1, boolean included1, Version version2, boolean included2, boolean upper)... |
349 |
|
{ |
350 |
16 |
int compare = version1.compareTo(version2); |
351 |
|
|
352 |
16 |
if (compare == 0) { |
353 |
5 |
if (included1 != included2) { |
354 |
0 |
compare = included1 ? (upper ? -1 : 1) : (upper ? 1 : -1); |
355 |
|
} |
356 |
|
} |
357 |
|
|
358 |
16 |
return compare; |
359 |
|
} |
360 |
|
|
361 |
|
|
362 |
|
@param |
363 |
|
@param |
364 |
|
@param |
365 |
|
@param |
366 |
|
@return |
367 |
|
|
|
|
| 88.9% |
Uncovered Elements: 2 (18) |
Complexity: 5 |
Complexity Density: 0.5 |
|
368 |
8 |
private boolean isCompatible(Version upper, boolean upperInclusive, Version lower, boolean lowerInclusive)... |
369 |
|
{ |
370 |
8 |
boolean compatible = true; |
371 |
|
|
372 |
8 |
if (upper != null) { |
373 |
8 |
if (lower != null) { |
374 |
8 |
int comparison = upper.compareTo(lower); |
375 |
|
|
376 |
8 |
if (comparison > 0) { |
377 |
2 |
compatible = true; |
378 |
6 |
} else if (comparison < 0) { |
379 |
2 |
compatible = false; |
380 |
|
} else { |
381 |
4 |
compatible = upperInclusive && lowerInclusive; |
382 |
|
} |
383 |
|
} |
384 |
|
} |
385 |
|
|
386 |
8 |
return compatible; |
387 |
|
} |
388 |
|
|
389 |
|
|
390 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
391 |
1 |
@Override... |
392 |
|
public String toString() |
393 |
|
{ |
394 |
1 |
return getValue(); |
395 |
|
} |
396 |
|
|
|
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
397 |
7 |
@Override... |
398 |
|
public int hashCode() |
399 |
|
{ |
400 |
7 |
HashCodeBuilder builder = new HashCodeBuilder(17, 31); |
401 |
|
|
402 |
7 |
builder.append(this.upperBound); |
403 |
7 |
builder.append(this.upperBoundInclusive ? 1 : 0); |
404 |
7 |
builder.append(this.lowerBound); |
405 |
7 |
builder.append(this.lowerBoundInclusive ? 1 : 0); |
406 |
|
|
407 |
7 |
return builder.toHashCode(); |
408 |
|
} |
409 |
|
|
|
|
| 41.2% |
Uncovered Elements: 10 (17) |
Complexity: 5 |
Complexity Density: 0.45 |
|
410 |
14 |
@Override... |
411 |
|
public boolean equals(Object obj) |
412 |
|
{ |
413 |
14 |
if (obj == this) { |
414 |
0 |
return true; |
415 |
|
} |
416 |
|
|
417 |
14 |
boolean equals; |
418 |
|
|
419 |
14 |
if (obj instanceof DefaultVersionRange) { |
420 |
14 |
equals = equals((DefaultVersionRange) obj); |
421 |
0 |
} else if (obj instanceof VersionRange) { |
422 |
0 |
try { |
423 |
0 |
equals = equals(new DefaultVersionRange(((VersionRange) obj).getValue())); |
424 |
|
} catch (InvalidVersionRangeException e) { |
425 |
0 |
equals = false; |
426 |
|
} |
427 |
|
} else { |
428 |
0 |
equals = false; |
429 |
|
} |
430 |
|
|
431 |
14 |
return equals; |
432 |
|
} |
433 |
|
|
434 |
|
|
435 |
|
@param |
436 |
|
@return |
437 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
438 |
14 |
public boolean equals(DefaultVersionRange version)... |
439 |
|
{ |
440 |
14 |
return this.upperBoundInclusive == version.upperBoundInclusive |
441 |
|
&& this.lowerBoundInclusive == version.lowerBoundInclusive |
442 |
|
&& Objects.equals(this.upperBound, version.upperBound) |
443 |
|
&& Objects.equals(this.lowerBound, version.lowerBound); |
444 |
|
} |
445 |
|
|
446 |
|
|
447 |
|
|
448 |
|
|
449 |
|
@param |
450 |
|
@throws |
451 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
452 |
0 |
private void writeObject(ObjectOutputStream out) throws IOException... |
453 |
|
{ |
454 |
0 |
out.writeObject(getValue()); |
455 |
|
} |
456 |
|
|
457 |
|
|
458 |
|
@param |
459 |
|
@throws |
460 |
|
@throws |
461 |
|
@throws |
462 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
463 |
0 |
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException,... |
464 |
|
InvalidVersionRangeException |
465 |
|
{ |
466 |
0 |
setRange((String) in.readObject()); |
467 |
|
} |
468 |
|
} |