This is an automated email from the ASF dual-hosted git repository.
cstamas pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-resolver.git
The following commit(s) were added to refs/heads/master by this push:
new 7872699c Small helper similar to maven-artifact ComparableVersion
(#242)
7872699c is described below
commit 7872699c02b3cee4e92e0adfdc07d0a961928ae4
Author: Tamas Cservenak <[email protected]>
AuthorDate: Tue Feb 14 15:01:31 2023 +0100
Small helper similar to maven-artifact ComparableVersion (#242)
Just a small helper to simplify version parsing peculiarities
and compare them to ComparableVersion in cases like MNG-7690 is.
Make GenericVersion truly immutable and then make new methods public
As it is now totally safe to expose them. Also, GenericVersion was
last member of generic version related stuff that used non mutable
Arrays.
More cleanup and my pet peeve
* add missing Override annotations
* normalize use of requireNonNull (remove duplicates), enforce in ctors
* generic scheme creates generic components: usually is directly
instantianated, this just made a lot of casting issues
* remove some redundant checks (length > 0 then startsWith)
* union version range: is really reusable, lifted package only scope, added
javadoc
* package javadoc
No issue created, as this is really not a feature or new API in
resolver., but more internal improvement.
---
.../aether/util/version/GenericVersion.java | 55 +++++++++++++------
.../util/version/GenericVersionConstraint.java | 7 ++-
.../aether/util/version/GenericVersionRange.java | 7 ++-
.../aether/util/version/GenericVersionScheme.java | 63 +++++++++++++++++-----
.../aether/util/version/UnionVersionRange.java | 31 ++++++-----
.../eclipse/aether/util/version/package-info.java | 8 ++-
6 files changed, 124 insertions(+), 47 deletions(-)
diff --git
a/maven-resolver-util/src/main/java/org/eclipse/aether/util/version/GenericVersion.java
b/maven-resolver-util/src/main/java/org/eclipse/aether/util/version/GenericVersion.java
index e4302670..10219185 100644
---
a/maven-resolver-util/src/main/java/org/eclipse/aether/util/version/GenericVersion.java
+++
b/maven-resolver-util/src/main/java/org/eclipse/aether/util/version/GenericVersion.java
@@ -21,7 +21,7 @@ package org.eclipse.aether.util.version;
import java.math.BigInteger;
import java.util.ArrayList;
-import java.util.Arrays;
+import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@@ -29,6 +29,8 @@ import java.util.TreeMap;
import org.eclipse.aether.version.Version;
+import static java.util.Objects.requireNonNull;
+
/**
* A generic version, that is a version that accepts any input string and
tries to apply common sense sorting. See
* {@link GenericVersionScheme} for details.
@@ -39,7 +41,7 @@ final class GenericVersion
private final String version;
- private final Item[] items;
+ private final List<Item> items;
private final int hash;
@@ -50,12 +52,32 @@ final class GenericVersion
*/
GenericVersion( String version )
{
- this.version = version;
+ this.version = requireNonNull( version, "version cannot be null" );
items = parse( version );
- hash = Arrays.hashCode( items );
+ hash = items.hashCode();
+ }
+
+ /**
+ * Returns this instance backing string representation.
+ *
+ * @since 1.9.5
+ */
+ public String asString()
+ {
+ return version;
}
- private static Item[] parse( String version )
+ /**
+ * Returns this instance tokenized representation as unmodifiable list.
+ *
+ * @since 1.9.5
+ */
+ public List<Item> asItems()
+ {
+ return items;
+ }
+
+ private static List<Item> parse( String version )
{
List<Item> items = new ArrayList<>();
@@ -67,7 +89,7 @@ final class GenericVersion
trimPadding( items );
- return items.toArray( new Item[0] );
+ return Collections.unmodifiableList( items );
}
private static void trimPadding( List<Item> items )
@@ -91,30 +113,31 @@ final class GenericVersion
}
}
+ @Override
public int compareTo( Version obj )
{
- final Item[] these = items;
- final Item[] those = ( (GenericVersion) obj ).items;
+ final List<Item> these = items;
+ final List<Item> those = ( (GenericVersion) obj ).items;
boolean number = true;
for ( int index = 0;; index++ )
{
- if ( index >= these.length && index >= those.length )
+ if ( index >= these.size() && index >= those.size() )
{
return 0;
}
- else if ( index >= these.length )
+ else if ( index >= these.size() )
{
return -comparePadding( those, index, null );
}
- else if ( index >= those.length )
+ else if ( index >= those.size() )
{
return comparePadding( these, index, null );
}
- Item thisItem = these[index];
- Item thatItem = those[index];
+ Item thisItem = these.get( index );
+ Item thatItem = those.get( index );
if ( thisItem.isNumber() != thatItem.isNumber() )
{
@@ -139,12 +162,12 @@ final class GenericVersion
}
}
- private static int comparePadding( Item[] items, int index, Boolean number
)
+ private static int comparePadding( List<Item> items, int index, Boolean
number )
{
int rel = 0;
- for ( int i = index; i < items.length; i++ )
+ for ( int i = index; i < items.size(); i++ )
{
- Item item = items[i];
+ Item item = items.get( i );
if ( number != null && number != item.isNumber() )
{
break;
diff --git
a/maven-resolver-util/src/main/java/org/eclipse/aether/util/version/GenericVersionConstraint.java
b/maven-resolver-util/src/main/java/org/eclipse/aether/util/version/GenericVersionConstraint.java
index 8186941f..f4b6f570 100644
---
a/maven-resolver-util/src/main/java/org/eclipse/aether/util/version/GenericVersionConstraint.java
+++
b/maven-resolver-util/src/main/java/org/eclipse/aether/util/version/GenericVersionConstraint.java
@@ -19,14 +19,14 @@ package org.eclipse.aether.util.version;
* under the License.
*/
-import static java.util.Objects.requireNonNull;
-
import org.eclipse.aether.version.Version;
import org.eclipse.aether.version.VersionConstraint;
import org.eclipse.aether.version.VersionRange;
import java.util.Objects;
+import static java.util.Objects.requireNonNull;
+
/**
* A constraint on versions for a dependency.
*/
@@ -60,16 +60,19 @@ final class GenericVersionConstraint
this.range = null;
}
+ @Override
public VersionRange getRange()
{
return range;
}
+ @Override
public Version getVersion()
{
return version;
}
+ @Override
public boolean containsVersion( Version version )
{
if ( range == null )
diff --git
a/maven-resolver-util/src/main/java/org/eclipse/aether/util/version/GenericVersionRange.java
b/maven-resolver-util/src/main/java/org/eclipse/aether/util/version/GenericVersionRange.java
index 7346dcf4..b300d373 100644
---
a/maven-resolver-util/src/main/java/org/eclipse/aether/util/version/GenericVersionRange.java
+++
b/maven-resolver-util/src/main/java/org/eclipse/aether/util/version/GenericVersionRange.java
@@ -25,6 +25,8 @@ import org.eclipse.aether.version.VersionRange;
import java.util.Objects;
+import static java.util.Objects.requireNonNull;
+
/**
* A version range inspired by mathematical range syntax. For example,
"[1.0,2.0)", "[1.0,)" or "[1.0]".
*/
@@ -45,7 +47,7 @@ final class GenericVersionRange
GenericVersionRange( String range )
throws InvalidVersionSpecificationException
{
- String process = range;
+ String process = requireNonNull( range, "version range cannot be null"
);
boolean lowerBoundInclusive, upperBoundInclusive;
Version lowerBound, upperBound;
@@ -137,16 +139,19 @@ final class GenericVersionRange
return new GenericVersion( version );
}
+ @Override
public Bound getLowerBound()
{
return lowerBound;
}
+ @Override
public Bound getUpperBound()
{
return upperBound;
}
+ @Override
public boolean containsVersion( Version version )
{
if ( lowerBound != null )
diff --git
a/maven-resolver-util/src/main/java/org/eclipse/aether/util/version/GenericVersionScheme.java
b/maven-resolver-util/src/main/java/org/eclipse/aether/util/version/GenericVersionScheme.java
index 9b87abd4..3f6e13b5 100644
---
a/maven-resolver-util/src/main/java/org/eclipse/aether/util/version/GenericVersionScheme.java
+++
b/maven-resolver-util/src/main/java/org/eclipse/aether/util/version/GenericVersionScheme.java
@@ -23,9 +23,6 @@ import java.util.ArrayList;
import java.util.Collection;
import org.eclipse.aether.version.InvalidVersionSpecificationException;
-import org.eclipse.aether.version.Version;
-import org.eclipse.aether.version.VersionConstraint;
-import org.eclipse.aether.version.VersionRange;
import org.eclipse.aether.version.VersionScheme;
import static java.util.Objects.requireNonNull;
@@ -66,27 +63,27 @@ public final class GenericVersionScheme
{
}
- public Version parseVersion( final String version )
+ @Override
+ public GenericVersion parseVersion( final String version )
throws InvalidVersionSpecificationException
{
- requireNonNull( version, "version cannot be null" );
return new GenericVersion( version );
}
- public VersionRange parseVersionRange( final String range )
+ @Override
+ public GenericVersionRange parseVersionRange( final String range )
throws InvalidVersionSpecificationException
{
- requireNonNull( range, "range cannot be null" );
return new GenericVersionRange( range );
}
- public VersionConstraint parseVersionConstraint( final String constraint )
+ @Override
+ public GenericVersionConstraint parseVersionConstraint( final String
constraint )
throws InvalidVersionSpecificationException
{
- requireNonNull( constraint, "constraint cannot be null" );
- Collection<VersionRange> ranges = new ArrayList<>();
+ String process = requireNonNull( constraint, "constraint cannot be
null" );
- String process = constraint;
+ Collection<GenericVersionRange> ranges = new ArrayList<>();
while ( process.startsWith( "[" ) || process.startsWith( "(" ) )
{
@@ -104,12 +101,12 @@ public final class GenericVersionScheme
throw new InvalidVersionSpecificationException( constraint,
"Unbounded version range " + constraint );
}
- VersionRange range = parseVersionRange( process.substring( 0,
index + 1 ) );
+ GenericVersionRange range = parseVersionRange( process.substring(
0, index + 1 ) );
ranges.add( range );
process = process.substring( index + 1 ).trim();
- if ( process.length() > 0 && process.startsWith( "," ) )
+ if ( process.startsWith( "," ) )
{
process = process.substring( 1 ).trim();
}
@@ -121,7 +118,7 @@ public final class GenericVersionScheme
+ ", expected [ or ( but got " + process );
}
- VersionConstraint result;
+ GenericVersionConstraint result;
if ( ranges.isEmpty() )
{
result = new GenericVersionConstraint( parseVersion( constraint )
);
@@ -151,4 +148,42 @@ public final class GenericVersionScheme
return getClass().hashCode();
}
+ // CHECKSTYLE_OFF: LineLength
+ /**
+ * A handy main method that behaves similarly like maven-artifact
ComparableVersion is, to make possible test
+ * and possibly compare differences between the two.
+ * <p>
+ * To check how "1.2.7" compares to "1.2-SNAPSHOT", for example, you can
issue
+ * <pre>java -cp
${maven.repo.local}/org/apache/maven/resolver/maven-resolver-api/${resolver.version}/maven-resolver-api-${resolver.version}.jar:${maven.repo.local}/org/apache/maven/resolver/maven-resolver-util/${resolver.version}/maven-resolver-util-${resolver.version}.jar
org.eclipse.aether.util.version.GenericVersionScheme "1.2.7"
"1.2-SNAPSHOT"</pre>
+ * command to command line, output is very similar to that of
ComparableVersion on purpose.
+ */
+ // CHECKSTYLE_ON: LineLength
+ public static void main( String... args )
+ {
+ System.out.println( "Display parameters as parsed by Maven Resolver
(in canonical form and as a list of tokens)"
+ + " and comparison result:" );
+ if ( args.length == 0 )
+ {
+ return;
+ }
+
+ GenericVersion prev = null;
+ int i = 1;
+ for ( String version : args )
+ {
+ GenericVersion c = new GenericVersion( version );
+
+ if ( prev != null )
+ {
+ int compare = prev.compareTo( c );
+ System.out.println( " " + prev + ' ' + ( ( compare == 0 ) ?
"==" : ( ( compare < 0 ) ? "<" : ">" ) )
+ + ' ' + version );
+ }
+
+ System.out.println( ( i++ ) + ". " + version + " -> " +
c.asString() + "; tokens: " + c.asItems() );
+
+ prev = c;
+ }
+ }
+
}
diff --git
a/maven-resolver-util/src/main/java/org/eclipse/aether/util/version/UnionVersionRange.java
b/maven-resolver-util/src/main/java/org/eclipse/aether/util/version/UnionVersionRange.java
index 87ec1906..45ed90a8 100644
---
a/maven-resolver-util/src/main/java/org/eclipse/aether/util/version/UnionVersionRange.java
+++
b/maven-resolver-util/src/main/java/org/eclipse/aether/util/version/UnionVersionRange.java
@@ -24,6 +24,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
+import java.util.stream.Collectors;
import org.eclipse.aether.version.Version;
import org.eclipse.aether.version.VersionRange;
@@ -31,7 +32,7 @@ import org.eclipse.aether.version.VersionRange;
/**
* A union of version ranges.
*/
-final class UnionVersionRange
+public final class UnionVersionRange
implements VersionRange
{
@@ -41,15 +42,25 @@ final class UnionVersionRange
private final Bound upperBound;
+ /**
+ * Creates union {@link VersionRange}s out of passed in {@link
VersionRange} instances.
+ *
+ * @param ranges The ranges, may be empty array or even {@code null}.
+ */
public static VersionRange from( VersionRange... ranges )
{
- if ( ranges == null )
+ if ( ranges == null || ranges.length == 0 )
{
- return from( Collections.<VersionRange>emptySet() );
+ return from( Collections.emptySet() );
}
return from( Arrays.asList( ranges ) );
}
+ /**
+ * Creates union {@link VersionRange}s out of passed in {@link
VersionRange} collection.
+ *
+ * @param ranges The ranges, may be empty collection or even {@code null}.
+ */
public static VersionRange from( Collection<? extends VersionRange> ranges
)
{
if ( ranges != null && ranges.size() == 1 )
@@ -118,6 +129,7 @@ final class UnionVersionRange
}
}
+ @Override
public boolean containsVersion( Version version )
{
for ( VersionRange range : ranges )
@@ -130,11 +142,13 @@ final class UnionVersionRange
return false;
}
+ @Override
public Bound getLowerBound()
{
return lowerBound;
}
+ @Override
public Bound getUpperBound()
{
return upperBound;
@@ -167,16 +181,7 @@ final class UnionVersionRange
@Override
public String toString()
{
- StringBuilder buffer = new StringBuilder( 128 );
- for ( VersionRange range : ranges )
- {
- if ( buffer.length() > 0 )
- {
- buffer.append( ", " );
- }
- buffer.append( range );
- }
- return buffer.toString();
+ return ranges.stream().map( VersionRange::toString ).collect(
Collectors.joining( ", " ) );
}
}
diff --git
a/maven-resolver-util/src/main/java/org/eclipse/aether/util/version/package-info.java
b/maven-resolver-util/src/main/java/org/eclipse/aether/util/version/package-info.java
index 18dc724b..9540aefc 100644
---
a/maven-resolver-util/src/main/java/org/eclipse/aether/util/version/package-info.java
+++
b/maven-resolver-util/src/main/java/org/eclipse/aether/util/version/package-info.java
@@ -18,7 +18,13 @@
* under the License.
*/
/**
- * Ready-to-use version schemes for parsing/comparing versions.
+ * Ready-to-use version scheme for parsing/comparing versions and utility
classes.
+ * <p>
+ * Contains the "generic" scheme {@link
org.eclipse.aether.util.version.GenericVersionScheme}
+ * that serves the purpose of "factory" (and/or parser) for all corresponding
elements (all those are package private).
+ * <p>
+ * On the other hand, the {@link
org.eclipse.aether.util.version.UnionVersionRange} is universal implementation
of
+ * "unions" of various {@link org.eclipse.aether.version.VersionRange}
instances.
*/
package org.eclipse.aether.util.version;