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