Debian Codegraph @

bundles/org.eclipse.equinox.p2.metadata/src/org/eclipse/equinox/p2/metadata/VersionRange.java

raw
1 /*******************************************************************************
2 * Copyright (c) 2003, 2017 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 * Cloudsmith Inc - rewrite to handle non-OSGi versions.
14 *******************************************************************************/
15 package org.eclipse.equinox.p2.metadata;
16
17 import java.io.Serializable;
18 import java.lang.ref.SoftReference;
19 import java.util.WeakHashMap;
20 import org.eclipse.equinox.internal.p2.metadata.*;
21 import org.eclipse.osgi.util.NLS;
22
23 /**
24 * This class represents a version range with Omni Version bounds. It is signature
25 * equivalent with the OSGi org.eclipse.osgi.service.resolver.VersionRange
26 *
27 * @Immutable
28 * @noextend This class is not intended to be subclassed by clients.
29 * @since 2.0
30 */
31 public class VersionRange implements Serializable {
32 private static final long serialVersionUID = 4988030307298088028L;
33
34 /**
35 * TODO: This should not be OSGi but it has to be that for now since the resolver creates
36 * a filter where the min and max are converted into strings. When the filter is evaluated an
37 * attempt is made to recreate them as OSGi versions.
38 *
39 * An empty OSGi Version range.
40 */
41 public static final VersionRange emptyRange = new VersionRange(Version.emptyVersion, true, Version.MAX_VERSION, true);
42 private static WeakHashMap<String, SoftReference<VersionRange>> POOL = new WeakHashMap<>();
43
44 private final Version minVersion;
45 private final boolean includeMin;
46 private final Version maxVersion;
47 private final boolean includeMax;
48
49 private static int copyEscaped(String vr, int pos, String breakChars, StringBuffer sb) {
50 int top = vr.length();
51 pos = VersionParser.skipWhite(vr, pos);
52 if (pos >= top)
53 throw new IllegalArgumentException();
54
55 char c = vr.charAt(pos);
56 for (;;) {
57 if (c == '\\' && ++pos < top)
58 c = vr.charAt(pos);
59 else {
60 if (c <= ' ')
61 return VersionParser.skipWhite(vr, pos);
62 if (breakChars != null && breakChars.indexOf(c) >= 0)
63 break;
64 }
65 sb.append(c);
66 if (++pos >= top)
67 break;
68 c = vr.charAt(pos);
69 }
70 return pos;
71 }
72
73 /**
74 * Constructs a VersionRange with the specified minVersion and maxVersion.
75 * @param minVersion the minimum version of the range
76 * @param maxVersion the maximum version of the range
77 */
78 public VersionRange(Version minVersion, boolean includeMin, Version maxVersion, boolean includeMax) {
79 if (minVersion == null) {
80 if (maxVersion == null) {
81 minVersion = Version.emptyVersion;
82 maxVersion = Version.MAX_VERSION;
83 } else
84 minVersion = Version.emptyVersion;
85 } else {
86 if (maxVersion == null)
87 maxVersion = Version.MAX_VERSION;
88 else {
89 if (minVersion != maxVersion && minVersion.equals(maxVersion))
90 maxVersion = minVersion;
91 else if (!(minVersion.getFormat() == null ? maxVersion.getFormat() == null : minVersion.getFormat().equals(maxVersion.getFormat()))) {
92 // We always allow the MIN and MAX boundaries
93 if (!(minVersion.equals(Version.emptyVersion) || maxVersion.equals(Version.MAX_VERSION)))
94 throw new IllegalArgumentException(NLS.bind(Messages.range_boundaries_0_and_1_cannot_have_different_formats, minVersion, maxVersion));
95 }
96 }
97 }
98 this.minVersion = minVersion;
99 this.includeMin = includeMin;
100 this.maxVersion = maxVersion;
101 this.includeMax = includeMax;
102 validateRange();
103 }
104
105 /**
106 * Constructs a VersionRange from the given versionRange String.
107 * @param versionRange a version range String that specifies a range of
108 * versions.
109 */
110 public VersionRange(String versionRange) {
111 int top = 0;
112 int pos = 0;
113 if (versionRange != null) {
114 top = versionRange.length();
115 pos = VersionParser.skipWhite(versionRange, 0);
116 top = VersionParser.skipTrailingWhite(versionRange, pos, top);
117 }
118
119 if (pos >= top) {
120 minVersion = Version.emptyVersion;
121 includeMin = true;
122 maxVersion = Version.MAX_VERSION;
123 includeMax = true;
124 return;
125 }
126
127 char c = versionRange.charAt(pos);
128 int[] position = new int[1];
129 boolean rawPrefix = false;
130 IVersionFormat fmt = null;
131 if (VersionParser.isLetter(c)) {
132 if (versionRange.startsWith("raw:", pos)) { //$NON-NLS-1$
133 rawPrefix = true;
134 pos += 4;
135 } else {
136 position[0] = pos;
137 fmt = parseFormat(versionRange, position);
138 pos = position[0];
139 if (pos >= versionRange.length())
140 throw new IllegalArgumentException(NLS.bind(Messages.format_must_be_delimited_by_colon_0, versionRange));
141
142 c = versionRange.charAt(pos);
143 if (c != ':')
144 throw new IllegalArgumentException(NLS.bind(Messages.format_must_be_delimited_by_colon_0, versionRange));
145 ++pos;
146 }
147 pos = VersionParser.skipWhite(versionRange, pos);
148 if (pos >= top)
149 throw new IllegalArgumentException(NLS.bind(Messages.premature_EOS_0, versionRange));
150 c = versionRange.charAt(pos);
151 } else
152 fmt = VersionFormat.OSGI_FORMAT;
153
154 String minStr;
155 String maxStr;
156 StringBuffer sb = new StringBuffer();
157 if (c == '[' || c == '(') {
158 includeMin = (c == '[');
159 pos = copyEscaped(versionRange, ++pos, ",)]", sb); //$NON-NLS-1$
160 if (pos >= top)
161 throw new IllegalArgumentException(NLS.bind(Messages.premature_EOS_0, versionRange));
162 c = versionRange.charAt(pos++);
163 if (c != ',')
164 throw new IllegalArgumentException(NLS.bind(Messages.missing_comma_in_range_0, versionRange));
165
166 minStr = sb.toString();
167 sb.setLength(0);
168 pos = copyEscaped(versionRange, pos, ")]", sb); //$NON-NLS-1$
169 if (pos >= top)
170 throw new IllegalArgumentException();
171 maxStr = sb.toString();
172
173 c = versionRange.charAt(pos++);
174 includeMax = (c == ']');
175 } else {
176 StringBuffer sbMin = new StringBuffer();
177 pos = copyEscaped(versionRange, pos, rawPrefix ? "/" : null, sbMin); //$NON-NLS-1$
178 includeMin = includeMax = true;
179 minStr = sbMin.toString();
180 maxStr = null;
181 }
182
183 if (rawPrefix) {
184 String origMin = null;
185 String origMax = null;
186 pos = VersionParser.skipWhite(versionRange, pos);
187 if (pos < top && versionRange.charAt(pos) == '/') {
188 if (++pos == top)
189 throw new IllegalArgumentException(NLS.bind(Messages.original_stated_but_missing_0, versionRange));
190 position[0] = pos;
191 fmt = parseFormat(versionRange, position);
192 pos = VersionParser.skipWhite(versionRange, position[0]);
193 if (pos < top) {
194 boolean origUseIncDelims = false;
195 c = versionRange.charAt(pos);
196 if (c != ':')
197 throw new IllegalArgumentException(NLS.bind(Messages.original_must_start_with_colon_0, versionRange));
198
199 pos = VersionParser.skipWhite(versionRange, ++pos);
200 if (pos == top)
201 throw new IllegalArgumentException(NLS.bind(Messages.original_stated_but_missing_0, versionRange));
202
203 c = versionRange.charAt(pos);
204 if (c == '[' || c == '(') {
205 if (includeMin != (c == '[') || maxStr == null)
206 throw new IllegalArgumentException(NLS.bind(Messages.raw_and_original_must_use_same_range_inclusion_0, versionRange));
207 pos = VersionParser.skipWhite(versionRange, ++pos);
208 origUseIncDelims = true;
209 }
210
211 sb.setLength(0);
212 if (maxStr == null) {
213 copyEscaped(versionRange, pos, ",])", sb); //$NON-NLS-1$
214 origMin = sb.toString();
215 } else {
216 pos = copyEscaped(versionRange, pos, ",])", sb); //$NON-NLS-1$
217 if (pos >= top)
218 throw new IllegalArgumentException(NLS.bind(Messages.premature_EOS_0, versionRange));
219 c = versionRange.charAt(pos++);
220 if (c != ',')
221 throw new IllegalArgumentException(NLS.bind(Messages.missing_comma_in_range_0, versionRange));
222 origMin = sb.toString();
223
224 sb.setLength(0);
225 pos = copyEscaped(versionRange, pos, "])", sb); //$NON-NLS-1$
226 if (origUseIncDelims) {
227 if (pos >= top)
228 throw new IllegalArgumentException(NLS.bind(Messages.premature_EOS_0, versionRange));
229 c = versionRange.charAt(pos++);
230 if (includeMax != (c == ']'))
231 throw new IllegalArgumentException(NLS.bind(Messages.raw_and_original_must_use_same_range_inclusion_0, versionRange));
232 }
233 origMax = sb.toString();
234 }
235 }
236 }
237 minVersion = VersionFormat.parseRaw(minStr, fmt, origMin);
238 if (maxStr != null) {
239 if (maxStr.equals(minStr))
240 maxVersion = minVersion;
241 else
242 maxVersion = VersionFormat.parseRaw(maxStr, fmt, origMax);
243 } else
244 maxVersion = Version.MAX_VERSION;
245 } else {
246 if (fmt == null)
247 fmt = VersionFormat.OSGI_FORMAT;
248 minVersion = fmt.parse(minStr);
249 if (maxStr != null) {
250 if (maxStr.equals(minStr))
251 maxVersion = minVersion;
252 else
253 maxVersion = fmt.parse(maxStr);
254 } else {
255 maxVersion = Version.MAX_VERSION;
256 }
257 }
258 validateRange();
259 }
260
261 /**
262 * Parses a version range from the specified string.
263 * <p>
264 * Note that this method performs a non thread-safe object pooling. Instances are
265 * stored in a weak cache, i.e. for multiple calls with the same input it is likely
266 * but not guaranteed that the same instance is retrieved. Same holds for concurrent
267 * access on this method. Clients must not assume to get the same instance for
268 * subsequent calls.
269 *
270 * @param versionRange String representation of the version range. Leading
271 * and trailing whitespace will be ignored.
272 * @return A <code>VersionRange</code> object representing the version range
273 * or <code>null</code> if <code>versionRange</code> is <code>null</code> or
274 * an empty string.
275 * @throws IllegalArgumentException If <code>versionRange</code> is improperly
276 * formatted.
277 * @since 2.4
278 */
279 public static VersionRange create(String versionRange) {
280 VersionRange v = null;
281 if (versionRange != null && versionRange.length() > 0) {
282 SoftReference<VersionRange> vRef = POOL.get(versionRange);
283 v = vRef != null ? vRef.get() : null;
284 if (v == null) {
285 v = new VersionRange(versionRange);
286 synchronized (POOL) {
287 POOL.put(versionRange, new SoftReference<>(v));
288 }
289 }
290 }
291 return v;
292 }
293
294 private static IVersionFormat parseFormat(String versionRange, int[] position) {
295 int pos = VersionParser.skipWhite(versionRange, position[0]);
296 if (!versionRange.startsWith("format(", pos)) //$NON-NLS-1$
297 return null;
298
299 pos += 7;
300 int end = VersionParser.findEndOfFormat(versionRange, pos, versionRange.length());
301 try {
302 position[0] = end + 1;
303 return VersionFormat.compile(versionRange, pos, end);
304 } catch (VersionFormatException e) {
305 throw new IllegalArgumentException(e.getMessage(), e);
306 }
307 }
308
309 /**
310 * Returns the version format.
311 */
312 public IVersionFormat getFormat() {
313 return minVersion.equals(Version.emptyVersion) ? maxVersion.getFormat() : minVersion.getFormat();
314 }
315
316 /**
317 * Returns the minimum Version of this VersionRange
318 * @return the minimum Version of this VersionRange
319 */
320 public Version getMinimum() {
321 return minVersion;
322 }
323
324 /**
325 * Indicates if the minimum version is included in the version range.
326 * @return true if the minimum version is included in the version range;
327 * otherwise false is returned
328 */
329 public boolean getIncludeMinimum() {
330 return includeMin;
331 }
332
333 /**
334 * Returns the maximum Version of this VersionRange
335 * @return the maximum Version of this VersionRange
336 */
337 public Version getMaximum() {
338 return maxVersion;
339 }
340
341 /**
342 * Indicates if the maximum version is included in the version range.
343 * @return true if the maximum version is included in the version range;
344 * otherwise false is returned
345 */
346 public boolean getIncludeMaximum() {
347 return includeMax;
348 }
349
350 public VersionRange intersect(VersionRange r2) {
351 int minCompare = minVersion.compareTo(r2.getMinimum());
352 int maxCompare = maxVersion.compareTo(r2.getMaximum());
353
354 boolean resultMinIncluded;
355 Version resultMin;
356 if (minCompare == 0) {
357 if (maxCompare == 0 && includeMin == r2.getIncludeMinimum() && includeMax == r2.getIncludeMaximum())
358 return this;
359 resultMin = minVersion;
360 resultMinIncluded = includeMin && r2.getIncludeMinimum();
361 } else if (minCompare < 0) {
362 resultMin = r2.getMinimum();
363 resultMinIncluded = r2.getIncludeMinimum();
364 } else { // minCompare > 0)
365 resultMin = minVersion;
366 resultMinIncluded = includeMin;
367 }
368
369 boolean resultMaxIncluded;
370 Version resultMax;
371 if (maxCompare > 0) {
372 resultMax = r2.getMaximum();
373 resultMaxIncluded = r2.getIncludeMaximum();
374 } else if (maxCompare < 0) {
375 resultMax = maxVersion;
376 resultMaxIncluded = includeMax;
377 } else {//maxCompare == 0
378 resultMax = maxVersion;
379 resultMaxIncluded = includeMax && r2.getIncludeMaximum();
380 }
381
382 int minMaxCmp = resultMin.compareTo(resultMax);
383 if (minMaxCmp < 0 || (minMaxCmp == 0 && resultMinIncluded && resultMaxIncluded))
384 return new VersionRange(resultMin, resultMinIncluded, resultMax, resultMaxIncluded);
385
386 return null;
387 }
388
389 /**
390 * Returns whether the given version is included in this VersionRange.
391 * This will depend on the minimum and maximum versions of this VersionRange
392 * and the given version.
393 *
394 * @param version a version to be tested for inclusion in this VersionRange.
395 * (may be <code>null</code>)
396 * @return <code>true</code> if the version is include,
397 * <code>false</code> otherwise
398 */
399 public boolean isIncluded(Version version) {
400 if (version == null)
401 return false;
402
403 if (minVersion == maxVersion)
404 // Can only happen when both includeMin and includeMax are true
405 return minVersion.equals(version);
406
407 int minCheck = includeMin ? 0 : -1;
408 int maxCheck = includeMax ? 0 : 1;
409 return minVersion.compareTo(version) <= minCheck && maxVersion.compareTo(version) >= maxCheck;
410 }
411
412 /**
413 * Checks if the versions of this range is in compliance with the OSGi version spec.
414 * @return A flag indicating whether the range is OSGi compatible or not.
415 */
416 public boolean isOSGiCompatible() {
417 return minVersion.isOSGiCompatible() && maxVersion.isOSGiCompatible();
418 }
419
420 @Override
421 public boolean equals(Object object) {
422 if (!(object instanceof VersionRange))
423 return false;
424 VersionRange vr = (VersionRange) object;
425 return includeMin == vr.includeMin && includeMax == vr.includeMax && minVersion.equals(vr.getMinimum()) && maxVersion.equals(vr.getMaximum());
426 }
427
428 @Override
429 public int hashCode() {
430 final int prime = 31;
431 int result = 1;
432 result = prime * result + maxVersion.hashCode();
433 result = prime * result + minVersion.hashCode();
434 result = prime * result + (includeMax ? 1231 : 1237);
435 result = prime * result + (includeMin ? 1231 : 1237);
436 return result;
437 }
438
439 @Override
440 public String toString() {
441 StringBuffer result = new StringBuffer();
442 toString(result);
443 return result.toString();
444 }
445
446 public void toString(StringBuffer result) {
447 boolean gtEqual = includeMin && includeMax && Version.MAX_VERSION.equals(maxVersion);
448 if (gtEqual && Version.emptyVersion.equals(minVersion)) {
449 minVersion.toString(result);
450 return;
451 }
452
453 IVersionFormat fmt = getFormat();
454 if (fmt == VersionFormat.OSGI_FORMAT) {
455 if (gtEqual) {
456 minVersion.toString(result);
457 } else {
458 result.append(includeMin ? '[' : '(');
459 minVersion.toString(result);
460 result.append(',');
461 maxVersion.toString(result);
462 result.append(includeMax ? ']' : ')');
463 }
464 return;
465 }
466
467 result.append("raw:"); //$NON-NLS-1$
468 if (gtEqual) {
469 ((BasicVersion) minVersion).rawToString(result, true);
470 } else {
471 result.append(includeMin ? '[' : '(');
472 ((BasicVersion) minVersion).rawToString(result, true);
473 result.append(',');
474 ((BasicVersion) maxVersion).rawToString(result, true);
475 result.append(includeMax ? ']' : ')');
476 }
477 boolean hasOriginal = (minVersion.getOriginal() != null || maxVersion.getOriginal() != null);
478 if (fmt != null || hasOriginal) {
479 result.append('/');
480 if (fmt != null)
481 fmt.toString(result);
482 if (hasOriginal) {
483 result.append(':');
484 if (gtEqual) {
485 ((BasicVersion) minVersion).originalToString(result, true);
486 } else {
487 if (Version.emptyVersion.equals(minVersion))
488 ((BasicVersion) minVersion).rawToString(result, true);
489 else
490 ((BasicVersion) minVersion).originalToString(result, true);
491 result.append(',');
492 ((BasicVersion) maxVersion).originalToString(result, true);
493 }
494 }
495 }
496 }
497
498 // Preserve singletons during deserialization
499 private Object readResolve() {
500 VersionRange vr = this;
501 if (equals(emptyRange))
502 vr = emptyRange;
503 return vr;
504 }
505
506 private void validateRange() {
507 int cmp = minVersion.compareTo(maxVersion);
508 if (!(cmp < 0 || (cmp == 0 && includeMin && includeMax)))
509 throw new IllegalArgumentException(NLS.bind(Messages.range_min_0_is_not_less_then_range_max_1, minVersion, maxVersion));
510 }
511
512 }