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.List; |
28 |
|
|
29 |
|
import org.apache.commons.lang3.StringUtils; |
30 |
|
import org.xwiki.extension.version.InvalidVersionRangeException; |
31 |
|
import org.xwiki.extension.version.Version; |
32 |
|
import org.xwiki.extension.version.VersionRange; |
33 |
|
import org.xwiki.extension.version.VersionRangeCollection; |
34 |
|
|
35 |
|
|
36 |
|
@link |
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
@see |
43 |
|
@version |
44 |
|
@since |
45 |
|
|
|
|
| 77.2% |
Uncovered Elements: 23 (101) |
Complexity: 33 |
Complexity Density: 0.56 |
|
46 |
|
public class DefaultVersionRangeCollection implements VersionRangeCollection |
47 |
|
{ |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
private static final long serialVersionUID = 1L; |
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
private static final char RANGE_SEPARATOR = ','; |
57 |
|
|
58 |
|
|
59 |
|
@see |
60 |
|
|
61 |
|
private List<VersionRange> ranges = new ArrayList<VersionRange>(); |
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
private String value; |
67 |
|
|
68 |
|
|
69 |
|
@param |
70 |
|
@throws |
71 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
72 |
734769 |
public DefaultVersionRangeCollection(String rawRanges) throws InvalidVersionRangeException... |
73 |
|
{ |
74 |
734769 |
setRanges(rawRanges); |
75 |
|
} |
76 |
|
|
77 |
|
|
78 |
|
@param |
79 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
80 |
0 |
public DefaultVersionRangeCollection(Collection<? extends VersionRange> ranges)... |
81 |
|
{ |
82 |
0 |
for (VersionRange range : ranges) { |
83 |
0 |
this.ranges.add(range); |
84 |
|
} |
85 |
|
} |
86 |
|
|
87 |
|
|
88 |
|
@param |
89 |
|
@throws |
90 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
91 |
734769 |
private void setRanges(String rawRanges) throws InvalidVersionRangeException... |
92 |
|
{ |
93 |
734769 |
this.value = rawRanges; |
94 |
|
|
95 |
|
|
96 |
|
|
97 |
734769 |
if (StringUtils.isEmpty(rawRanges)) { |
98 |
0 |
throw new InvalidVersionRangeException("Range can't be empty"); |
99 |
|
} |
100 |
|
|
101 |
734769 |
parseRanges(this.value); |
102 |
|
} |
103 |
|
|
104 |
|
|
105 |
|
@param |
106 |
|
@throws |
107 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (24) |
Complexity: 9 |
Complexity Density: 0.64 |
|
108 |
734769 |
private void parseRanges(String rawRanges) throws InvalidVersionRangeException... |
109 |
|
{ |
110 |
734769 |
String currentRanges = rawRanges; |
111 |
|
|
112 |
734797 |
while (VersionUtils.startsWith(currentRanges, '[') || VersionUtils.startsWith(currentRanges, '(')) { |
113 |
29 |
int index1 = currentRanges.indexOf(')'); |
114 |
29 |
int index2 = currentRanges.indexOf(']'); |
115 |
|
|
116 |
29 |
int index = index2; |
117 |
29 |
if (index2 < 0 || (index1 >= 0 && index1 < index2)) { |
118 |
13 |
index = index1; |
119 |
|
} |
120 |
|
|
121 |
29 |
if (index < 0) { |
122 |
1 |
throw new InvalidVersionRangeException("Unbounded version range [" + rawRanges + "]"); |
123 |
|
} |
124 |
|
|
125 |
28 |
currentRanges = parseRange(currentRanges, index, rawRanges); |
126 |
|
|
127 |
28 |
if (VersionUtils.startsWith(currentRanges, RANGE_SEPARATOR)) { |
128 |
3 |
currentRanges = currentRanges.substring(1).trim(); |
129 |
|
} |
130 |
|
} |
131 |
|
|
132 |
734768 |
if (!currentRanges.isEmpty()) { |
133 |
734743 |
throw new InvalidVersionRangeException(String.format( |
134 |
|
"Invalid version range [%s], expected [ or ( but got [%s]", rawRanges, currentRanges)); |
135 |
|
} |
136 |
|
} |
137 |
|
|
138 |
|
|
139 |
|
@param |
140 |
|
@param |
141 |
|
@param |
142 |
|
@return |
143 |
|
@throws |
144 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
145 |
28 |
private String parseRange(String currentRanges, int index, String rawRanges) throws InvalidVersionRangeException... |
146 |
|
{ |
147 |
28 |
String range = currentRanges.substring(0, index + 1); |
148 |
28 |
try { |
149 |
28 |
this.ranges.add(new DefaultVersionRange(range)); |
150 |
|
} catch (InvalidVersionRangeException e) { |
151 |
0 |
throw new InvalidVersionRangeException(String.format( |
152 |
|
"Failed to parse version range [%s] in constraint [%s]", range, rawRanges), e); |
153 |
|
} |
154 |
|
|
155 |
28 |
return currentRanges.substring(index + 1).trim(); |
156 |
|
} |
157 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
158 |
30 |
@Override... |
159 |
|
public Collection<VersionRange> getRanges() |
160 |
|
{ |
161 |
30 |
return this.ranges; |
162 |
|
} |
163 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
164 |
22 |
@Override... |
165 |
|
public boolean containsVersion(Version version) |
166 |
|
{ |
167 |
22 |
for (VersionRange range : getRanges()) { |
168 |
26 |
if (range.containsVersion(version)) { |
169 |
18 |
return true; |
170 |
|
} |
171 |
|
} |
172 |
|
|
173 |
4 |
return false; |
174 |
|
} |
175 |
|
|
|
|
| 25% |
Uncovered Elements: 9 (12) |
Complexity: 3 |
Complexity Density: 0.38 |
|
176 |
5 |
@Override... |
177 |
|
public String getValue() |
178 |
|
{ |
179 |
5 |
if (this.value == null) { |
180 |
0 |
StringBuilder buffer = new StringBuilder(); |
181 |
|
|
182 |
0 |
for (VersionRange range : this.ranges) { |
183 |
0 |
if (buffer.length() > 0) { |
184 |
0 |
buffer.append(RANGE_SEPARATOR); |
185 |
|
} |
186 |
0 |
buffer.append(range); |
187 |
|
} |
188 |
|
|
189 |
0 |
this.value = buffer.toString(); |
190 |
|
} |
191 |
|
|
192 |
5 |
return this.value; |
193 |
|
} |
194 |
|
|
|
|
| 87.5% |
Uncovered Elements: 2 (16) |
Complexity: 4 |
Complexity Density: 0.4 |
|
195 |
4 |
@Override... |
196 |
|
public boolean isCompatible(VersionRange otherRange) |
197 |
|
{ |
198 |
4 |
if (equals(otherRange)) { |
199 |
0 |
return true; |
200 |
|
} |
201 |
|
|
202 |
4 |
for (VersionRange versionRange : this.ranges) { |
203 |
4 |
boolean compatible; |
204 |
|
|
205 |
4 |
if (otherRange instanceof VersionRangeCollection) { |
206 |
2 |
compatible = ((VersionRangeCollection) otherRange).isCompatible(versionRange); |
207 |
|
} else { |
208 |
2 |
compatible = versionRange.isCompatible(otherRange); |
209 |
|
} |
210 |
|
|
211 |
4 |
if (compatible) { |
212 |
2 |
return true; |
213 |
|
} |
214 |
|
} |
215 |
|
|
216 |
2 |
return false; |
217 |
|
} |
218 |
|
|
219 |
|
|
220 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
221 |
2 |
@Override... |
222 |
|
public String toString() |
223 |
|
{ |
224 |
2 |
return getValue(); |
225 |
|
} |
226 |
|
|
|
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 4 |
Complexity Density: 0.67 |
|
227 |
10 |
@Override... |
228 |
|
public boolean equals(Object obj) |
229 |
|
{ |
230 |
10 |
if (this == obj) { |
231 |
0 |
return true; |
232 |
|
} |
233 |
|
|
234 |
10 |
if (obj == null || !(obj instanceof DefaultVersionRangeCollection)) { |
235 |
2 |
return false; |
236 |
|
} |
237 |
|
|
238 |
8 |
DefaultVersionRangeCollection versionConstraint = (DefaultVersionRangeCollection) obj; |
239 |
|
|
240 |
8 |
return this.ranges.equals(versionConstraint.getRanges()); |
241 |
|
} |
242 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
243 |
1 |
@Override... |
244 |
|
public int hashCode() |
245 |
|
{ |
246 |
1 |
return this.ranges.hashCode(); |
247 |
|
} |
248 |
|
|
249 |
|
|
250 |
|
|
251 |
|
|
252 |
|
@param |
253 |
|
@throws |
254 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
255 |
0 |
private void writeObject(ObjectOutputStream out) throws IOException... |
256 |
|
{ |
257 |
0 |
out.writeObject(getValue()); |
258 |
|
} |
259 |
|
|
260 |
|
|
261 |
|
@param |
262 |
|
@throws |
263 |
|
@throws |
264 |
|
@throws |
265 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
266 |
0 |
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException,... |
267 |
|
InvalidVersionRangeException |
268 |
|
{ |
269 |
0 |
setRanges((String) in.readObject()); |
270 |
|
} |
271 |
|
} |