1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.util.version;
20
21 import java.util.ArrayList;
22 import java.util.Collection;
23
24 import org.eclipse.aether.version.InvalidVersionSpecificationException;
25 import org.eclipse.aether.version.VersionRange;
26 import org.eclipse.aether.version.VersionScheme;
27
28 import static java.util.Objects.requireNonNull;
29
30
31
32
33
34
35
36
37 abstract class VersionSchemeSupport implements VersionScheme {
38 @Override
39 public GenericVersionRange parseVersionRange(final String range) throws InvalidVersionSpecificationException {
40 return new GenericVersionRange(this, range);
41 }
42
43 @Override
44 public GenericVersionConstraint parseVersionConstraint(final String constraint)
45 throws InvalidVersionSpecificationException {
46 String process = requireNonNull(constraint, "constraint cannot be null");
47
48 Collection<VersionRange> ranges = new ArrayList<>();
49
50 while (process.startsWith("[") || process.startsWith("(")) {
51 int index1 = process.indexOf(')');
52 int index2 = process.indexOf(']');
53
54 int index = index2;
55 if (index2 < 0 || (index1 >= 0 && index1 < index2)) {
56 index = index1;
57 }
58
59 if (index < 0) {
60 throw new InvalidVersionSpecificationException(constraint, "Unbounded version range " + constraint);
61 }
62
63 VersionRange range = parseVersionRange(process.substring(0, index + 1));
64 ranges.add(range);
65
66 process = process.substring(index + 1).trim();
67
68 if (process.startsWith(",")) {
69 process = process.substring(1).trim();
70 }
71 }
72
73 if (!process.isEmpty() && !ranges.isEmpty()) {
74 throw new InvalidVersionSpecificationException(
75 constraint, "Invalid version range " + constraint + ", expected [ or ( but got " + process);
76 }
77
78 GenericVersionConstraint result;
79 if (ranges.isEmpty()) {
80 result = new GenericVersionConstraint(parseVersion(constraint));
81 } else {
82 result = new GenericVersionConstraint(UnionVersionRange.from(ranges));
83 }
84
85 return result;
86 }
87 }