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 static org.apache.maven.artifact.versioning.VersionRange.createFromVersionSpec;
22 import static org.apache.maven.internal.impl.Utils.nonNull;
23
24 import java.util.Objects;
25 import java.util.regex.Pattern;
26 import javax.inject.Named;
27 import javax.inject.Singleton;
28 import org.apache.maven.api.Version;
29 import org.apache.maven.api.VersionRange;
30 import org.apache.maven.api.services.VersionParser;
31 import org.apache.maven.api.services.VersionParserException;
32 import org.apache.maven.artifact.versioning.ArtifactVersion;
33 import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
34 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
35
36 @Named
37 @Singleton
38 public class DefaultVersionParser implements VersionParser {
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
42 @Override
43 public Version parseVersion(String version) {
44 return new DefaultVersion(new DefaultArtifactVersion(nonNull(version, "version")));
45 }
46
47 @Override
48 public VersionRange parseVersionRange(String range) {
49 try {
50 return new DefaultVersionRange(createFromVersionSpec(nonNull(range, "version")));
51 } catch (InvalidVersionSpecificationException e) {
52 throw new VersionParserException("Unable to parse version range: " + range, e);
53 }
54 }
55
56 @Override
57 public boolean isSnapshot(String version) {
58 return checkSnapshot(version);
59 }
60
61 static boolean checkSnapshot(String version) {
62 return version.endsWith(SNAPSHOT) || SNAPSHOT_TIMESTAMP.matcher(version).matches();
63 }
64
65 static class DefaultVersion implements Version {
66 private final ArtifactVersion delegate;
67
68 DefaultVersion(ArtifactVersion delegate) {
69 this.delegate = delegate;
70 }
71
72 @Override
73 public int compareTo(Version o) {
74 if (o instanceof DefaultVersion) {
75 return delegate.compareTo(((DefaultVersion) o).delegate);
76 } else {
77 return delegate.compareTo(new DefaultArtifactVersion(o.toString()));
78 }
79 }
80
81 @Override
82 public boolean equals(Object o) {
83 if (this == o) {
84 return true;
85 }
86 if (o == null || getClass() != o.getClass()) {
87 return false;
88 }
89 DefaultVersion that = (DefaultVersion) o;
90 return delegate.equals(that.delegate);
91 }
92
93 @Override
94 public int hashCode() {
95 return Objects.hash(delegate);
96 }
97
98 @Override
99 public String asString() {
100 return delegate.toString();
101 }
102
103 @Override
104 public String toString() {
105 return asString();
106 }
107 }
108
109 static class DefaultVersionRange implements VersionRange {
110 private final org.apache.maven.artifact.versioning.VersionRange delegate;
111
112 DefaultVersionRange(org.apache.maven.artifact.versioning.VersionRange delegate) {
113 this.delegate = delegate;
114 }
115
116 @Override
117 public boolean contains(Version version) {
118 if (version instanceof DefaultVersion) {
119 return delegate.containsVersion(((DefaultVersion) version).delegate);
120 } else {
121 return delegate.containsVersion(new DefaultArtifactVersion(version.toString()));
122 }
123 }
124
125 @Override
126 public String asString() {
127 return delegate.toString();
128 }
129
130 @Override
131 public String toString() {
132 return asString();
133 }
134 }
135 }