Skip to content

Commit 507d897

Browse files
committed
Use ComparableVersion from Maven and remove the duplicate
1 parent 10f8af1 commit 507d897

24 files changed

+277
-571
lines changed

pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,11 @@
267267
<artifactId>commons-text</artifactId>
268268
<version>1.10.0</version>
269269
</dependency>
270-
270+
<dependency>
271+
<groupId>org.apache.commons</groupId>
272+
<artifactId>commons-collections4</artifactId>
273+
<version>4.4</version>
274+
</dependency>
271275
<dependency>
272276
<groupId>org.codehaus.plexus</groupId>
273277
<artifactId>plexus-utils</artifactId>

versions-common/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131

3232
import org.apache.maven.artifact.ArtifactUtils;
3333
import org.apache.maven.artifact.versioning.ArtifactVersion;
34-
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
3534
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
3635
import org.apache.maven.artifact.versioning.Restriction;
3736
import org.apache.maven.artifact.versioning.VersionRange;
3837
import org.codehaus.mojo.versions.ordering.BoundArtifactVersion;
3938
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
4039
import org.codehaus.mojo.versions.ordering.VersionComparator;
40+
import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache;
4141

4242
import static java.util.Collections.reverseOrder;
4343
import static java.util.Optional.empty;
@@ -123,7 +123,7 @@ public final void setCurrentVersion(ArtifactVersion currentVersion) {
123123

124124
@Override
125125
public final void setCurrentVersion(String currentVersion) {
126-
setCurrentVersion(currentVersion == null ? null : new DefaultArtifactVersion(currentVersion));
126+
setCurrentVersion(currentVersion == null ? null : DefaultArtifactVersionCache.of(currentVersion));
127127
}
128128

129129
@Override
@@ -194,7 +194,7 @@ private ArtifactVersion[] getNewerVersions(ArtifactVersion version, boolean incl
194194

195195
@Override
196196
public final ArtifactVersion[] getNewerVersions(String version, boolean includeSnapshots) {
197-
return getNewerVersions(new DefaultArtifactVersion(version), includeSnapshots);
197+
return getNewerVersions(DefaultArtifactVersionCache.of(version), includeSnapshots);
198198
}
199199

200200
@Deprecated
@@ -209,10 +209,10 @@ public final ArtifactVersion[] getNewerVersions(
209209
public final ArtifactVersion[] getNewerVersions(
210210
String versionString, Optional<Segment> unchangedSegment, boolean includeSnapshots, boolean allowDowngrade)
211211
throws InvalidSegmentException {
212-
ArtifactVersion currentVersion = new DefaultArtifactVersion(versionString);
212+
ArtifactVersion currentVersion = DefaultArtifactVersionCache.of(versionString);
213213
ArtifactVersion lowerBound = allowDowngrade
214214
? getLowerBound(currentVersion, unchangedSegment)
215-
.map(DefaultArtifactVersion::new)
215+
.map(DefaultArtifactVersionCache::of)
216216
.orElse(null)
217217
: currentVersion;
218218
ArtifactVersion upperBound = unchangedSegment
@@ -228,10 +228,10 @@ public final ArtifactVersion[] getNewerVersions(
228228
public Optional<ArtifactVersion> getNewestVersion(
229229
String versionString, Optional<Segment> upperBoundSegment, boolean includeSnapshots, boolean allowDowngrade)
230230
throws InvalidSegmentException {
231-
ArtifactVersion currentVersion = new DefaultArtifactVersion(versionString);
231+
ArtifactVersion currentVersion = DefaultArtifactVersionCache.of(versionString);
232232
ArtifactVersion lowerBound = allowDowngrade
233233
? getLowerBound(currentVersion, upperBoundSegment)
234-
.map(DefaultArtifactVersion::new)
234+
.map(DefaultArtifactVersionCache::of)
235235
.orElse(null)
236236
: currentVersion;
237237
ArtifactVersion upperBound = upperBoundSegment

versions-common/src/main/java/org/codehaus/mojo/versions/api/DefaultVersionsHelper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
import org.apache.maven.artifact.ArtifactUtils;
5353
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
5454
import org.apache.maven.artifact.versioning.ArtifactVersion;
55-
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
5655
import org.apache.maven.artifact.versioning.Restriction;
5756
import org.apache.maven.artifact.versioning.VersionRange;
5857
import org.apache.maven.execution.MavenSession;
@@ -73,6 +72,7 @@
7372
import org.codehaus.mojo.versions.model.io.xpp3.RuleXpp3Reader;
7473
import org.codehaus.mojo.versions.ordering.VersionComparator;
7574
import org.codehaus.mojo.versions.ordering.VersionComparators;
75+
import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache;
7676
import org.codehaus.mojo.versions.utils.DependencyBuilder;
7777
import org.codehaus.mojo.versions.utils.DependencyComparator;
7878
import org.codehaus.mojo.versions.utils.PluginComparator;
@@ -228,7 +228,7 @@ public ArtifactVersions lookupArtifactVersions(
228228

229229
return false;
230230
}))
231-
.map(v -> new DefaultArtifactVersion(v.toString()))
231+
.map(v -> DefaultArtifactVersionCache.of(v.toString()))
232232
.collect(Collectors.toList()),
233233
getVersionComparator(artifact));
234234
} catch (VersionRangeResolutionException e) {
@@ -425,7 +425,7 @@ public Set<Artifact> extractArtifacts(Collection<MavenProject> mavenProjects) {
425425

426426
@Override
427427
public ArtifactVersion createArtifactVersion(String version) {
428-
return new DefaultArtifactVersion(version);
428+
return DefaultArtifactVersionCache.of(version);
429429
}
430430

431431
@Override

versions-common/src/main/java/org/codehaus/mojo/versions/api/PropertyVersions.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.apache.maven.artifact.Artifact;
3434
import org.apache.maven.artifact.ArtifactUtils;
3535
import org.apache.maven.artifact.versioning.ArtifactVersion;
36-
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
3736
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
3837
import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
3938
import org.apache.maven.artifact.versioning.Restriction;
@@ -42,6 +41,7 @@
4241
import org.codehaus.mojo.versions.ordering.BoundArtifactVersion;
4342
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
4443
import org.codehaus.mojo.versions.ordering.VersionComparator;
44+
import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache;
4545

4646
import static java.util.Optional.empty;
4747
import static org.codehaus.mojo.versions.api.Segment.SUBINCREMENTAL;
@@ -306,10 +306,10 @@ public ArtifactVersion getNewestVersion(
306306
property.getVersion() != null ? VersionRange.createFromVersionSpec(property.getVersion()) : null;
307307
helper.getLog().debug("Property ${" + property.getName() + "}: Restricting results to " + range);
308308

309-
ArtifactVersion currentVersion = new DefaultArtifactVersion(versionString);
309+
ArtifactVersion currentVersion = DefaultArtifactVersionCache.of(versionString);
310310
ArtifactVersion lowerBound = allowDowngrade
311311
? getLowerBound(currentVersion, upperBoundSegment)
312-
.map(DefaultArtifactVersion::new)
312+
.map(DefaultArtifactVersionCache::of)
313313
.orElse(null)
314314
: currentVersion;
315315
if (helper.getLog().isDebugEnabled()) {

versions-common/src/main/java/org/codehaus/mojo/versions/filtering/WildcardMatcher.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import java.util.function.Predicate;
44

5-
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
65
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
76
import org.apache.maven.artifact.versioning.VersionRange;
7+
import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache;
88

99
public class WildcardMatcher implements Predicate<String> {
1010
public static final String WILDCARD = "*";
@@ -61,7 +61,7 @@ else if (pattern.startsWith("[") || pattern.startsWith("(")) {
6161

6262
private boolean isVersionIncludedInRange(final String version, final String range) {
6363
try {
64-
return VersionRange.createFromVersionSpec(range).containsVersion(new DefaultArtifactVersion(version));
64+
return VersionRange.createFromVersionSpec(range).containsVersion(DefaultArtifactVersionCache.of(version));
6565
} catch (InvalidVersionSpecificationException e) {
6666
return false;
6767
}
Lines changed: 107 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,106 @@
11
package org.codehaus.mojo.versions.ordering;
22

3-
import java.util.Iterator;
3+
import java.util.ArrayList;
44
import java.util.List;
55

66
import org.apache.commons.lang3.builder.EqualsBuilder;
77
import org.apache.commons.lang3.builder.HashCodeBuilder;
88
import org.apache.maven.artifact.versioning.ArtifactVersion;
9-
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
109
import org.codehaus.mojo.versions.api.Segment;
11-
12-
import static org.codehaus.mojo.versions.ordering.ComparableVersion.IntegerItem.ZERO;
10+
import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache;
11+
import org.codehaus.plexus.util.StringUtils;
1312

1413
/**
15-
* <p>Represents an artifact version with all segments more major or equal to a given segment
16-
* held in place. It can be thought of as an artifact having +&infin; as its upper bound
17-
* on all segments less major than the held segment.</p>
18-
* <p>When compared with another artifact versions, this results with the other object
14+
* <p>Represents an <b>immutable</b> artifact version with all segments <em>major</em> to the given segment
15+
* held in place. It can be thought of as an artifact having +∞ as its upper bound
16+
* on all segments minor to the held segment.</p>
17+
* <p>For example:</p>
18+
* <p>A {@link BoundArtifactVersion} of {@code [1.2.3-2, INCREMENTAL]} can be seen as {@code 1.2.+∞}
19+
* and will be greater than all versions matching the {@code 1.2.*} pattern.</p>
20+
* <p>A {@link BoundArtifactVersion} of {@code [1.2.3-2, SUBINCREMENTAL]} will be greater
21+
* * than all versions matching the {@code 1.2.3-2.*} pattern.</p>
22+
* <p>When compared to another artifact versions, this results with the other object
1923
* with the segment versions up to the held segment being equal,
2024
* always comparing lower than this object.</p>
2125
* <p>This is particularly helpful for -SNAPSHOT and other versions with qualifiers, which
2226
* are lower than version 0 in the Maven versioning system.</p>
2327
*/
24-
public class BoundArtifactVersion extends DefaultArtifactVersion {
28+
public class BoundArtifactVersion implements ArtifactVersion {
2529
/**
2630
* Most major segment that can change, i.e. not held in place.
2731
* All segments that are more major than this one are held in place.
2832
*/
2933
private final Segment segment;
3034

31-
private final BoundComparableVersion comparator;
35+
private final ArtifactVersion comparable;
3236

3337
/**
34-
* Constructs the instance
38+
* Constructs the instance given the version in a text format.
39+
* @param artifactVersion version in a text format
40+
* @param segment most major segment that can change, i.e. <em>not</em> held in place
41+
*/
42+
public BoundArtifactVersion(String artifactVersion, Segment segment) {
43+
this.segment = segment;
44+
StringBuilder versionBuilder = new StringBuilder();
45+
String[] segments = tokens(artifactVersion);
46+
for (int segNr = 0;
47+
segNr <= segments.length || segNr <= Segment.SUBINCREMENTAL.value();
48+
segNr++, versionBuilder.append(".")) {
49+
if (segNr < segment.value()) {
50+
versionBuilder.append(segNr < segments.length ? integerItemOrZero(segments[segNr]) : "0");
51+
} else {
52+
versionBuilder.append(Integer.MAX_VALUE);
53+
}
54+
}
55+
versionBuilder.append(Integer.MAX_VALUE);
56+
comparable = DefaultArtifactVersionCache.of(versionBuilder.toString());
57+
}
58+
59+
/**
60+
* Constructs the instance given a {@link ArtifactVersion instance}
3561
* @param artifactVersion artifact version containing the segment version values
3662
* @param segment most major segment that can change, i.e. <em>not</em> held in place
3763
*/
3864
public BoundArtifactVersion(ArtifactVersion artifactVersion, Segment segment) {
39-
super(artifactVersion.toString());
40-
this.segment = segment;
41-
this.comparator = new BoundComparableVersion(this);
65+
this(artifactVersion.toString(), segment);
66+
}
67+
68+
/**
69+
* Splits the given version string into tokens, splitting them on the {@code .} or {@code -} characters
70+
* as well as on letter/digit boundaries.
71+
* @param version version string
72+
* @return tokens of the parsed version string
73+
*/
74+
private static String[] tokens(String version) {
75+
if (version == null) {
76+
return new String[0];
77+
}
78+
List<String> result = new ArrayList<>();
79+
for (int begin = 0, end = 0; end <= version.length(); end++) {
80+
if (end == version.length()
81+
|| version.charAt(end) == '.'
82+
|| version.charAt(end) == '-'
83+
|| isTokenBoundary(version.charAt(begin), version.charAt(end))) {
84+
if (end > begin) {
85+
result.add(version.substring(begin, end));
86+
}
87+
begin = end + 1;
88+
}
89+
}
90+
return result.toArray(new String[0]);
91+
}
92+
93+
/**
94+
* @param c1 character
95+
* @param c2 another character
96+
* @return will only return {@code true} if one of the characters is a digit and the other a letter
97+
*/
98+
private static boolean isTokenBoundary(char c1, char c2) {
99+
return Character.isDigit(c1) ^ Character.isDigit(c2);
100+
}
101+
102+
private static String integerItemOrZero(String item) {
103+
return StringUtils.isNumeric(item) ? item : "0";
42104
}
43105

44106
/**
@@ -56,7 +118,7 @@ public int compareTo(ArtifactVersion other) {
56118
return -1;
57119
}
58120

59-
return comparator.compareTo(ComparableVersion.of(other.toString()));
121+
return comparable.compareTo(other);
60122
}
61123

62124
@Override
@@ -74,7 +136,7 @@ public boolean equals(Object o) {
74136
return new EqualsBuilder()
75137
.appendSuper(super.equals(o))
76138
.append(getSegment(), that.getSegment())
77-
.append(comparator, that.comparator)
139+
.append(comparable, that.comparable)
78140
.isEquals();
79141
}
80142

@@ -83,48 +145,42 @@ public int hashCode() {
83145
return new HashCodeBuilder(17, 37)
84146
.appendSuper(super.hashCode())
85147
.append(getSegment())
86-
.append(comparator)
148+
.append(comparable)
87149
.toHashCode();
88150
}
89151

90-
protected static class BoundComparableVersion extends ComparableVersion {
91-
private BoundArtifactVersion artifactVersion;
92-
93-
protected BoundComparableVersion(BoundArtifactVersion artifactVersion) {
94-
super(artifactVersion.toString());
95-
this.artifactVersion = artifactVersion;
96-
}
97-
98-
@Override
99-
public int compareTo(ComparableVersion o) {
100-
// all segments more or equally major than artifactVersion.segment can change
101-
return compareTo(
102-
((List<Item>) items).iterator(),
103-
((Iterable<Item>) o.items).iterator(),
104-
artifactVersion.segment.value());
105-
}
152+
@Override
153+
public int getMajorVersion() {
154+
return comparable.getMajorVersion();
155+
}
106156

107-
private int compareTo(Iterator<Item> left, Iterator<Item> right, int comparisonsLeft) {
108-
if (comparisonsLeft <= 0) {
109-
// always greater than the other version if all more major segments are equal
110-
return 1;
111-
}
157+
@Override
158+
public int getMinorVersion() {
159+
return comparable.getMinorVersion();
160+
}
112161

113-
int result = left.hasNext() && right.hasNext()
114-
? integerItemOrZero(left.next()).compareTo(right.next())
115-
: left.hasNext() || right.hasNext() ? compareToZero(left, right) : 1;
162+
@Override
163+
public int getIncrementalVersion() {
164+
return comparable.getIncrementalVersion();
165+
}
116166

117-
return result != 0 ? result : compareTo(left, right, comparisonsLeft - 1);
118-
}
167+
@Override
168+
public int getBuildNumber() {
169+
return comparable.getBuildNumber();
170+
}
119171

120-
private static int compareToZero(Iterator<Item> left, Iterator<Item> right) {
121-
return left.hasNext()
122-
? integerItemOrZero(left.next()).compareTo(ZERO)
123-
: -right.next().compareTo(ZERO);
124-
}
172+
@Override
173+
public String getQualifier() {
174+
return comparable.getQualifier();
175+
}
125176

126-
private static Item integerItemOrZero(Item item) {
127-
return item instanceof IntegerItem ? item : ZERO;
128-
}
177+
/**
178+
* @deprecated do not use: this method would mutate the state and therefore is illegal to use
179+
* @throws UnsupportedOperationException thrown if the method is called
180+
*/
181+
@Override
182+
@Deprecated
183+
public void parseVersion(String version) {
184+
throw new UnsupportedOperationException();
129185
}
130186
}

0 commit comments

Comments
 (0)