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.util.ArrayList; |
26 |
|
import java.util.Collection; |
27 |
|
import java.util.Collections; |
28 |
|
import java.util.List; |
29 |
|
import java.util.Objects; |
30 |
|
|
31 |
|
import org.apache.commons.lang3.builder.HashCodeBuilder; |
32 |
|
import org.xwiki.extension.version.IncompatibleVersionConstraintException; |
33 |
|
import org.xwiki.extension.version.InvalidVersionConstraintException; |
34 |
|
import org.xwiki.extension.version.InvalidVersionRangeException; |
35 |
|
import org.xwiki.extension.version.Version; |
36 |
|
import org.xwiki.extension.version.VersionConstraint; |
37 |
|
import org.xwiki.extension.version.VersionRange; |
38 |
|
import org.xwiki.extension.version.VersionRangeCollection; |
39 |
|
|
40 |
|
|
41 |
|
@link |
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
@see |
50 |
|
@version |
51 |
|
@since |
52 |
|
|
|
|
| 89.9% |
Uncovered Elements: 18 (179) |
Complexity: 55 |
Complexity Density: 0.52 |
|
53 |
|
public class DefaultVersionConstraint implements VersionConstraint |
54 |
|
{ |
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
private static final long serialVersionUID = 1L; |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
private static final char RANGE_SEPARATOR = ','; |
64 |
|
|
65 |
|
|
66 |
|
@see |
67 |
|
|
68 |
|
private List<VersionRangeCollection> ranges; |
69 |
|
|
70 |
|
|
71 |
|
@see |
72 |
|
|
73 |
|
private Version version; |
74 |
|
|
75 |
|
|
76 |
|
@see |
77 |
|
|
78 |
|
private String value; |
79 |
|
|
80 |
|
private int hashCode = -1; |
81 |
|
|
82 |
|
|
83 |
|
@param |
84 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
85 |
734855 |
public DefaultVersionConstraint(String rawConstraint)... |
86 |
|
{ |
87 |
734855 |
this.value = rawConstraint; |
88 |
|
} |
89 |
|
|
90 |
|
|
91 |
|
@link |
92 |
|
|
93 |
|
@param |
94 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
95 |
0 |
public DefaultVersionConstraint(VersionConstraint versionConstraint)... |
96 |
|
{ |
97 |
0 |
this(versionConstraint.getRanges(), versionConstraint.getVersion()); |
98 |
|
} |
99 |
|
|
100 |
|
|
101 |
|
@param |
102 |
|
@param |
103 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
104 |
2 |
public DefaultVersionConstraint(Collection<? extends VersionRangeCollection> ranges, Version version)... |
105 |
|
{ |
106 |
2 |
if (ranges != null && !ranges.isEmpty()) { |
107 |
1 |
this.ranges = new ArrayList<>(ranges); |
108 |
|
} else { |
109 |
1 |
this.ranges = Collections.emptyList(); |
110 |
|
} |
111 |
2 |
this.version = version; |
112 |
|
} |
113 |
|
|
114 |
|
|
115 |
|
@param |
116 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
117 |
0 |
public DefaultVersionConstraint(Version version)... |
118 |
|
{ |
119 |
0 |
this.ranges = Collections.emptyList(); |
120 |
0 |
this.version = version; |
121 |
|
} |
122 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 6 |
Complexity Density: 0.75 |
|
123 |
1471779 |
private void init()... |
124 |
|
{ |
125 |
1471779 |
if (this.ranges == null && this.value != null) { |
126 |
|
|
127 |
|
|
128 |
734764 |
List<VersionRangeCollection> newRanges = null; |
129 |
734764 |
try { |
130 |
734764 |
newRanges = parseRanges(this.value); |
131 |
|
} catch (InvalidVersionConstraintException e) { |
132 |
|
|
133 |
|
} |
134 |
|
|
135 |
|
|
136 |
|
|
137 |
734764 |
if (newRanges == null || newRanges.isEmpty()) { |
138 |
734744 |
this.version = new DefaultVersion(this.value); |
139 |
734744 |
this.ranges = Collections.emptyList(); |
140 |
|
} else { |
141 |
20 |
this.ranges = newRanges; |
142 |
|
} |
143 |
|
} |
144 |
|
} |
145 |
|
|
146 |
|
|
147 |
|
@param |
148 |
|
@return |
149 |
|
@throws |
150 |
|
|
|
|
| 83.3% |
Uncovered Elements: 5 (30) |
Complexity: 8 |
Complexity Density: 0.4 |
|
151 |
734764 |
private List<VersionRangeCollection> parseRanges(String rawConstraint) throws InvalidVersionConstraintException... |
152 |
|
{ |
153 |
734764 |
String constraint = rawConstraint; |
154 |
|
|
155 |
734764 |
List<VersionRangeCollection> newRanges = new ArrayList<>(); |
156 |
|
|
157 |
734767 |
while (VersionUtils.startsWith(constraint, '{')) { |
158 |
3 |
int index = constraint.indexOf('}'); |
159 |
|
|
160 |
3 |
if (index < 0) { |
161 |
0 |
throw new InvalidVersionConstraintException( |
162 |
|
String.format("Unbounded version range [{%s}]", rawConstraint)); |
163 |
|
} |
164 |
|
|
165 |
3 |
String range = constraint.substring(1, index); |
166 |
3 |
try { |
167 |
3 |
newRanges.add(new DefaultVersionRangeCollection(range)); |
168 |
|
} catch (InvalidVersionRangeException e) { |
169 |
0 |
throw new InvalidVersionConstraintException( |
170 |
|
String.format("Failed to parse version range [%s] in constraint [%s]", range, rawConstraint), e); |
171 |
|
} |
172 |
|
|
173 |
3 |
constraint = constraint.substring(index + 1).trim(); |
174 |
|
|
175 |
3 |
if (VersionUtils.startsWith(constraint, RANGE_SEPARATOR)) { |
176 |
1 |
constraint = constraint.substring(1).trim(); |
177 |
|
} |
178 |
|
} |
179 |
|
|
180 |
734764 |
if (!constraint.isEmpty()) { |
181 |
734762 |
if (newRanges.isEmpty()) { |
182 |
734762 |
try { |
183 |
734762 |
newRanges.add(new DefaultVersionRangeCollection(constraint)); |
184 |
|
} catch (InvalidVersionRangeException e) { |
185 |
734744 |
throw new InvalidVersionConstraintException( |
186 |
|
String.format("Failed to parse version range [{%s}]", constraint), e); |
187 |
|
} |
188 |
|
} else { |
189 |
0 |
throw new InvalidVersionConstraintException(String |
190 |
|
.format("Invalid version range [{%s}], expected [ or ( but got [{%s}]", rawConstraint, constraint)); |
191 |
|
} |
192 |
|
} |
193 |
|
|
194 |
20 |
return newRanges; |
195 |
|
} |
196 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
197 |
735023 |
private List<VersionRangeCollection> getRangesInternal()... |
198 |
|
{ |
199 |
735023 |
init(); |
200 |
|
|
201 |
735023 |
return this.ranges; |
202 |
|
} |
203 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
204 |
125 |
@Override... |
205 |
|
public Collection<VersionRangeCollection> getRanges() |
206 |
|
{ |
207 |
125 |
return getRangesInternal(); |
208 |
|
} |
209 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
210 |
736718 |
@Override... |
211 |
|
public Version getVersion() |
212 |
|
{ |
213 |
736718 |
init(); |
214 |
|
|
215 |
736718 |
return this.version; |
216 |
|
} |
217 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
218 |
115 |
@Override... |
219 |
|
public boolean containsVersion(Version version) |
220 |
|
{ |
221 |
115 |
if (getRangesInternal().isEmpty()) { |
222 |
93 |
return getVersion() != null && getVersion().equals(version); |
223 |
|
} else { |
224 |
22 |
for (VersionRange range : getRangesInternal()) { |
225 |
22 |
if (!range.containsVersion(version)) { |
226 |
4 |
return false; |
227 |
|
} |
228 |
|
} |
229 |
|
} |
230 |
|
|
231 |
18 |
return true; |
232 |
|
} |
233 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
234 |
74 |
@Override... |
235 |
|
public boolean isCompatible(Version version) |
236 |
|
{ |
237 |
74 |
boolean compatible; |
238 |
74 |
if (getVersion() == null) { |
239 |
3 |
compatible = containsVersion(version); |
240 |
|
} else { |
241 |
71 |
compatible = version.compareTo(getVersion()) >= 0; |
242 |
|
} |
243 |
|
|
244 |
74 |
return compatible; |
245 |
|
} |
246 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 3 |
Complexity Density: 0.75 |
|
247 |
14 |
@Override... |
248 |
|
public VersionConstraint merge(VersionConstraint versionConstraint) throws IncompatibleVersionConstraintException |
249 |
|
{ |
250 |
14 |
if (equals(versionConstraint)) { |
251 |
1 |
return this; |
252 |
|
} else { |
253 |
13 |
VersionConstraint mergedConstraint = mergeVersions(versionConstraint); |
254 |
|
|
255 |
12 |
return mergedConstraint == null ? mergeRanges(versionConstraint.getRanges()) : mergedConstraint; |
256 |
|
} |
257 |
|
} |
258 |
|
|
259 |
|
|
260 |
|
@param |
261 |
|
@return |
262 |
|
@throws |
263 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
264 |
13 |
private VersionConstraint mergeVersions(VersionConstraint versionConstraint)... |
265 |
|
throws IncompatibleVersionConstraintException |
266 |
|
{ |
267 |
13 |
if (getVersion() != null) { |
268 |
10 |
return mergeVersions(this, versionConstraint); |
269 |
3 |
} else if (versionConstraint.getVersion() != null) { |
270 |
1 |
return mergeVersions(versionConstraint, this); |
271 |
|
} |
272 |
|
|
273 |
2 |
return null; |
274 |
|
} |
275 |
|
|
276 |
|
|
277 |
|
@param |
278 |
|
@param |
279 |
|
@return |
280 |
|
@throws |
281 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 4 |
Complexity Density: 0.8 |
|
282 |
11 |
private VersionConstraint mergeVersions(VersionConstraint versionConstraintWithVersion,... |
283 |
|
VersionConstraint versionConstraint) throws IncompatibleVersionConstraintException |
284 |
|
{ |
285 |
11 |
if (versionConstraint.getVersion() != null) { |
286 |
9 |
return versionConstraintWithVersion.getVersion().compareTo(versionConstraint.getVersion()) >= 0 |
287 |
|
? versionConstraintWithVersion : versionConstraint; |
288 |
|
} else { |
289 |
2 |
if (!versionConstraint.containsVersion(versionConstraintWithVersion.getVersion())) { |
290 |
1 |
throw new IncompatibleVersionConstraintException("Version [" + versionConstraintWithVersion.getVersion() |
291 |
|
+ "] is not part of version constraint [" + versionConstraint + "]"); |
292 |
|
} |
293 |
|
|
294 |
1 |
return versionConstraintWithVersion; |
295 |
|
} |
296 |
|
} |
297 |
|
|
298 |
|
|
299 |
|
@link |
300 |
|
|
301 |
|
|
302 |
|
@param |
303 |
|
@return@link |
304 |
|
@throws |
305 |
|
|
306 |
|
|
307 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
308 |
2 |
private DefaultVersionConstraint mergeRanges(Collection<VersionRangeCollection> otherRanges)... |
309 |
|
throws IncompatibleVersionConstraintException |
310 |
|
{ |
311 |
|
|
312 |
2 |
validateCompatibility(otherRanges); |
313 |
|
|
314 |
1 |
Collection<VersionRangeCollection> newRanges = new ArrayList<>(getRangesInternal().size() + otherRanges.size()); |
315 |
1 |
newRanges.addAll(getRangesInternal()); |
316 |
1 |
newRanges.addAll(otherRanges); |
317 |
|
|
318 |
1 |
return new DefaultVersionConstraint(newRanges, null); |
319 |
|
} |
320 |
|
|
321 |
|
|
322 |
|
@param |
323 |
|
@throws |
324 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
325 |
2 |
private void validateCompatibility(Collection<VersionRangeCollection> otherRanges)... |
326 |
|
throws IncompatibleVersionConstraintException |
327 |
|
{ |
328 |
2 |
for (VersionRange otherRange : otherRanges) { |
329 |
2 |
for (VersionRange range : getRangesInternal()) { |
330 |
2 |
if (!range.isCompatible(otherRange)) { |
331 |
1 |
throw new IncompatibleVersionConstraintException( |
332 |
|
"Ranges [" + range + "] and [" + otherRange + "] are incompatibles"); |
333 |
|
} |
334 |
|
} |
335 |
|
} |
336 |
|
} |
337 |
|
|
|
|
| 90.9% |
Uncovered Elements: 2 (22) |
Complexity: 5 |
Complexity Density: 0.36 |
|
338 |
694217 |
@Override... |
339 |
|
public String getValue() |
340 |
|
{ |
341 |
694217 |
if (this.value == null) { |
342 |
2 |
StringBuilder builder = new StringBuilder(); |
343 |
|
|
344 |
2 |
if (getVersion() != null) { |
345 |
1 |
builder.append(getVersion()); |
346 |
|
} else { |
347 |
1 |
if (getRangesInternal().size() == 1) { |
348 |
0 |
builder.append(getRangesInternal().get(0).getValue()); |
349 |
|
} else { |
350 |
1 |
for (VersionRange range : getRangesInternal()) { |
351 |
2 |
if (builder.length() > 0) { |
352 |
1 |
builder.append(RANGE_SEPARATOR); |
353 |
|
} |
354 |
2 |
builder.append('{'); |
355 |
2 |
builder.append(range.getValue()); |
356 |
2 |
builder.append('}'); |
357 |
|
} |
358 |
|
} |
359 |
|
} |
360 |
|
|
361 |
2 |
this.value = builder.toString(); |
362 |
|
} |
363 |
|
|
364 |
694217 |
return this.value; |
365 |
|
} |
366 |
|
|
367 |
|
|
368 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
369 |
645 |
@Override... |
370 |
|
public String toString() |
371 |
|
{ |
372 |
645 |
return getValue(); |
373 |
|
} |
374 |
|
|
|
|
| 81.8% |
Uncovered Elements: 2 (11) |
Complexity: 4 |
Complexity Density: 0.57 |
|
375 |
39 |
@Override... |
376 |
|
public boolean equals(Object obj) |
377 |
|
{ |
378 |
39 |
if (this == obj) { |
379 |
1 |
return true; |
380 |
|
} |
381 |
|
|
382 |
38 |
if (obj == null || !(obj instanceof VersionConstraint)) { |
383 |
0 |
return false; |
384 |
|
} |
385 |
|
|
386 |
38 |
init(); |
387 |
|
|
388 |
38 |
VersionConstraint versionConstraint = (VersionConstraint) obj; |
389 |
|
|
390 |
38 |
return getRangesInternal().equals(versionConstraint.getRanges()) |
391 |
|
&& Objects.equals(getVersion(), versionConstraint.getVersion()); |
392 |
|
} |
393 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
394 |
2085481 |
@Override... |
395 |
|
public int hashCode() |
396 |
|
{ |
397 |
2085481 |
if (this.hashCode == -1) { |
398 |
734717 |
HashCodeBuilder builder = new HashCodeBuilder(17, 31); |
399 |
734717 |
builder.append(getRangesInternal()); |
400 |
734717 |
builder.append(getVersion()); |
401 |
|
|
402 |
734717 |
this.hashCode = builder.toHashCode(); |
403 |
|
} |
404 |
|
|
405 |
2085481 |
return this.hashCode; |
406 |
|
} |
407 |
|
|
408 |
|
|
409 |
|
|
410 |
|
|
411 |
|
@param |
412 |
|
@throws |
413 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
414 |
0 |
private void writeObject(ObjectOutputStream out) throws IOException... |
415 |
|
{ |
416 |
0 |
out.writeObject(getValue()); |
417 |
|
} |
418 |
|
|
419 |
|
|
420 |
|
@param |
421 |
|
@throws |
422 |
|
@throws |
423 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
424 |
0 |
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException... |
425 |
|
{ |
426 |
0 |
this.value = (String) in.readObject(); |
427 |
|
} |
428 |
|
} |