1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.internal.impl;
20
21 import java.util.regex.Pattern;
22
23 import org.apache.maven.api.Version;
24 import org.apache.maven.api.VersionConstraint;
25 import org.apache.maven.api.VersionRange;
26 import org.apache.maven.api.di.Inject;
27 import org.apache.maven.api.di.Named;
28 import org.apache.maven.api.di.Singleton;
29 import org.apache.maven.api.services.VersionParserException;
30 import org.apache.maven.api.services.model.ModelVersionParser;
31 import org.eclipse.aether.version.InvalidVersionSpecificationException;
32 import org.eclipse.aether.version.VersionScheme;
33
34 import static java.util.Objects.requireNonNull;
35
36 @Named
37 @Singleton
38 public class DefaultModelVersionParser implements ModelVersionParser {
39 private static final String SNAPSHOT = "SNAPSHOT";
40 private static final Pattern SNAPSHOT_TIMESTAMP = Pattern.compile("^(.*-)?([0-9]{8}\\.[0-9]{6}-[0-9]+)$");
41 private final VersionScheme versionScheme;
42
43 @Inject
44 public DefaultModelVersionParser(VersionScheme versionScheme) {
45 this.versionScheme = requireNonNull(versionScheme, "versionScheme");
46 }
47
48 @Override
49 public Version parseVersion(String version) {
50 requireNonNull(version, "version");
51 return new DefaultVersion(versionScheme, version);
52 }
53
54 @Override
55 public VersionRange parseVersionRange(String range) {
56 requireNonNull(range, "range");
57 return new DefaultVersionRange(versionScheme, range);
58 }
59
60 @Override
61 public boolean isSnapshot(String version) {
62 return checkSnapshot(version);
63 }
64
65 public static boolean checkSnapshot(String version) {
66 return version.endsWith(SNAPSHOT) || SNAPSHOT_TIMESTAMP.matcher(version).matches();
67 }
68
69 @Override
70 public VersionConstraint parseVersionConstraint(String constraint) {
71 requireNonNull(constraint, "constraint");
72 return new DefaultVersionConstraint(versionScheme, constraint);
73 }
74
75 static class DefaultVersion implements Version {
76 private final VersionScheme versionScheme;
77 private final org.eclipse.aether.version.Version delegate;
78
79 DefaultVersion(VersionScheme versionScheme, org.eclipse.aether.version.Version delegate) {
80 this.versionScheme = versionScheme;
81 this.delegate = delegate;
82 }
83
84 DefaultVersion(VersionScheme versionScheme, String delegateValue) {
85 this.versionScheme = versionScheme;
86 try {
87 this.delegate = versionScheme.parseVersion(delegateValue);
88 } catch (InvalidVersionSpecificationException e) {
89 throw new VersionParserException("Unable to parse version: " + delegateValue, e);
90 }
91 }
92
93 @Override
94 public int compareTo(Version o) {
95 if (o instanceof DefaultVersion) {
96 return delegate.compareTo(((DefaultVersion) o).delegate);
97 } else {
98 return compareTo(new DefaultVersion(versionScheme, o.asString()));
99 }
100 }
101
102 @Override
103 public boolean equals(Object o) {
104 if (this == o) {
105 return true;
106 }
107 if (o == null || getClass() != o.getClass()) {
108 return false;
109 }
110 DefaultVersion that = (DefaultVersion) o;
111 return delegate.equals(that.delegate);
112 }
113
114 @Override
115 public int hashCode() {
116 return delegate.hashCode();
117 }
118
119 @Override
120 public String asString() {
121 return delegate.toString();
122 }
123
124 @Override
125 public String toString() {
126 return asString();
127 }
128 }
129
130 static class DefaultVersionRange implements VersionRange {
131 private final VersionScheme versionScheme;
132 private final org.eclipse.aether.version.VersionRange delegate;
133
134 DefaultVersionRange(VersionScheme versionScheme, org.eclipse.aether.version.VersionRange delegate) {
135 this.versionScheme = versionScheme;
136 this.delegate = delegate;
137 }
138
139 DefaultVersionRange(VersionScheme versionScheme, String delegateValue) {
140 this.versionScheme = versionScheme;
141 try {
142 this.delegate = versionScheme.parseVersionRange(delegateValue);
143 } catch (InvalidVersionSpecificationException e) {
144 throw new VersionParserException("Unable to parse version range: " + delegateValue, e);
145 }
146 }
147
148 @Override
149 public boolean contains(Version version) {
150 if (version instanceof DefaultVersion) {
151 return delegate.containsVersion(((DefaultVersion) version).delegate);
152 } else {
153 return contains(new DefaultVersion(versionScheme, version.asString()));
154 }
155 }
156
157 @Override
158 public Boundary getUpperBoundary() {
159 org.eclipse.aether.version.VersionRange.Bound bound = delegate.getUpperBound();
160 if (bound == null) {
161 return null;
162 }
163 return new Boundary() {
164 @Override
165 public Version getVersion() {
166 return new DefaultVersion(versionScheme, bound.getVersion());
167 }
168
169 @Override
170 public boolean isInclusive() {
171 return bound.isInclusive();
172 }
173 };
174 }
175
176 @Override
177 public Boundary getLowerBoundary() {
178 org.eclipse.aether.version.VersionRange.Bound bound = delegate.getLowerBound();
179 if (bound == null) {
180 return null;
181 }
182 return new Boundary() {
183 @Override
184 public Version getVersion() {
185 return new DefaultVersion(versionScheme, bound.getVersion());
186 }
187
188 @Override
189 public boolean isInclusive() {
190 return bound.isInclusive();
191 }
192 };
193 }
194
195 @Override
196 public String asString() {
197 return delegate.toString();
198 }
199
200 @Override
201 public String toString() {
202 return asString();
203 }
204
205 @Override
206 public boolean equals(Object o) {
207 if (this == o) {
208 return true;
209 }
210 if (o == null || getClass() != o.getClass()) {
211 return false;
212 }
213 DefaultVersionRange that = (DefaultVersionRange) o;
214 return delegate.equals(that.delegate);
215 }
216
217 @Override
218 public int hashCode() {
219 return delegate.hashCode();
220 }
221 }
222
223 static class DefaultVersionConstraint implements VersionConstraint {
224 private final VersionScheme versionScheme;
225 private final org.eclipse.aether.version.VersionConstraint delegate;
226
227 DefaultVersionConstraint(VersionScheme versionScheme, String delegateValue) {
228 this.versionScheme = versionScheme;
229 try {
230 this.delegate = versionScheme.parseVersionConstraint(delegateValue);
231 } catch (InvalidVersionSpecificationException e) {
232 throw new VersionParserException("Unable to parse version constraint: " + delegateValue, e);
233 }
234 }
235
236 @Override
237 public boolean contains(Version version) {
238 if (version instanceof DefaultVersion) {
239 return delegate.containsVersion(((DefaultVersion) version).delegate);
240 } else {
241 return contains(new DefaultVersion(versionScheme, version.asString()));
242 }
243 }
244
245 @Override
246 public String asString() {
247 return delegate.toString();
248 }
249
250 @Override
251 public VersionRange getVersionRange() {
252 if (delegate.getRange() == null) {
253 return null;
254 }
255 return new DefaultVersionRange(versionScheme, delegate.getRange());
256 }
257
258 @Override
259 public Version getRecommendedVersion() {
260 if (delegate.getVersion() == null) {
261 return null;
262 }
263 return new DefaultVersion(versionScheme, delegate.getVersion());
264 }
265
266 @Override
267 public String toString() {
268 return asString();
269 }
270
271 @Override
272 public boolean equals(Object o) {
273 if (this == o) {
274 return true;
275 }
276 if (o == null || getClass() != o.getClass()) {
277 return false;
278 }
279 DefaultVersionConstraint that = (DefaultVersionConstraint) o;
280 return delegate.equals(that.delegate);
281 }
282
283 @Override
284 public int hashCode() {
285 return delegate.hashCode();
286 }
287 }
288 }