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.VersionScheme;
26
27 import static java.util.Objects.requireNonNull;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public final class GenericVersionScheme implements VersionScheme {
55
56
57
58
59 public GenericVersionScheme() {}
60
61 @Override
62 public GenericVersion parseVersion(final String version) throws InvalidVersionSpecificationException {
63 return new GenericVersion(version);
64 }
65
66 @Override
67 public GenericVersionRange parseVersionRange(final String range) throws InvalidVersionSpecificationException {
68 return new GenericVersionRange(range);
69 }
70
71 @Override
72 public GenericVersionConstraint parseVersionConstraint(final String constraint)
73 throws InvalidVersionSpecificationException {
74 String process = requireNonNull(constraint, "constraint cannot be null");
75
76 Collection<GenericVersionRange> ranges = new ArrayList<>();
77
78 while (process.startsWith("[") || process.startsWith("(")) {
79 int index1 = process.indexOf(')');
80 int index2 = process.indexOf(']');
81
82 int index = index2;
83 if (index2 < 0 || (index1 >= 0 && index1 < index2)) {
84 index = index1;
85 }
86
87 if (index < 0) {
88 throw new InvalidVersionSpecificationException(constraint, "Unbounded version range " + constraint);
89 }
90
91 GenericVersionRange range = parseVersionRange(process.substring(0, index + 1));
92 ranges.add(range);
93
94 process = process.substring(index + 1).trim();
95
96 if (process.startsWith(",")) {
97 process = process.substring(1).trim();
98 }
99 }
100
101 if (!process.isEmpty() && !ranges.isEmpty()) {
102 throw new InvalidVersionSpecificationException(
103 constraint, "Invalid version range " + constraint + ", expected [ or ( but got " + process);
104 }
105
106 GenericVersionConstraint result;
107 if (ranges.isEmpty()) {
108 result = new GenericVersionConstraint(parseVersion(constraint));
109 } else {
110 result = new GenericVersionConstraint(UnionVersionRange.from(ranges));
111 }
112
113 return result;
114 }
115
116 @Override
117 public boolean equals(final Object obj) {
118 if (this == obj) {
119 return true;
120 }
121
122 return obj != null && getClass().equals(obj.getClass());
123 }
124
125 @Override
126 public int hashCode() {
127 return getClass().hashCode();
128 }
129
130
131
132
133
134
135
136
137
138
139
140 public static void main(String... args) {
141 System.out.println("Display parameters as parsed by Maven Resolver (in canonical form and as a list of tokens)"
142 + " and comparison result:");
143 if (args.length == 0) {
144 return;
145 }
146
147 GenericVersion prev = null;
148 int i = 1;
149 for (String version : args) {
150 GenericVersion c = new GenericVersion(version);
151
152 if (prev != null) {
153 int compare = prev.compareTo(c);
154 System.out.println(
155 " " + prev + ' ' + ((compare == 0) ? "==" : ((compare < 0) ? "<" : ">")) + ' ' + version);
156 }
157
158 System.out.println((i++) + ". " + version + " -> " + c.asString() + "; tokens: " + c.asItems());
159
160 prev = c;
161 }
162 }
163 }