diff --git a/pom.xml b/pom.xml index 8054a918e5..507a2bff93 100644 --- a/pom.xml +++ b/pom.xml @@ -267,7 +267,11 @@ commons-text 1.10.0 - + + org.apache.commons + commons-collections4 + 4.4 + org.codehaus.plexus plexus-utils diff --git a/versions-common/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java b/versions-common/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java index 21731cb716..02f7bae7d1 100644 --- a/versions-common/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java +++ b/versions-common/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java @@ -31,13 +31,13 @@ import org.apache.maven.artifact.ArtifactUtils; import org.apache.maven.artifact.versioning.ArtifactVersion; -import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; import org.apache.maven.artifact.versioning.Restriction; import org.apache.maven.artifact.versioning.VersionRange; import org.codehaus.mojo.versions.ordering.BoundArtifactVersion; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.ordering.VersionComparator; +import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache; import static java.util.Collections.reverseOrder; import static java.util.Optional.empty; @@ -123,7 +123,7 @@ public final void setCurrentVersion(ArtifactVersion currentVersion) { @Override public final void setCurrentVersion(String currentVersion) { - setCurrentVersion(currentVersion == null ? null : new DefaultArtifactVersion(currentVersion)); + setCurrentVersion(currentVersion == null ? null : DefaultArtifactVersionCache.of(currentVersion)); } @Override @@ -194,7 +194,7 @@ private ArtifactVersion[] getNewerVersions(ArtifactVersion version, boolean incl @Override public final ArtifactVersion[] getNewerVersions(String version, boolean includeSnapshots) { - return getNewerVersions(new DefaultArtifactVersion(version), includeSnapshots); + return getNewerVersions(DefaultArtifactVersionCache.of(version), includeSnapshots); } @Deprecated @@ -209,10 +209,10 @@ public final ArtifactVersion[] getNewerVersions( public final ArtifactVersion[] getNewerVersions( String versionString, Optional unchangedSegment, boolean includeSnapshots, boolean allowDowngrade) throws InvalidSegmentException { - ArtifactVersion currentVersion = new DefaultArtifactVersion(versionString); + ArtifactVersion currentVersion = DefaultArtifactVersionCache.of(versionString); ArtifactVersion lowerBound = allowDowngrade ? getLowerBound(currentVersion, unchangedSegment) - .map(DefaultArtifactVersion::new) + .map(DefaultArtifactVersionCache::of) .orElse(null) : currentVersion; ArtifactVersion upperBound = unchangedSegment @@ -228,10 +228,10 @@ public final ArtifactVersion[] getNewerVersions( public Optional getNewestVersion( String versionString, Optional upperBoundSegment, boolean includeSnapshots, boolean allowDowngrade) throws InvalidSegmentException { - ArtifactVersion currentVersion = new DefaultArtifactVersion(versionString); + ArtifactVersion currentVersion = DefaultArtifactVersionCache.of(versionString); ArtifactVersion lowerBound = allowDowngrade ? getLowerBound(currentVersion, upperBoundSegment) - .map(DefaultArtifactVersion::new) + .map(DefaultArtifactVersionCache::of) .orElse(null) : currentVersion; ArtifactVersion upperBound = upperBoundSegment diff --git a/versions-common/src/main/java/org/codehaus/mojo/versions/api/DefaultVersionsHelper.java b/versions-common/src/main/java/org/codehaus/mojo/versions/api/DefaultVersionsHelper.java index 5d3850f70d..d3844285c1 100644 --- a/versions-common/src/main/java/org/codehaus/mojo/versions/api/DefaultVersionsHelper.java +++ b/versions-common/src/main/java/org/codehaus/mojo/versions/api/DefaultVersionsHelper.java @@ -52,7 +52,6 @@ import org.apache.maven.artifact.ArtifactUtils; import org.apache.maven.artifact.resolver.ArtifactResolutionException; import org.apache.maven.artifact.versioning.ArtifactVersion; -import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.apache.maven.artifact.versioning.Restriction; import org.apache.maven.artifact.versioning.VersionRange; import org.apache.maven.execution.MavenSession; @@ -73,6 +72,7 @@ import org.codehaus.mojo.versions.model.io.xpp3.RuleXpp3Reader; import org.codehaus.mojo.versions.ordering.VersionComparator; import org.codehaus.mojo.versions.ordering.VersionComparators; +import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache; import org.codehaus.mojo.versions.utils.DependencyBuilder; import org.codehaus.mojo.versions.utils.DependencyComparator; import org.codehaus.mojo.versions.utils.PluginComparator; @@ -228,7 +228,7 @@ public ArtifactVersions lookupArtifactVersions( return false; })) - .map(v -> new DefaultArtifactVersion(v.toString())) + .map(v -> DefaultArtifactVersionCache.of(v.toString())) .collect(Collectors.toList()), getVersionComparator(artifact)); } catch (VersionRangeResolutionException e) { @@ -425,7 +425,7 @@ public Set extractArtifacts(Collection mavenProjects) { @Override public ArtifactVersion createArtifactVersion(String version) { - return new DefaultArtifactVersion(version); + return DefaultArtifactVersionCache.of(version); } @Override diff --git a/versions-common/src/main/java/org/codehaus/mojo/versions/api/PropertyVersions.java b/versions-common/src/main/java/org/codehaus/mojo/versions/api/PropertyVersions.java index eecff4b98a..b61b47990d 100644 --- a/versions-common/src/main/java/org/codehaus/mojo/versions/api/PropertyVersions.java +++ b/versions-common/src/main/java/org/codehaus/mojo/versions/api/PropertyVersions.java @@ -33,7 +33,6 @@ import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.ArtifactUtils; import org.apache.maven.artifact.versioning.ArtifactVersion; -import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; import org.apache.maven.artifact.versioning.OverConstrainedVersionException; import org.apache.maven.artifact.versioning.Restriction; @@ -42,6 +41,7 @@ import org.codehaus.mojo.versions.ordering.BoundArtifactVersion; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.ordering.VersionComparator; +import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache; import static java.util.Optional.empty; import static org.codehaus.mojo.versions.api.Segment.SUBINCREMENTAL; @@ -306,10 +306,10 @@ public ArtifactVersion getNewestVersion( property.getVersion() != null ? VersionRange.createFromVersionSpec(property.getVersion()) : null; helper.getLog().debug("Property ${" + property.getName() + "}: Restricting results to " + range); - ArtifactVersion currentVersion = new DefaultArtifactVersion(versionString); + ArtifactVersion currentVersion = DefaultArtifactVersionCache.of(versionString); ArtifactVersion lowerBound = allowDowngrade ? getLowerBound(currentVersion, upperBoundSegment) - .map(DefaultArtifactVersion::new) + .map(DefaultArtifactVersionCache::of) .orElse(null) : currentVersion; if (helper.getLog().isDebugEnabled()) { diff --git a/versions-common/src/main/java/org/codehaus/mojo/versions/filtering/WildcardMatcher.java b/versions-common/src/main/java/org/codehaus/mojo/versions/filtering/WildcardMatcher.java index 5b4905636f..17b466c2c4 100644 --- a/versions-common/src/main/java/org/codehaus/mojo/versions/filtering/WildcardMatcher.java +++ b/versions-common/src/main/java/org/codehaus/mojo/versions/filtering/WildcardMatcher.java @@ -2,9 +2,9 @@ import java.util.function.Predicate; -import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; import org.apache.maven.artifact.versioning.VersionRange; +import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache; public class WildcardMatcher implements Predicate { public static final String WILDCARD = "*"; @@ -61,7 +61,7 @@ else if (pattern.startsWith("[") || pattern.startsWith("(")) { private boolean isVersionIncludedInRange(final String version, final String range) { try { - return VersionRange.createFromVersionSpec(range).containsVersion(new DefaultArtifactVersion(version)); + return VersionRange.createFromVersionSpec(range).containsVersion(DefaultArtifactVersionCache.of(version)); } catch (InvalidVersionSpecificationException e) { return false; } diff --git a/versions-common/src/main/java/org/codehaus/mojo/versions/ordering/BoundArtifactVersion.java b/versions-common/src/main/java/org/codehaus/mojo/versions/ordering/BoundArtifactVersion.java index 981c3ac3ff..f7f1279139 100644 --- a/versions-common/src/main/java/org/codehaus/mojo/versions/ordering/BoundArtifactVersion.java +++ b/versions-common/src/main/java/org/codehaus/mojo/versions/ordering/BoundArtifactVersion.java @@ -1,44 +1,106 @@ package org.codehaus.mojo.versions.ordering; -import java.util.Iterator; +import java.util.ArrayList; import java.util.List; import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder; import org.apache.maven.artifact.versioning.ArtifactVersion; -import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.codehaus.mojo.versions.api.Segment; - -import static org.codehaus.mojo.versions.ordering.ComparableVersion.IntegerItem.ZERO; +import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache; +import org.codehaus.plexus.util.StringUtils; /** - *

Represents an artifact version with all segments more major or equal to a given segment - * held in place. It can be thought of as an artifact having +∞ as its upper bound - * on all segments less major than the held segment.

- *

When compared with another artifact versions, this results with the other object + *

Represents an immutable artifact version with all segments major to the given segment + * held in place. It can be thought of as an artifact having +∞ as its upper bound + * on all segments minor to the held segment.

+ *

For example:

+ *

A {@link BoundArtifactVersion} of {@code [1.2.3-2, INCREMENTAL]} can be seen as {@code 1.2.+∞} + * and will be greater than all versions matching the {@code 1.2.*} pattern.

+ *

A {@link BoundArtifactVersion} of {@code [1.2.3-2, SUBINCREMENTAL]} will be greater + * * than all versions matching the {@code 1.2.3-2.*} pattern.

+ *

When compared to another artifact versions, this results with the other object * with the segment versions up to the held segment being equal, * always comparing lower than this object.

*

This is particularly helpful for -SNAPSHOT and other versions with qualifiers, which * are lower than version 0 in the Maven versioning system.

*/ -public class BoundArtifactVersion extends DefaultArtifactVersion { +public class BoundArtifactVersion implements ArtifactVersion { /** * Most major segment that can change, i.e. not held in place. * All segments that are more major than this one are held in place. */ private final Segment segment; - private final BoundComparableVersion comparator; + private final ArtifactVersion comparable; /** - * Constructs the instance + * Constructs the instance given the version in a text format. + * @param artifactVersion version in a text format + * @param segment most major segment that can change, i.e. not held in place + */ + public BoundArtifactVersion(String artifactVersion, Segment segment) { + this.segment = segment; + StringBuilder versionBuilder = new StringBuilder(); + String[] segments = tokens(artifactVersion); + for (int segNr = 0; + segNr <= segments.length || segNr <= Segment.SUBINCREMENTAL.value(); + segNr++, versionBuilder.append(".")) { + if (segNr < segment.value()) { + versionBuilder.append(segNr < segments.length ? integerItemOrZero(segments[segNr]) : "0"); + } else { + versionBuilder.append(Integer.MAX_VALUE); + } + } + versionBuilder.append(Integer.MAX_VALUE); + comparable = DefaultArtifactVersionCache.of(versionBuilder.toString()); + } + + /** + * Constructs the instance given a {@link ArtifactVersion instance} * @param artifactVersion artifact version containing the segment version values * @param segment most major segment that can change, i.e. not held in place */ public BoundArtifactVersion(ArtifactVersion artifactVersion, Segment segment) { - super(artifactVersion.toString()); - this.segment = segment; - this.comparator = new BoundComparableVersion(this); + this(artifactVersion.toString(), segment); + } + + /** + * Splits the given version string into tokens, splitting them on the {@code .} or {@code -} characters + * as well as on letter/digit boundaries. + * @param version version string + * @return tokens of the parsed version string + */ + private static String[] tokens(String version) { + if (version == null) { + return new String[0]; + } + List result = new ArrayList<>(); + for (int begin = 0, end = 0; end <= version.length(); end++) { + if (end == version.length() + || version.charAt(end) == '.' + || version.charAt(end) == '-' + || isTokenBoundary(version.charAt(begin), version.charAt(end))) { + if (end > begin) { + result.add(version.substring(begin, end)); + } + begin = end + 1; + } + } + return result.toArray(new String[0]); + } + + /** + * @param c1 character + * @param c2 another character + * @return will only return {@code true} if one of the characters is a digit and the other a letter + */ + private static boolean isTokenBoundary(char c1, char c2) { + return Character.isDigit(c1) ^ Character.isDigit(c2); + } + + private static String integerItemOrZero(String item) { + return StringUtils.isNumeric(item) ? item : "0"; } /** @@ -56,7 +118,7 @@ public int compareTo(ArtifactVersion other) { return -1; } - return comparator.compareTo(ComparableVersion.of(other.toString())); + return comparable.compareTo(other); } @Override @@ -74,7 +136,7 @@ public boolean equals(Object o) { return new EqualsBuilder() .appendSuper(super.equals(o)) .append(getSegment(), that.getSegment()) - .append(comparator, that.comparator) + .append(comparable, that.comparable) .isEquals(); } @@ -83,48 +145,42 @@ public int hashCode() { return new HashCodeBuilder(17, 37) .appendSuper(super.hashCode()) .append(getSegment()) - .append(comparator) + .append(comparable) .toHashCode(); } - protected static class BoundComparableVersion extends ComparableVersion { - private BoundArtifactVersion artifactVersion; - - protected BoundComparableVersion(BoundArtifactVersion artifactVersion) { - super(artifactVersion.toString()); - this.artifactVersion = artifactVersion; - } - - @Override - public int compareTo(ComparableVersion o) { - // all segments more or equally major than artifactVersion.segment can change - return compareTo( - ((List) items).iterator(), - ((Iterable) o.items).iterator(), - artifactVersion.segment.value()); - } + @Override + public int getMajorVersion() { + return comparable.getMajorVersion(); + } - private int compareTo(Iterator left, Iterator right, int comparisonsLeft) { - if (comparisonsLeft <= 0) { - // always greater than the other version if all more major segments are equal - return 1; - } + @Override + public int getMinorVersion() { + return comparable.getMinorVersion(); + } - int result = left.hasNext() && right.hasNext() - ? integerItemOrZero(left.next()).compareTo(right.next()) - : left.hasNext() || right.hasNext() ? compareToZero(left, right) : 1; + @Override + public int getIncrementalVersion() { + return comparable.getIncrementalVersion(); + } - return result != 0 ? result : compareTo(left, right, comparisonsLeft - 1); - } + @Override + public int getBuildNumber() { + return comparable.getBuildNumber(); + } - private static int compareToZero(Iterator left, Iterator right) { - return left.hasNext() - ? integerItemOrZero(left.next()).compareTo(ZERO) - : -right.next().compareTo(ZERO); - } + @Override + public String getQualifier() { + return comparable.getQualifier(); + } - private static Item integerItemOrZero(Item item) { - return item instanceof IntegerItem ? item : ZERO; - } + /** + * @deprecated do not use: this method would mutate the state and therefore is illegal to use + * @throws UnsupportedOperationException thrown if the method is called + */ + @Override + @Deprecated + public void parseVersion(String version) { + throw new UnsupportedOperationException(); } } diff --git a/versions-common/src/main/java/org/codehaus/mojo/versions/ordering/ComparableVersion.java b/versions-common/src/main/java/org/codehaus/mojo/versions/ordering/ComparableVersion.java deleted file mode 100644 index fe5a5f0190..0000000000 --- a/versions-common/src/main/java/org/codehaus/mojo/versions/ordering/ComparableVersion.java +++ /dev/null @@ -1,417 +0,0 @@ -package org.codehaus.mojo.versions.ordering; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import java.math.BigInteger; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Iterator; -import java.util.List; -import java.util.ListIterator; -import java.util.Locale; -import java.util.Map; -import java.util.Properties; -import java.util.Stack; -import java.util.concurrent.locks.ReentrantReadWriteLock; - -import org.apache.commons.collections4.map.LRUMap; - -/** - * Generic implementation of version comparison. - * - * @author Kenney Westerhof - * @author Herve Boutemy - * Note: The implementation of the maven core should be used. - */ -public class ComparableVersion implements Comparable { - private static final int MAX_CACHE_SIZE = 0x200; - private static final Map CACHE = new LRUMap<>(MAX_CACHE_SIZE); - private static final ReentrantReadWriteLock CACHE_LOCK = new ReentrantReadWriteLock(); - - private String value; - - private String canonical; - - protected ListItem items; - - protected interface Item { - int INTEGER_ITEM = 0; - - int STRING_ITEM = 1; - - int LIST_ITEM = 2; - - int compareTo(Item item); - - int getType(); - - boolean isNull(); - } - - /** - * Represents a numeric item in the version item list. - */ - protected static class IntegerItem implements Item { - private static final BigInteger BIG_INTEGER_ZERO = new BigInteger("0"); - - private final BigInteger value; - - public static final IntegerItem ZERO = new IntegerItem(); - - private IntegerItem() { - this.value = BIG_INTEGER_ZERO; - } - - IntegerItem(String str) { - this.value = new BigInteger(str); - } - - public int getType() { - return INTEGER_ITEM; - } - - public boolean isNull() { - return BIG_INTEGER_ZERO.equals(value); - } - - public int compareTo(Item item) { - if (item == null) { - return BIG_INTEGER_ZERO.equals(value) ? 0 : 1; // 1.0 == 1, 1.1 > 1 - } - - switch (item.getType()) { - case INTEGER_ITEM: - return value.compareTo(((IntegerItem) item).value); - - case STRING_ITEM: - return 1; // 1.1 > 1-sp - - case LIST_ITEM: - return 1; // 1.1 > 1-1 - - default: - throw new RuntimeException("invalid item: " + item.getClass()); - } - } - - public String toString() { - return value.toString(); - } - } - - /** - * Represents a string in the version item list, usually a qualifier. - */ - private static class StringItem implements Item { - private static final String[] QUALIFIERS = {"snapshot", "alpha", "beta", "milestone", "preview", "rc", "", "sp" - }; - - private static final List QUALIFIERS_LIST = Arrays.asList(QUALIFIERS); - - private static final Properties ALIASES = new Properties(); - - static { - ALIASES.put("mr", "milestone"); - ALIASES.put("cr", "rc"); - ALIASES.put("final", ""); - ALIASES.put("ga", ""); - } - - /** - * A comparable for the empty-string qualifier. This one is used to determine if a given qualifier makes the - * version older than one without a qualifier, or more recent. - */ - private static final Comparable RELEASE_VERSION_INDEX = String.valueOf(QUALIFIERS_LIST.indexOf("")); - - private final String value; - - StringItem(String value, boolean followedByDigit) { - if (followedByDigit && value.length() == 1) { - // a1 = alpha-1, b1 = beta-1, m1 = milestone-1 - switch (value.charAt(0)) { - case 'a': - value = "alpha"; - break; - case 'b': - value = "beta"; - break; - case 'm': - value = "milestone"; - break; - default: - // no action - break; - } - } - this.value = ALIASES.getProperty(value, value); - } - - public int getType() { - return STRING_ITEM; - } - - public boolean isNull() { - return (comparableQualifier(value).compareTo(RELEASE_VERSION_INDEX) == 0); - } - - /** - * Returns a comparable for a qualifier. - *

- * This method both takes into account the ordering of known qualifiers as well as lexical ordering for unknown - * qualifiers. - *

- * just returning an Integer with the index here is faster, but requires a lot of if/then/else to check for -1 - * or QUALIFIERS.size and then resort to lexical ordering. Most comparisons are decided by the first character, - * so this is still fast. If more characters are needed then it requires a lexical sort anyway. - * - * @param qualifier - * @return - */ - public static Comparable comparableQualifier(String qualifier) { - int i = QUALIFIERS_LIST.indexOf(qualifier); - - return i == -1 ? QUALIFIERS_LIST.size() + "-" + qualifier : String.valueOf(i); - } - - public int compareTo(Item item) { - if (item == null) { - // 1-rc < 1, 1-ga > 1 - return comparableQualifier(value).compareTo(RELEASE_VERSION_INDEX); - } - switch (item.getType()) { - case INTEGER_ITEM: - return -1; // 1.any < 1.1 ? - - case STRING_ITEM: - return comparableQualifier(value).compareTo(comparableQualifier(((StringItem) item).value)); - - case LIST_ITEM: - return -1; // 1.any < 1-1 - - default: - throw new RuntimeException("invalid item: " + item.getClass()); - } - } - - public String toString() { - return value; - } - } - - /** - * Represents a version list item. This class is used both for the global item list and for sub-lists (which start - * with '-(number)' in the version specification). - */ - private static class ListItem extends ArrayList implements Item { - public int getType() { - return LIST_ITEM; - } - - public boolean isNull() { - return (size() == 0); - } - - void normalize() { - for (ListIterator iterator = listIterator(size()); iterator.hasPrevious(); ) { - Item item = iterator.previous(); - if (item.isNull()) { - iterator.remove(); // remove null trailing items: 0, "", empty list - } else { - break; - } - } - } - - public int compareTo(Item item) { - if (item == null) { - if (size() == 0) { - return 0; // 1-0 = 1- (normalize) = 1 - } - Item first = get(0); - return first.compareTo(null); - } - - switch (item.getType()) { - case INTEGER_ITEM: - return -1; // 1-1 < 1.0.x - - case STRING_ITEM: - return 1; // 1-1 > 1-sp - - case LIST_ITEM: - Iterator left = iterator(); - Iterator right = ((ListItem) item).iterator(); - - while (left.hasNext() || right.hasNext()) { - Item l = left.hasNext() ? left.next() : null; - Item r = right.hasNext() ? right.next() : null; - - // if this is shorter, then invert the compare and mul with -1 - int result = l == null ? -1 * r.compareTo(l) : l.compareTo(r); - - if (result != 0) { - return result; - } - } - - return 0; - - default: - throw new RuntimeException("invalid item: " + item.getClass()); - } - } - - public String toString() { - StringBuilder buffer = new StringBuilder("("); - for (Iterator iter = iterator(); iter.hasNext(); ) { - buffer.append(iter.next()); - if (iter.hasNext()) { - buffer.append(','); - } - } - buffer.append(')'); - return buffer.toString(); - } - } - - /** - * Get a ComparableVersion representing the version in a string. - */ - public static ComparableVersion of(String version) { - try { - CACHE_LOCK.readLock().lock(); - ComparableVersion result = CACHE.get(version); - if (result != null) { - return result; - } - } finally { - CACHE_LOCK.readLock().unlock(); - } - try { - CACHE_LOCK.writeLock().lock(); - return CACHE.computeIfAbsent(version, ComparableVersion::new); - } finally { - CACHE_LOCK.writeLock().unlock(); - } - } - - /** - * Create a ComparableVersion from a string. Try to avoid using this and instead use the cache by calling {@link #of(String)} - */ - protected ComparableVersion(String version) { - parseVersion(version); - } - - public final void parseVersion(String version) { - this.value = version; - - items = new ListItem(); - - version = version.toLowerCase(Locale.ENGLISH); - - ListItem list = items; - - Stack stack = new Stack<>(); - stack.push(list); - - boolean isDigit = false; - - int startIndex = 0; - - for (int i = 0; i < version.length(); i++) { - char c = version.charAt(i); - - if (c == '.') { - if (i == startIndex) { - list.add(IntegerItem.ZERO); - } else { - list.add(parseItem(isDigit, version.substring(startIndex, i))); - } - startIndex = i + 1; - } else if (c == '-') { - if (i == startIndex) { - list.add(IntegerItem.ZERO); - } else { - list.add(parseItem(isDigit, version.substring(startIndex, i))); - } - startIndex = i + 1; - - if (isDigit) { - list.normalize(); // 1.0-* = 1-* - - if ((i + 1 < version.length()) && Character.isDigit(version.charAt(i + 1))) { - // new ListItem only if previous were digits and new char is a digit, - // ie need to differentiate only 1.1 from 1-1 - // CHECKSTYLE_OFF: InnerAssignment - list.add(list = new ListItem()); - // CHECKSTYLE_ON: InnerAssignment - - stack.push(list); - } - } - } else if (Character.isDigit(c)) { - if (!isDigit && i > startIndex) { - list.add(new StringItem(version.substring(startIndex, i), true)); - startIndex = i; - } - - isDigit = true; - } else { - if (isDigit && i > startIndex) { - list.add(parseItem(true, version.substring(startIndex, i))); - startIndex = i; - } - - isDigit = false; - } - } - - if (version.length() > startIndex) { - list.add(parseItem(isDigit, version.substring(startIndex))); - } - - while (!stack.isEmpty()) { - list = stack.pop(); - list.normalize(); - } - - canonical = items.toString(); - } - - private static Item parseItem(boolean isDigit, String buf) { - return isDigit ? new IntegerItem(buf) : new StringItem(buf, false); - } - - public int compareTo(ComparableVersion o) { - return items.compareTo((o).items); - } - - public String toString() { - return value; - } - - public boolean equals(Object o) { - return (o instanceof ComparableVersion) && canonical.equals(((ComparableVersion) o).canonical); - } - - public int hashCode() { - return canonical.hashCode(); - } -} diff --git a/versions-common/src/main/java/org/codehaus/mojo/versions/ordering/MavenVersionComparator.java b/versions-common/src/main/java/org/codehaus/mojo/versions/ordering/MavenVersionComparator.java index 96b0140c65..b9647b5562 100644 --- a/versions-common/src/main/java/org/codehaus/mojo/versions/ordering/MavenVersionComparator.java +++ b/versions-common/src/main/java/org/codehaus/mojo/versions/ordering/MavenVersionComparator.java @@ -20,8 +20,9 @@ */ import org.apache.maven.artifact.versioning.ArtifactVersion; -import org.apache.maven.artifact.versioning.DefaultArtifactVersion; +import org.apache.maven.artifact.versioning.ComparableVersion; import org.codehaus.mojo.versions.api.Segment; +import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache; import org.codehaus.plexus.util.StringUtils; /** @@ -39,7 +40,7 @@ public int compare(ArtifactVersion o1, ArtifactVersion o2) { if (o1 instanceof BoundArtifactVersion) { return o1.compareTo(o2); } - return ComparableVersion.of(o1.toString()).compareTo(ComparableVersion.of(o2.toString())); + return new ComparableVersion(o1.toString()).compareTo(new ComparableVersion(o2.toString())); } /** @@ -89,7 +90,7 @@ protected ArtifactVersion innerIncrementSegment(ArtifactVersion v, Segment segme if (innerGetSegmentCount(v) == 1) { // only the qualifier version = VersionComparators.alphaNumIncrement(version); - return new DefaultArtifactVersion(version); + return DefaultArtifactVersionCache.of(version); } else { int major = v.getMajorVersion(); int minor = v.getMinorVersion(); @@ -154,7 +155,7 @@ protected ArtifactVersion innerIncrementSegment(ArtifactVersion v, Segment segme result.append('-'); result.append(build); } - return new DefaultArtifactVersion(result.toString()); + return DefaultArtifactVersionCache.of(result.toString()); } } diff --git a/versions-common/src/main/java/org/codehaus/mojo/versions/ordering/MercuryVersionComparator.java b/versions-common/src/main/java/org/codehaus/mojo/versions/ordering/MercuryVersionComparator.java index 8ee8809281..b7c526ca46 100644 --- a/versions-common/src/main/java/org/codehaus/mojo/versions/ordering/MercuryVersionComparator.java +++ b/versions-common/src/main/java/org/codehaus/mojo/versions/ordering/MercuryVersionComparator.java @@ -23,8 +23,9 @@ import java.util.StringTokenizer; import org.apache.maven.artifact.versioning.ArtifactVersion; -import org.apache.maven.artifact.versioning.DefaultArtifactVersion; +import org.apache.maven.artifact.versioning.ComparableVersion; import org.codehaus.mojo.versions.api.Segment; +import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache; /** * A comparator which uses Mercury's version rules. @@ -40,7 +41,7 @@ public class MercuryVersionComparator extends AbstractVersionComparator { * {@inheritDoc} */ public int compare(ArtifactVersion o1, ArtifactVersion o2) { - return ComparableVersion.of(o1.toString()).compareTo(ComparableVersion.of(o2.toString())); + return new ComparableVersion(o1.toString()).compareTo(new ComparableVersion(o2.toString())); } protected int innerGetSegmentCount(ArtifactVersion v) { @@ -114,6 +115,6 @@ protected ArtifactVersion innerIncrementSegment(ArtifactVersion v, Segment segme index++; } } - return new DefaultArtifactVersion(result.toString()); + return DefaultArtifactVersionCache.of(result.toString()); } } diff --git a/versions-common/src/main/java/org/codehaus/mojo/versions/ordering/NumericVersionComparator.java b/versions-common/src/main/java/org/codehaus/mojo/versions/ordering/NumericVersionComparator.java index 71172f288b..dee1194ca7 100644 --- a/versions-common/src/main/java/org/codehaus/mojo/versions/ordering/NumericVersionComparator.java +++ b/versions-common/src/main/java/org/codehaus/mojo/versions/ordering/NumericVersionComparator.java @@ -23,8 +23,8 @@ import java.util.StringTokenizer; import org.apache.maven.artifact.versioning.ArtifactVersion; -import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.codehaus.mojo.versions.api.Segment; +import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache; /** * A comparator which will compare all segments of a dot separated version string as numbers if possible, i.e. 1.3.34 @@ -243,6 +243,6 @@ protected ArtifactVersion innerIncrementSegment(ArtifactVersion v, Segment segme tok.nextToken(); buf.append("0"); } - return new DefaultArtifactVersion(buf.toString()); + return DefaultArtifactVersionCache.of(buf.toString()); } } diff --git a/versions-common/src/main/java/org/codehaus/mojo/versions/ordering/VersionComparators.java b/versions-common/src/main/java/org/codehaus/mojo/versions/ordering/VersionComparators.java index 9e00ab02a7..2cc04e1698 100644 --- a/versions-common/src/main/java/org/codehaus/mojo/versions/ordering/VersionComparators.java +++ b/versions-common/src/main/java/org/codehaus/mojo/versions/ordering/VersionComparators.java @@ -23,7 +23,7 @@ import java.util.regex.Pattern; import org.apache.maven.artifact.versioning.ArtifactVersion; -import org.apache.maven.artifact.versioning.DefaultArtifactVersion; +import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache; /** * Utility. @@ -120,7 +120,7 @@ static ArtifactVersion stripSnapshot(ArtifactVersion v) { final String version = v.toString(); final Matcher matcher = SNAPSHOT_PATTERN.matcher(version); if (matcher.find()) { - return new DefaultArtifactVersion(version.substring(0, matcher.start(1) - 1)); + return DefaultArtifactVersionCache.of(version.substring(0, matcher.start(1) - 1)); } return v; } @@ -131,9 +131,9 @@ static ArtifactVersion copySnapshot(ArtifactVersion source, ArtifactVersion dest } final Matcher matcher = SNAPSHOT_PATTERN.matcher(source.toString()); if (matcher.find()) { - return new DefaultArtifactVersion(destination.toString() + "-" + matcher.group(0)); + return DefaultArtifactVersionCache.of(destination.toString() + "-" + matcher.group(0)); } else { - return new DefaultArtifactVersion(destination.toString() + "-SNAPSHOT"); + return DefaultArtifactVersionCache.of(destination.toString() + "-SNAPSHOT"); } } } diff --git a/versions-common/src/main/java/org/codehaus/mojo/versions/utils/DefaultArtifactVersionCache.java b/versions-common/src/main/java/org/codehaus/mojo/versions/utils/DefaultArtifactVersionCache.java new file mode 100644 index 0000000000..14d6cdff93 --- /dev/null +++ b/versions-common/src/main/java/org/codehaus/mojo/versions/utils/DefaultArtifactVersionCache.java @@ -0,0 +1,55 @@ +package org.codehaus.mojo.versions.utils; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.util.Map; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +import org.apache.commons.collections4.map.LRUMap; +import org.apache.maven.artifact.versioning.DefaultArtifactVersion; + +/** + * Simple cache for {@link org.apache.maven.artifact.versioning.ArtifactVersion} + */ +public class DefaultArtifactVersionCache { + private static final int MAX_CACHE_SIZE = 0x200; + private static final Map CACHE = new LRUMap<>(MAX_CACHE_SIZE); + private static final ReentrantReadWriteLock CACHE_LOCK = new ReentrantReadWriteLock(); + + /** + * Get a ComparableVersion representing the version in a string. + */ + public static DefaultArtifactVersion of(String version) { + try { + CACHE_LOCK.readLock().lock(); + DefaultArtifactVersion result = CACHE.get(version); + if (result != null) { + return result; + } + } finally { + CACHE_LOCK.readLock().unlock(); + } + try { + CACHE_LOCK.writeLock().lock(); + return CACHE.computeIfAbsent(version, DefaultArtifactVersion::new); + } finally { + CACHE_LOCK.writeLock().unlock(); + } + } +} diff --git a/versions-common/src/test/java/org/codehaus/mojo/versions/api/ArtifactVersionsTest.java b/versions-common/src/test/java/org/codehaus/mojo/versions/api/ArtifactVersionsTest.java index a3481223e7..6f1342d6d1 100644 --- a/versions-common/src/test/java/org/codehaus/mojo/versions/api/ArtifactVersionsTest.java +++ b/versions-common/src/test/java/org/codehaus/mojo/versions/api/ArtifactVersionsTest.java @@ -24,12 +24,12 @@ import org.apache.maven.artifact.DefaultArtifact; import org.apache.maven.artifact.handler.DefaultArtifactHandler; import org.apache.maven.artifact.versioning.ArtifactVersion; -import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.apache.maven.artifact.versioning.Restriction; import org.apache.maven.artifact.versioning.VersionRange; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.ordering.MavenVersionComparator; import org.codehaus.mojo.versions.ordering.MercuryVersionComparator; +import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache; import org.junit.Test; import static java.util.Optional.of; @@ -72,30 +72,31 @@ public void test4DigitVersions() throws Exception { arrayContaining(versions( "1.0.0.1", "1.0.0.2", "1.1.1", "2.0.0-SNAPSHOT", "2.100.0.1", "2.121.2.1", "3.1.0.1"))); assertThat( - instance.getVersions(new DefaultArtifactVersion("1.1"), null, false), + instance.getVersions(DefaultArtifactVersionCache.of("1.1"), null, false), arrayContaining(versions("1.1.1", "2.100.0.1", "2.121.2.1", "3.1.0.1"))); assertThat( - instance.getVersions(new DefaultArtifactVersion("1.1"), null, true), + instance.getVersions(DefaultArtifactVersionCache.of("1.1"), null, true), arrayContaining(versions("1.1.1", "2.0.0-SNAPSHOT", "2.100.0.1", "2.121.2.1", "3.1.0.1"))); assertThat( - instance.getVersions(new DefaultArtifactVersion("1.0.0.2"), null, false), + instance.getVersions(DefaultArtifactVersionCache.of("1.0.0.2"), null, false), // Matchers.arrayContaining(versions("1.1.1", "2.121.2.1", "2.100.0.1", "3.1.0.1"))); arrayContaining(versions("1.1.1", "2.100.0.1", "2.121.2.1", "3.1.0.1"))); assertThat( - instance.getVersions(new DefaultArtifactVersion("1.0.0.2"), null, true), + instance.getVersions(DefaultArtifactVersionCache.of("1.0.0.2"), null, true), // Matchers.arrayContaining(versions("1.1.1", "2.121.2.1", "2.100.0.1", "3.1.0.1"))); arrayContaining(versions("1.1.1", "2.0.0-SNAPSHOT", "2.100.0.1", "2.121.2.1", "3.1.0.1"))); assertEquals( - new DefaultArtifactVersion("2.121.2.1"), - instance.getNewestVersion(new DefaultArtifactVersion("1.0"), new DefaultArtifactVersion("3.0"), false)); + DefaultArtifactVersionCache.of("2.121.2.1"), + instance.getNewestVersion( + DefaultArtifactVersionCache.of("1.0"), DefaultArtifactVersionCache.of("3.0"), false)); assertNull(instance.getNewestVersion( - new DefaultArtifactVersion("1.1.1"), new DefaultArtifactVersion("2.0"), false)); + DefaultArtifactVersionCache.of("1.1.1"), DefaultArtifactVersionCache.of("2.0"), false)); assertEquals( - new DefaultArtifactVersion("2.0.0-SNAPSHOT"), + DefaultArtifactVersionCache.of("2.0.0-SNAPSHOT"), instance.getNewestVersion( - new DefaultArtifactVersion("1.1.1"), new DefaultArtifactVersion("2.0"), true)); + DefaultArtifactVersionCache.of("1.1.1"), DefaultArtifactVersionCache.of("2.0"), true)); } @Test @@ -131,20 +132,21 @@ public void testSmokes() throws Exception { assertEquals("artifact", instance.getArtifactId()); assertEquals("group", instance.getGroupId()); assertArrayEquals(versions("1.0", "1.0.1", "1.1", "3.0"), instance.getVersions(true)); - assertArrayEquals(versions("3.0"), instance.getVersions(new DefaultArtifactVersion("1.1"), null, true)); + assertArrayEquals(versions("3.0"), instance.getVersions(DefaultArtifactVersionCache.of("1.1"), null, true)); assertArrayEquals( - versions("1.1", "3.0"), instance.getVersions(new DefaultArtifactVersion("1.0.1"), null, true)); + versions("1.1", "3.0"), instance.getVersions(DefaultArtifactVersionCache.of("1.0.1"), null, true)); assertEquals( - new DefaultArtifactVersion("1.1"), - instance.getNewestVersion(new DefaultArtifactVersion("1.0"), new DefaultArtifactVersion("3.0"), true)); - assertNull( - instance.getNewestVersion(new DefaultArtifactVersion("1.1"), new DefaultArtifactVersion("3.0"), true)); + DefaultArtifactVersionCache.of("1.1"), + instance.getNewestVersion( + DefaultArtifactVersionCache.of("1.0"), DefaultArtifactVersionCache.of("3.0"), true)); + assertNull(instance.getNewestVersion( + DefaultArtifactVersionCache.of("1.1"), DefaultArtifactVersionCache.of("3.0"), true)); } private ArtifactVersion[] versions(String... versions) { ArtifactVersion[] artifactVersions = new ArtifactVersion[versions.length]; for (int i = 0; i < versions.length; i++) { - artifactVersions[i] = new DefaultArtifactVersion(versions[i]); + artifactVersions[i] = DefaultArtifactVersionCache.of(versions[i]); } return artifactVersions; } @@ -193,7 +195,7 @@ public void testGetNewerVersionsWithSnapshot() throws InvalidSegmentException { assertThat( instance.getNewerVersions("1.0.0-SNAPSHOT", of(SUBINCREMENTAL), false, false), - arrayContaining(new DefaultArtifactVersion("1.0.0"))); + arrayContaining(DefaultArtifactVersionCache.of("1.0.0"))); } @Test @@ -206,7 +208,7 @@ public void testAllVersionsForIgnoreScopeSubIncremental() { Restriction restriction = instance.restrictionForIgnoreScope(of(SUBINCREMENTAL)); ArtifactVersion[] filteredVersions = instance.getVersions(restriction, false); assertThat(filteredVersions, arrayWithSize(1)); - assertThat(filteredVersions, arrayContaining(new DefaultArtifactVersion("1.0.1"))); + assertThat(filteredVersions, arrayContaining(DefaultArtifactVersionCache.of("1.0.1"))); } @Test @@ -219,7 +221,7 @@ public void testAllVersionsForIgnoreScopeIncremental() { Restriction restriction = instance.restrictionForIgnoreScope(of(INCREMENTAL)); ArtifactVersion[] filteredVersions = instance.getVersions(restriction, false); assertThat(filteredVersions, arrayWithSize(1)); - assertThat(filteredVersions, arrayContaining(new DefaultArtifactVersion("1.1.0"))); + assertThat(filteredVersions, arrayContaining(DefaultArtifactVersionCache.of("1.1.0"))); } @Test @@ -232,7 +234,7 @@ public void testAllVersionsForIgnoreScopeMinor() { Restriction restriction = instance.restrictionForIgnoreScope(of(MINOR)); ArtifactVersion[] filteredVersions = instance.getVersions(restriction, false); assertThat(filteredVersions, arrayWithSize(1)); - assertThat(filteredVersions, arrayContaining(new DefaultArtifactVersion("2.0.0"))); + assertThat(filteredVersions, arrayContaining(DefaultArtifactVersionCache.of("2.0.0"))); } @Test diff --git a/versions-common/src/test/java/org/codehaus/mojo/versions/ordering/BoundArtifactVersionTest.java b/versions-common/src/test/java/org/codehaus/mojo/versions/ordering/BoundArtifactVersionTest.java index a172eac1d7..18bad59112 100644 --- a/versions-common/src/test/java/org/codehaus/mojo/versions/ordering/BoundArtifactVersionTest.java +++ b/versions-common/src/test/java/org/codehaus/mojo/versions/ordering/BoundArtifactVersionTest.java @@ -19,7 +19,7 @@ */ import org.apache.maven.artifact.versioning.ArtifactVersion; -import org.apache.maven.artifact.versioning.DefaultArtifactVersion; +import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache; import org.junit.Test; import static org.codehaus.mojo.versions.api.Segment.INCREMENTAL; @@ -36,57 +36,57 @@ public class BoundArtifactVersionTest { @Test public void testMajorUpperBoundGreaterThanNextMajor() { - BoundArtifactVersion bound = new BoundArtifactVersion(new DefaultArtifactVersion("1.2.3"), MAJOR); - ArtifactVersion artifactVersion = new DefaultArtifactVersion("2.0.0"); + BoundArtifactVersion bound = new BoundArtifactVersion("1.2.3", MAJOR); + ArtifactVersion artifactVersion = DefaultArtifactVersionCache.of("2.0.0"); assertThat(bound.compareTo(artifactVersion), greaterThan(0)); } @Test public void testSubIncrementalUpperBoundGreaterThanNextSubIncremental() { - BoundArtifactVersion bound = new BoundArtifactVersion(new DefaultArtifactVersion("1.2.3-2"), SUBINCREMENTAL); - ArtifactVersion artifactVersion = new DefaultArtifactVersion("1.2.3-3"); + BoundArtifactVersion bound = new BoundArtifactVersion("1.2.3-2", SUBINCREMENTAL); + ArtifactVersion artifactVersion = DefaultArtifactVersionCache.of("1.2.3-3"); assertThat(bound.compareTo(artifactVersion), greaterThan(0)); } @Test public void testVersionShorterThanSegment() { - BoundArtifactVersion bound = new BoundArtifactVersion(new DefaultArtifactVersion("1.1"), INCREMENTAL); - ArtifactVersion artifactVersion = new DefaultArtifactVersion("1.1.3"); + BoundArtifactVersion bound = new BoundArtifactVersion("1.1", INCREMENTAL); + ArtifactVersion artifactVersion = DefaultArtifactVersionCache.of("1.1.3"); assertThat(bound.compareTo(artifactVersion), greaterThan(0)); } @Test public void testVersionBoundArtifactVersionShorterThanConcreteVersionAndSegment() { - BoundArtifactVersion bound = new BoundArtifactVersion(new DefaultArtifactVersion("1.1"), SUBINCREMENTAL); - ArtifactVersion artifactVersion = new DefaultArtifactVersion("1.1.3"); + BoundArtifactVersion bound = new BoundArtifactVersion("1.1", SUBINCREMENTAL); + ArtifactVersion artifactVersion = DefaultArtifactVersionCache.of("1.1.3"); assertThat(bound.compareTo(artifactVersion), lessThan(0)); } @Test public void testVersionSubIncrementalBoundGreaterThanSubIncremental() { - BoundArtifactVersion bound = new BoundArtifactVersion(new DefaultArtifactVersion("1.1"), SUBINCREMENTAL); - ArtifactVersion artifactVersion = new DefaultArtifactVersion("1.1.0-2"); + BoundArtifactVersion bound = new BoundArtifactVersion("1.1", SUBINCREMENTAL); + ArtifactVersion artifactVersion = DefaultArtifactVersionCache.of("1.1.0-2"); assertThat(bound.compareTo(artifactVersion), greaterThan(0)); } @Test public void testVersionSubIncrementalBoundGreaterThanIncremental() { - BoundArtifactVersion bound = new BoundArtifactVersion(new DefaultArtifactVersion("1.1"), INCREMENTAL); - ArtifactVersion artifactVersion = new DefaultArtifactVersion("1.1.3"); + BoundArtifactVersion bound = new BoundArtifactVersion("1.1", INCREMENTAL); + ArtifactVersion artifactVersion = DefaultArtifactVersionCache.of("1.1.3"); assertThat(bound.compareTo(artifactVersion), greaterThan(0)); } @Test public void testVersionSubIncrementalBoundGreaterThanMinor() { - BoundArtifactVersion bound = new BoundArtifactVersion(new DefaultArtifactVersion("1.1"), MINOR); - ArtifactVersion artifactVersion = new DefaultArtifactVersion("1.3"); + BoundArtifactVersion bound = new BoundArtifactVersion("1.1", MINOR); + ArtifactVersion artifactVersion = DefaultArtifactVersionCache.of("1.3"); assertThat(bound.compareTo(artifactVersion), greaterThan(0)); } @Test public void testSnapshotWithSubIncremental() { - BoundArtifactVersion bound = new BoundArtifactVersion(new DefaultArtifactVersion("1.0.0-SNAPSHOT"), MINOR); - ArtifactVersion artifactVersion = new DefaultArtifactVersion("1.0.0"); + BoundArtifactVersion bound = new BoundArtifactVersion("1.0.0-SNAPSHOT", MINOR); + ArtifactVersion artifactVersion = DefaultArtifactVersionCache.of("1.0.0"); assertThat(bound.compareTo(artifactVersion), greaterThan(0)); } } diff --git a/versions-common/src/test/java/org/codehaus/mojo/versions/ordering/MercuryVersionComparatorTest.java b/versions-common/src/test/java/org/codehaus/mojo/versions/ordering/MercuryVersionComparatorTest.java index f5e8683cba..3ed017eb05 100644 --- a/versions-common/src/test/java/org/codehaus/mojo/versions/ordering/MercuryVersionComparatorTest.java +++ b/versions-common/src/test/java/org/codehaus/mojo/versions/ordering/MercuryVersionComparatorTest.java @@ -19,7 +19,7 @@ * under the License. */ -import org.apache.maven.artifact.versioning.DefaultArtifactVersion; +import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache; import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -31,11 +31,11 @@ public MercuryVersionComparatorTest() { @Test public void testSegmentCounting() { - assertEquals(1, instance.getSegmentCount(new DefaultArtifactVersion("5"))); - assertEquals(2, instance.getSegmentCount(new DefaultArtifactVersion("5.0"))); - assertEquals(2, instance.getSegmentCount(new DefaultArtifactVersion("5-0"))); - assertEquals(3, instance.getSegmentCount(new DefaultArtifactVersion("5.3.a"))); - assertEquals(6, instance.getSegmentCount(new DefaultArtifactVersion("5.0.a.1.4.5"))); - assertEquals(0, instance.getSegmentCount(new DefaultArtifactVersion(""))); + assertEquals(1, instance.getSegmentCount(DefaultArtifactVersionCache.of("5"))); + assertEquals(2, instance.getSegmentCount(DefaultArtifactVersionCache.of("5.0"))); + assertEquals(2, instance.getSegmentCount(DefaultArtifactVersionCache.of("5-0"))); + assertEquals(3, instance.getSegmentCount(DefaultArtifactVersionCache.of("5.3.a"))); + assertEquals(6, instance.getSegmentCount(DefaultArtifactVersionCache.of("5.0.a.1.4.5"))); + assertEquals(0, instance.getSegmentCount(DefaultArtifactVersionCache.of(""))); } } diff --git a/versions-common/src/test/java/org/codehaus/mojo/versions/ordering/VersionComparatorTestBase.java b/versions-common/src/test/java/org/codehaus/mojo/versions/ordering/VersionComparatorTestBase.java index 69a71510f7..372397d024 100644 --- a/versions-common/src/test/java/org/codehaus/mojo/versions/ordering/VersionComparatorTestBase.java +++ b/versions-common/src/test/java/org/codehaus/mojo/versions/ordering/VersionComparatorTestBase.java @@ -19,7 +19,7 @@ */ import org.apache.maven.artifact.versioning.ArtifactVersion; -import org.apache.maven.artifact.versioning.DefaultArtifactVersion; +import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache; import org.junit.Test; import static org.hamcrest.MatcherAssert.assertThat; @@ -37,7 +37,7 @@ public VersionComparatorTestBase(VersionComparator instance) { } protected static ArtifactVersion version(String version) { - return new DefaultArtifactVersion(version); + return DefaultArtifactVersionCache.of(version); } @Test diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayPluginUpdatesMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayPluginUpdatesMojo.java index acab4e0a84..4a8df84227 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayPluginUpdatesMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayPluginUpdatesMojo.java @@ -83,6 +83,7 @@ import org.codehaus.mojo.versions.api.recording.ChangeRecorder; import org.codehaus.mojo.versions.ordering.MavenVersionComparator; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; +import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache; import org.codehaus.mojo.versions.utils.DependencyBuilder; import org.codehaus.mojo.versions.utils.PluginComparator; import org.codehaus.plexus.util.IOUtil; @@ -344,7 +345,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { List pluginUpdates = new ArrayList<>(); List pluginLockdowns = new ArrayList<>(); - ArtifactVersion curMavenVersion = new DefaultArtifactVersion(runtimeInformation.getMavenVersion()); + ArtifactVersion curMavenVersion = DefaultArtifactVersionCache.of(runtimeInformation.getMavenVersion()); ArtifactVersion specMavenVersion = MinimalMavenBuildVersionFinder.find(getProject(), DEFAULT_MVN_VERSION, getLog()); ArtifactVersion minMavenVersion = null; @@ -433,7 +434,8 @@ public void execute() throws MojoExecutionException, MojoFailureException { && artifactVersion != null && newVersion != null && effectiveVersion != null - && new DefaultArtifactVersion(effectiveVersion).compareTo(new DefaultArtifactVersion(newVersion)) + && DefaultArtifactVersionCache.of(effectiveVersion) + .compareTo(DefaultArtifactVersionCache.of(newVersion)) < 0) { pluginUpdates.add(pad( compactKey(plugin.getGroupId(), plugin.getArtifactId()), @@ -815,7 +817,7 @@ private ArtifactVersion getPrerequisitesMavenVersion(MavenProject pluginProject) return ofNullable(pluginProject.getPrerequisites()) .map(Prerequisites::getMaven) .map(DefaultArtifactVersion::new) - .orElse(new DefaultArtifactVersion(DEFAULT_MVN_VERSION)); + .orElse(DefaultArtifactVersionCache.of(DEFAULT_MVN_VERSION)); } /** diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/MinimalMavenBuildVersionFinder.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/MinimalMavenBuildVersionFinder.java index 818ad30d71..de90d72fe1 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/MinimalMavenBuildVersionFinder.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/MinimalMavenBuildVersionFinder.java @@ -4,11 +4,11 @@ import java.util.List; import org.apache.maven.artifact.versioning.ArtifactVersion; -import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.apache.maven.model.Plugin; import org.apache.maven.model.PluginExecution; import org.apache.maven.plugin.logging.Log; import org.apache.maven.project.MavenProject; +import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache; import org.codehaus.plexus.util.xml.Xpp3Dom; /** @@ -27,7 +27,7 @@ private MinimalMavenBuildVersionFinder() { static ArtifactVersion find(MavenProject mavenProject, String defaultVersion, Log log) { ArtifactVersion version = getEnforcerMavenVersion(mavenProject, log); if (version == null && defaultVersion != null) { - version = new DefaultArtifactVersion(defaultVersion); + version = DefaultArtifactVersionCache.of(defaultVersion); } return version; } @@ -150,7 +150,7 @@ static ArtifactVersion getMinimumVersionFromRange(String versionRange) { } if (openIndicesCount == 0) { - return new DefaultArtifactVersion(versionRange); + return DefaultArtifactVersionCache.of(versionRange); } if (!((versionRange.charAt(0) == '[' || versionRange.charAt(0) == '(') @@ -169,7 +169,7 @@ static ArtifactVersion getMinimumVersionFromRange(String versionRange) { if (commaIndex == -1) { if (versionRange.charAt(0) == '[' && versionRange.charAt(versionRange.length() - 1) == ']') { - return new DefaultArtifactVersion(innerString); + return DefaultArtifactVersionCache.of(innerString); } else { return null; } @@ -183,13 +183,13 @@ static ArtifactVersion getMinimumVersionFromRange(String versionRange) { String minimumVersion = innerString.substring(0, innerString.length() - 1); if (versionRange.charAt(0) == '[' && versionRange.charAt(versionRange.length() - 1) == ')') { - return new DefaultArtifactVersion(minimumVersion); + return DefaultArtifactVersionCache.of(minimumVersion); } if (versionRange.charAt(0) == '(' && versionRange.charAt(versionRange.length() - 1) == ')') { // this is actually wrong - the Maven version should be higher than this, // the Maven version cannot be equal to this, but the Maven Enforcer plugin should capture this - return new DefaultArtifactVersion(minimumVersion); + return DefaultArtifactVersionCache.of(minimumVersion); } return null; @@ -198,13 +198,13 @@ static ArtifactVersion getMinimumVersionFromRange(String versionRange) { String minimumVersion = innerString.substring(0, commaIndex); if (versionRange.charAt(0) == '[') { - return new DefaultArtifactVersion(minimumVersion); + return DefaultArtifactVersionCache.of(minimumVersion); } if (versionRange.charAt(0) == '(') { // this is actually wrong - the Maven version should be higher than this, // the Maven version cannot be equal to this, but the Maven Enforcer plugin should capture this - return new DefaultArtifactVersion(minimumVersion); + return DefaultArtifactVersionCache.of(minimumVersion); } return null; diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java index 3ede5832ac..3b83b4cfb0 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java @@ -30,7 +30,6 @@ import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.versioning.ArtifactVersion; -import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; import org.apache.maven.artifact.versioning.VersionRange; import org.apache.maven.plugin.MojoExecutionException; @@ -48,6 +47,7 @@ import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.recording.DefaultChangeRecord; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; +import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache; import org.codehaus.mojo.versions.utils.DependencyBuilder; import org.codehaus.mojo.versions.utils.SegmentUtils; @@ -169,8 +169,9 @@ protected void update(ModifiedPomXMLEventReader pom) String initialVersion = parentVersion == null ? getProject().getParent().getVersion() : parentVersion; try { - ArtifactVersion artifactVersion = - skipResolution ? new DefaultArtifactVersion(parentVersion) : resolveTargetVersion(initialVersion); + ArtifactVersion artifactVersion = skipResolution + ? DefaultArtifactVersionCache.of(parentVersion) + : resolveTargetVersion(initialVersion); if (artifactVersion != null) { getLog().info("Updating parent from " + getProject().getParent().getVersion() + " to " + artifactVersion); diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseLatestVersionsMojoBase.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseLatestVersionsMojoBase.java index 41c7692001..3fde347f15 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseLatestVersionsMojoBase.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UseLatestVersionsMojoBase.java @@ -30,7 +30,6 @@ import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.versioning.ArtifactVersion; -import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.apache.maven.model.Dependency; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.repository.RepositorySystem; @@ -40,6 +39,7 @@ import org.codehaus.mojo.versions.api.recording.ChangeRecord; import org.codehaus.mojo.versions.api.recording.ChangeRecorder; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; +import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache; /** * Common base class for {@link UseLatestVersionsMojo} @@ -99,7 +99,7 @@ protected final void useLatestVersions( continue; } - ArtifactVersion selectedVersion = new DefaultArtifactVersion(dep.getVersion()); + ArtifactVersion selectedVersion = DefaultArtifactVersionCache.of(dep.getVersion()); getLog().debug("Selected version:" + selectedVersion); getLog().debug("Looking for newer versions of " + toString(dep)); ArtifactVersions versions = getHelper().lookupArtifactVersions(artifact, false); diff --git a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojoTest.java b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojoTest.java index b7945227a1..0e68f1c62e 100644 --- a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojoTest.java +++ b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojoTest.java @@ -19,8 +19,8 @@ * under the License. */ -import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.codehaus.mojo.versions.ordering.NumericVersionComparator; +import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache; import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -36,7 +36,7 @@ public class AbstractVersionsUpdaterMojoTest { private NumericVersionComparator instance = new NumericVersionComparator(); private int instanceCompare(String v1, String v2) { - return instance.compare(new DefaultArtifactVersion(v1), new DefaultArtifactVersion(v2)); + return instance.compare(DefaultArtifactVersionCache.of(v1), DefaultArtifactVersionCache.of(v2)); } /** diff --git a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DependencyUpdatesXmlRendererTest.java b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DependencyUpdatesXmlRendererTest.java index 7200dc914e..22d10f0b46 100644 --- a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DependencyUpdatesXmlRendererTest.java +++ b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DependencyUpdatesXmlRendererTest.java @@ -25,10 +25,10 @@ import java.util.Arrays; import org.apache.maven.artifact.DefaultArtifact; -import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.codehaus.mojo.versions.api.ArtifactVersions; import org.codehaus.mojo.versions.ordering.MavenVersionComparator; import org.codehaus.mojo.versions.reporting.model.DependencyUpdatesModel; +import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache; import org.codehaus.mojo.versions.utils.DependencyBuilder; import org.codehaus.mojo.versions.xml.DependencyUpdatesXmlReportRenderer; import org.junit.After; @@ -81,10 +81,10 @@ public void testReportGeneration() throws IOException { "default", null), Arrays.asList( - new DefaultArtifactVersion("1.0.0"), - new DefaultArtifactVersion("1.0.1"), - new DefaultArtifactVersion("1.1.0"), - new DefaultArtifactVersion("2.0.0")), + DefaultArtifactVersionCache.of("1.0.0"), + DefaultArtifactVersionCache.of("1.0.1"), + DefaultArtifactVersionCache.of("1.1.0"), + DefaultArtifactVersionCache.of("2.0.0")), new MavenVersionComparator())), emptyMap()), tempFile, diff --git a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/MinimalMavenBuildVersionFinderTest.java b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/MinimalMavenBuildVersionFinderTest.java index a2caf7a981..c558405a71 100644 --- a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/MinimalMavenBuildVersionFinderTest.java +++ b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/MinimalMavenBuildVersionFinderTest.java @@ -1,6 +1,7 @@ package org.codehaus.mojo.versions; import org.apache.maven.artifact.versioning.DefaultArtifactVersion; +import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache; import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -13,7 +14,7 @@ public class MinimalMavenBuildVersionFinderTest { @Test public void testValidVersionRanges() { - DefaultArtifactVersion expectedMinimumVersion = new DefaultArtifactVersion("1.0"); + DefaultArtifactVersion expectedMinimumVersion = DefaultArtifactVersionCache.of("1.0"); assertEquals(expectedMinimumVersion, MinimalMavenBuildVersionFinder.getMinimumVersionFromRange("1.0")); assertEquals(expectedMinimumVersion, MinimalMavenBuildVersionFinder.getMinimumVersionFromRange("[1.0]")); assertEquals(expectedMinimumVersion, MinimalMavenBuildVersionFinder.getMinimumVersionFromRange("[1.0,)")); diff --git a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/PluginUpdatesXmlRendererTest.java b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/PluginUpdatesXmlRendererTest.java index a763c80985..b27f6c13fb 100644 --- a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/PluginUpdatesXmlRendererTest.java +++ b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/PluginUpdatesXmlRendererTest.java @@ -26,12 +26,12 @@ import java.util.stream.Stream; import org.apache.maven.artifact.DefaultArtifact; -import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.apache.maven.model.Plugin; import org.codehaus.mojo.versions.api.ArtifactVersions; import org.codehaus.mojo.versions.api.PluginUpdatesDetails; import org.codehaus.mojo.versions.ordering.MavenVersionComparator; import org.codehaus.mojo.versions.reporting.model.PluginUpdatesModel; +import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache; import org.codehaus.mojo.versions.utils.DependencyBuilder; import org.codehaus.mojo.versions.xml.DependencyUpdatesXmlReportRenderer; import org.codehaus.mojo.versions.xml.PluginUpdatesXmlReportRenderer; @@ -74,7 +74,7 @@ public void testReportGeneration() throws IOException { new ArtifactVersions( artifactOf("default-group", "artifactA", "1.0.0"), Stream.of("1.0.0", "1.0.1", "1.1.0", "2.0.0") - .map(DefaultArtifactVersion::new) + .map(DefaultArtifactVersionCache::of) .collect(Collectors.toList()), new MavenVersionComparator()), singletonMap( @@ -82,7 +82,7 @@ public void testReportGeneration() throws IOException { new ArtifactVersions( artifactOf("default-group", "artifactB", "1.0.0"), Stream.of("1.0.0", "1.0.1-SNAPSHOT", "1.1.0-rc1", "2.0.0") - .map(DefaultArtifactVersion::new) + .map(DefaultArtifactVersionCache::of) .collect(Collectors.toList()), new MavenVersionComparator())), false)),