From f14f456ace67454474bad2f2f01541bf35f3daac Mon Sep 17 00:00:00 2001 From: Swell <5782559+sultan@users.noreply.github.com> Date: Thu, 15 Sep 2022 00:38:48 +0200 Subject: [PATCH 1/2] Restriction as parameter to prevent more than 7 parameters in methods in the next PRs. --- .../mojo/versions/AbstractVersionsReport.java | 12 +- .../versions/AbstractVersionsUpdaterMojo.java | 13 +- .../mojo/versions/UseLatestSnapshotsMojo.java | 4 +- .../mojo/versions/UseNextSnapshotsMojo.java | 4 +- .../versions/api/AbstractVersionDetails.java | 102 +++++------- .../mojo/versions/api/PropertyVersions.java | 18 ++- .../mojo/versions/api/UpdateScope.java | 148 ++++++++++++------ .../mojo/versions/api/VersionDetails.java | 49 ++---- 8 files changed, 172 insertions(+), 178 deletions(-) diff --git a/src/main/java/org/codehaus/mojo/versions/AbstractVersionsReport.java b/src/main/java/org/codehaus/mojo/versions/AbstractVersionsReport.java index 61fb7b63b8..9cc46b91f1 100644 --- a/src/main/java/org/codehaus/mojo/versions/AbstractVersionsReport.java +++ b/src/main/java/org/codehaus/mojo/versions/AbstractVersionsReport.java @@ -234,18 +234,10 @@ protected abstract void doGenerateReport( Locale locale, Sink sink ) * @since 1.0-alpha-1 */ protected ArtifactVersion findLatestVersion( Artifact artifact, VersionRange versionRange, - boolean allowingSnapshots, boolean usePluginRepositories ) + Boolean allowingSnapshots, boolean usePluginRepositories ) throws MavenReportException { - boolean includeSnapshots = this.allowSnapshots; - if ( allowingSnapshots ) - { - includeSnapshots = true; - } - if ( allowingSnapshots ) - { - includeSnapshots = false; - } + boolean includeSnapshots = allowingSnapshots != null ? allowingSnapshots : this.allowSnapshots; try { final ArtifactVersions artifactVersions = diff --git a/src/main/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojo.java b/src/main/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojo.java index 836579f548..b4fafb1e55 100644 --- a/src/main/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojo.java @@ -313,18 +313,9 @@ protected ArtifactVersion findLatestVersion( Artifact artifact, VersionRange ver boolean allowDowngrade ) throws ArtifactMetadataRetrievalException, MojoExecutionException { - boolean includeSnapshots = this.allowSnapshots; - if ( Boolean.TRUE.equals( allowingSnapshots ) ) - { - includeSnapshots = true; - } - if ( Boolean.FALSE.equals( allowingSnapshots ) ) - { - includeSnapshots = false; - } + boolean includeSnapshots = allowingSnapshots != null ? allowingSnapshots : this.allowSnapshots; final ArtifactVersions artifactVersions = getHelper().lookupArtifactVersions( artifact, usePluginRepositories ); - return artifactVersions.getNewestVersion( versionRange, null, null, includeSnapshots, - true, true, allowDowngrade ); + return artifactVersions.getNewestVersion( versionRange, null, includeSnapshots, allowDowngrade ); } /** diff --git a/src/main/java/org/codehaus/mojo/versions/UseLatestSnapshotsMojo.java b/src/main/java/org/codehaus/mojo/versions/UseLatestSnapshotsMojo.java index 7f2cc749de..0989500000 100644 --- a/src/main/java/org/codehaus/mojo/versions/UseLatestSnapshotsMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/UseLatestSnapshotsMojo.java @@ -32,6 +32,7 @@ import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException; 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.model.Dependency; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; @@ -173,7 +174,8 @@ private void useLatestSnapshots( ModifiedPomXMLEventReader pom, Collection= 0 ? versionComparator.incrementSegment( lowerBound, segment ) : null; getLog().info( "Upper bound: " + ( upperBound == null ? "none" : upperBound.toString() ) ); - ArtifactVersion[] newer = versions.getVersions( lowerBound, upperBound, true, false, false ); + Restriction restriction = new Restriction( lowerBound, false, upperBound, false ); + ArtifactVersion[] newer = versions.getVersions( restriction, true ); getLog().debug( "Candidate versions " + Arrays.asList( newer ) ); String latestVersion; diff --git a/src/main/java/org/codehaus/mojo/versions/UseNextSnapshotsMojo.java b/src/main/java/org/codehaus/mojo/versions/UseNextSnapshotsMojo.java index 520e12f695..b13960609f 100644 --- a/src/main/java/org/codehaus/mojo/versions/UseNextSnapshotsMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/UseNextSnapshotsMojo.java @@ -30,6 +30,7 @@ import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException; 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.model.Dependency; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; @@ -155,7 +156,8 @@ private void useNextSnapshots( ModifiedPomXMLEventReader pom, Collection= 0 ? versionComparator.incrementSegment( lowerBound, segment ) : null; getLog().info( "Upper bound: " + ( upperBound == null ? "none" : upperBound.toString() ) ); - ArtifactVersion[] newer = versions.getVersions( lowerBound, upperBound, true, false, false ); + Restriction restriction = new Restriction( lowerBound, false, upperBound, false ); + ArtifactVersion[] newer = versions.getVersions( restriction, true ); getLog().debug( "Candidate versions " + Arrays.asList( newer ) ); for ( ArtifactVersion artifactVersion : newer ) { diff --git a/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java b/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java index 9aa9fbf1df..bb2a375911 100644 --- a/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java +++ b/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java @@ -29,6 +29,7 @@ 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.Restriction; import org.apache.maven.artifact.versioning.VersionRange; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.ordering.VersionComparator; @@ -129,23 +130,25 @@ public final ArtifactVersion[] getVersions() public final ArtifactVersion[] getVersions( VersionRange versionRange, boolean includeSnapshots ) { - return getVersions( versionRange, null, null, includeSnapshots, true, true ); + return getVersions( versionRange, null, includeSnapshots ); } - public final ArtifactVersion[] getVersions( ArtifactVersion currentVersion, ArtifactVersion upperBound ) + public final ArtifactVersion[] getVersions( ArtifactVersion lowerBound, ArtifactVersion upperBound ) { - return getVersions( currentVersion, upperBound, isIncludeSnapshots() ); + return getVersions( lowerBound, upperBound, isIncludeSnapshots() ); } - public final ArtifactVersion[] getVersions( ArtifactVersion currentVersion, ArtifactVersion upperBound, + public final ArtifactVersion[] getVersions( ArtifactVersion lowerBound, ArtifactVersion upperBound, boolean includeSnapshots ) { - return getVersions( currentVersion, upperBound, includeSnapshots, false, false ); + Restriction restriction = new Restriction( lowerBound, false, upperBound, false ); + return getVersions( restriction, includeSnapshots ); } private ArtifactVersion[] getNewerVersions( ArtifactVersion version, boolean includeSnapshots ) { - return getVersions( version, null, includeSnapshots, false, true ); + Restriction restriction = new Restriction( version, false, null, false ); + return getVersions( restriction, includeSnapshots ); } public final ArtifactVersion getNewestVersion( ArtifactVersion lowerBound, ArtifactVersion upperBound ) @@ -156,15 +159,14 @@ public final ArtifactVersion getNewestVersion( ArtifactVersion lowerBound, Artif public final ArtifactVersion getNewestVersion( ArtifactVersion lowerBound, ArtifactVersion upperBound, boolean includeSnapshots ) { - return getNewestVersion( lowerBound, upperBound, includeSnapshots, false, false ); + Restriction restriction = new Restriction( lowerBound, false, upperBound, false ); + return getNewestVersion( restriction, includeSnapshots ); } - public final ArtifactVersion getNewestVersion( VersionRange versionRange, ArtifactVersion lowerBound, - ArtifactVersion upperBound, boolean includeSnapshots, - boolean includeLower, boolean includeUpper ) + public final ArtifactVersion getNewestVersion( VersionRange versionRange, Restriction restriction, + boolean includeSnapshots ) { - return getNewestVersion( versionRange, lowerBound, upperBound, includeSnapshots, includeLower, - includeUpper, false ); + return getNewestVersion( versionRange, restriction, includeSnapshots, false ); } private static Iterable reverse( T[] array ) @@ -172,9 +174,8 @@ private static Iterable reverse( T[] array ) return Arrays.stream( array ).sorted( Collections.reverseOrder() ).collect( Collectors.toList() ); } - public final ArtifactVersion getNewestVersion( VersionRange versionRange, ArtifactVersion lowerBound, - ArtifactVersion upperBound, boolean includeSnapshots, - boolean includeLower, boolean includeUpper, boolean allowDowngrade ) + public final ArtifactVersion getNewestVersion( VersionRange versionRange, Restriction restriction, + boolean includeSnapshots, boolean allowDowngrade ) { final VersionComparator versionComparator = getVersionComparator(); // reverse( getVersions( ... ) ) will contain versions sorted from latest to oldest, @@ -186,13 +187,7 @@ public final ArtifactVersion getNewestVersion( VersionRange versionRange, Artifa { continue; } - int lower = lowerBound == null ? -1 : versionComparator.compare( lowerBound, candidate ); - int upper = upperBound == null ? +1 : versionComparator.compare( upperBound, candidate ); - if ( lower > 0 || upper < 0 ) - { - continue; - } - if ( ( !includeLower && lower == 0 ) || ( !includeUpper && upper == 0 ) ) + if ( restriction != null && !restriction.containsVersion( candidate ) ) { continue; } @@ -205,16 +200,14 @@ public final ArtifactVersion getNewestVersion( VersionRange versionRange, Artifa return null; } - public final ArtifactVersion getNewestVersion( ArtifactVersion lowerBound, ArtifactVersion upperBound, - boolean includeSnapshots, boolean includeLower, - boolean includeUpper ) + public final ArtifactVersion getNewestVersion( Restriction restriction, boolean includeSnapshots ) { - return getNewestVersion( null, lowerBound, upperBound, includeSnapshots, includeLower, includeUpper ); + return getNewestVersion( null, restriction, includeSnapshots ); } public final ArtifactVersion getNewestVersion( VersionRange versionRange, boolean includeSnapshots ) { - return getNewestVersion( versionRange, null, null, includeSnapshots, true, true ); + return getNewestVersion( versionRange, null, includeSnapshots ); } public final boolean containsVersion( String version ) @@ -276,7 +269,8 @@ public final ArtifactVersion[] getNewerVersions( String versionString, int upper ArtifactVersion upperBound = upperBoundSegment == -1 ? null : getVersionComparator().incrementSegment( lowerBound, upperBoundSegment ); - return getVersions( lowerBound, upperBound, includeSnapshots, allowDowngrade, allowDowngrade ); + Restriction restriction = new Restriction( lowerBound, allowDowngrade, upperBound, allowDowngrade ); + return getVersions( restriction, includeSnapshots ); } public final ArtifactVersion getOldestVersion( ArtifactVersion lowerBound, ArtifactVersion upperBound ) @@ -286,25 +280,24 @@ public final ArtifactVersion getOldestVersion( ArtifactVersion lowerBound, Artif public final ArtifactVersion getOldestVersion( VersionRange versionRange, boolean includeSnapshots ) { - return getOldestVersion( versionRange, null, null, includeSnapshots, true, true ); + return getOldestVersion( versionRange, null, includeSnapshots ); } public final ArtifactVersion getOldestVersion( ArtifactVersion lowerBound, ArtifactVersion upperBound, boolean includeSnapshots ) { - return getOldestVersion( lowerBound, upperBound, includeSnapshots, false, false ); + Restriction restriction = new Restriction( lowerBound, false, upperBound, false ); + return getOldestVersion( restriction, includeSnapshots ); } - public final ArtifactVersion getOldestVersion( ArtifactVersion lowerBound, ArtifactVersion upperBound, - boolean includeSnapshots, boolean includeLower, - boolean includeUpper ) + public final ArtifactVersion getOldestVersion( Restriction restriction, + boolean includeSnapshots ) { - return getOldestVersion( null, lowerBound, upperBound, includeSnapshots, includeLower, includeUpper ); + return getOldestVersion( null, restriction, includeSnapshots ); } - public final ArtifactVersion getOldestVersion( VersionRange versionRange, ArtifactVersion lowerBound, - ArtifactVersion upperBound, boolean includeSnapshots, - boolean includeLower, boolean includeUpper ) + public final ArtifactVersion getOldestVersion( VersionRange versionRange, Restriction restriction, + boolean includeSnapshots ) { ArtifactVersion oldest = null; final VersionComparator versionComparator = getVersionComparator(); @@ -314,13 +307,7 @@ public final ArtifactVersion getOldestVersion( VersionRange versionRange, Artifa { continue; } - int lower = lowerBound == null ? -1 : versionComparator.compare( lowerBound, candidate ); - int upper = upperBound == null ? +1 : versionComparator.compare( upperBound, candidate ); - if ( lower > 0 || upper < 0 ) - { - continue; - } - if ( ( !includeLower && lower == 0 ) || ( !includeUpper && upper == 0 ) ) + if ( restriction != null && !restriction.containsVersion( candidate ) ) { continue; } @@ -340,15 +327,13 @@ else if ( versionComparator.compare( oldest, candidate ) > 0 ) return oldest; } - public final ArtifactVersion[] getVersions( ArtifactVersion lowerBound, ArtifactVersion upperBound, - boolean includeSnapshots, boolean includeLower, boolean includeUpper ) + public final ArtifactVersion[] getVersions( Restriction restriction, boolean includeSnapshots ) { - return getVersions( null, lowerBound, upperBound, includeSnapshots, includeLower, includeUpper ); + return getVersions( null, restriction, includeSnapshots ); } - public final ArtifactVersion[] getVersions( VersionRange versionRange, ArtifactVersion lowerBound, - ArtifactVersion upperBound, boolean includeSnapshots, - boolean includeLower, boolean includeUpper ) + public final ArtifactVersion[] getVersions( VersionRange versionRange, Restriction restriction, + boolean includeSnapshots ) { final VersionComparator versionComparator = getVersionComparator(); Set result = new TreeSet<>( versionComparator ); @@ -358,13 +343,7 @@ public final ArtifactVersion[] getVersions( VersionRange versionRange, ArtifactV { continue; } - int lower = lowerBound == null ? -1 : versionComparator.compare( lowerBound, candidate ); - int upper = upperBound == null ? +1 : versionComparator.compare( upperBound, candidate ); - if ( lower > 0 || upper < 0 ) - { - continue; - } - if ( ( !includeLower && lower == 0 ) || ( !includeUpper && upper == 0 ) ) + if ( restriction != null && !restriction.containsVersion( candidate ) ) { continue; } @@ -505,17 +484,20 @@ public final ArtifactVersion[] getAllUpdates( VersionRange versionRange ) public ArtifactVersion getOldestUpdate( VersionRange versionRange, boolean includeSnapshots ) { - return getOldestVersion( versionRange, getCurrentVersion(), null, includeSnapshots, false, true ); + Restriction restriction = new Restriction( getCurrentVersion(), false, null, false ); + return getOldestVersion( versionRange, restriction, includeSnapshots ); } public ArtifactVersion getNewestUpdate( VersionRange versionRange, boolean includeSnapshots ) { - return getNewestVersion( versionRange, getCurrentVersion(), null, includeSnapshots, false, true ); + Restriction restriction = new Restriction( getCurrentVersion(), false, null, false ); + return getNewestVersion( versionRange, restriction, includeSnapshots ); } public ArtifactVersion[] getAllUpdates( VersionRange versionRange, boolean includeSnapshots ) { - return getVersions( versionRange, getCurrentVersion(), null, includeSnapshots, false, true ); + Restriction restriction = new Restriction( getCurrentVersion(), false, null, false ); + return getVersions( versionRange, restriction, includeSnapshots ); } /** diff --git a/src/main/java/org/codehaus/mojo/versions/api/PropertyVersions.java b/src/main/java/org/codehaus/mojo/versions/api/PropertyVersions.java index be119df8df..6292349644 100644 --- a/src/main/java/org/codehaus/mojo/versions/api/PropertyVersions.java +++ b/src/main/java/org/codehaus/mojo/versions/api/PropertyVersions.java @@ -36,6 +36,7 @@ import org.apache.maven.artifact.versioning.ArtifactVersion; import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; import org.apache.maven.artifact.versioning.OverConstrainedVersionException; +import org.apache.maven.artifact.versioning.Restriction; import org.apache.maven.artifact.versioning.VersionRange; import org.apache.maven.project.MavenProject; import org.codehaus.mojo.versions.Property; @@ -342,25 +343,25 @@ public ArtifactVersion getNewestVersion( String currentVersion, Property propert ? VersionRange.createFromVersionSpec( property.getVersion() ) : null; helper.getLog().debug( "Property ${" + property.getName() + "}: Restricting results to " + range ); - ArtifactVersion lowerBoundArtifactVersion = helper.createArtifactVersion( currentVersion ); + ArtifactVersion lowerBound = helper.createArtifactVersion( currentVersion ); if ( allowDowngrade ) { - Optional updatedVersion = getLowerBound( lowerBoundArtifactVersion, unchangedSegment ); - lowerBoundArtifactVersion = updatedVersion.map( helper::createArtifactVersion ).orElse( null ); + Optional updatedVersion = getLowerBound( lowerBound, unchangedSegment ); + lowerBound = updatedVersion.map( helper::createArtifactVersion ).orElse( null ); } if ( helper.getLog().isDebugEnabled() ) { - helper.getLog().debug( "lowerBoundArtifactVersion: " + lowerBoundArtifactVersion ); + helper.getLog().debug( "lowerBoundArtifactVersion: " + lowerBound ); } ArtifactVersion upperBound = null; if ( unchangedSegment != -1 ) { - upperBound = getVersionComparator().incrementSegment( lowerBoundArtifactVersion, unchangedSegment ); + upperBound = getVersionComparator().incrementSegment( lowerBound, unchangedSegment ); helper.getLog().debug( "Property ${" + property.getName() + "}: upperBound is: " + upperBound ); } - ArtifactVersion result = - getNewestVersion( range, lowerBoundArtifactVersion, upperBound, includeSnapshots, false, false ); + Restriction restriction = new Restriction( lowerBound, false, upperBound, false ); + ArtifactVersion result = getNewestVersion( range, restriction, includeSnapshots ); helper.getLog().debug( "Property ${" + property.getName() + "}: Current winner is: " + result ); @@ -427,7 +428,8 @@ private ArtifactVersion getNewestVersion( String currentVersion, VersionsHelper { upperBound = getVersionComparator().incrementSegment( lowerBound, segment ); } - return getNewestVersion( range, lowerBound, upperBound, includeSnapshots, false, false ); + Restriction restriction = new Restriction( lowerBound, false, upperBound, false ); + return getNewestVersion( range, restriction, includeSnapshots ); } private final class PropertyVersionComparator diff --git a/src/main/java/org/codehaus/mojo/versions/api/UpdateScope.java b/src/main/java/org/codehaus/mojo/versions/api/UpdateScope.java index e3bdb6d16a..5dbdb2eb3f 100644 --- a/src/main/java/org/codehaus/mojo/versions/api/UpdateScope.java +++ b/src/main/java/org/codehaus/mojo/versions/api/UpdateScope.java @@ -26,6 +26,7 @@ import java.util.Map; import org.apache.maven.artifact.versioning.ArtifactVersion; +import org.apache.maven.artifact.versioning.Restriction; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.ordering.VersionComparator; @@ -54,11 +55,13 @@ public ArtifactVersion getOldestUpdate( VersionDetails versionDetails, ArtifactV VersionComparator versionComparator = versionDetails.getVersionComparator(); try { - return versionComparator.getSegmentCount( currentVersion ) < 3 - ? null - : versionDetails.getOldestVersion( currentVersion, - versionComparator.incrementSegment( currentVersion, 2 ), - includeSnapshots, false, false ); + if ( versionComparator.getSegmentCount( currentVersion ) < 3 ) + { + return null; + } + ArtifactVersion upperBound = versionComparator.incrementSegment( currentVersion, 2 ); + Restriction restriction = new Restriction( currentVersion, false, upperBound, false ); + return versionDetails.getOldestVersion( restriction, includeSnapshots ); } catch ( InvalidSegmentException e ) { @@ -73,11 +76,13 @@ public ArtifactVersion getNewestUpdate( VersionDetails versionDetails, ArtifactV VersionComparator versionComparator = versionDetails.getVersionComparator(); try { - return versionComparator.getSegmentCount( currentVersion ) < 3 - ? null - : versionDetails.getNewestVersion( currentVersion, - versionComparator.incrementSegment( currentVersion, 2 ), - includeSnapshots, false, false ); + if ( versionComparator.getSegmentCount( currentVersion ) < 3 ) + { + return null; + } + ArtifactVersion upperBound = versionComparator.incrementSegment( currentVersion, 2 ); + Restriction restriction = new Restriction( currentVersion, false, upperBound, false ); + return versionDetails.getNewestVersion( restriction, includeSnapshots ); } catch ( InvalidSegmentException e ) { @@ -92,10 +97,13 @@ public ArtifactVersion[] getAllUpdates( VersionDetails versionDetails, ArtifactV VersionComparator versionComparator = versionDetails.getVersionComparator(); try { - return versionComparator.getSegmentCount( currentVersion ) < 3 ? null - : versionDetails.getVersions( currentVersion, - versionComparator.incrementSegment( currentVersion, 2 ), - includeSnapshots, false, false ); + if ( versionComparator.getSegmentCount( currentVersion ) < 3 ) + { + return null; + } + ArtifactVersion upperBound = versionComparator.incrementSegment( currentVersion, 2 ); + Restriction restriction = new Restriction( currentVersion, false, upperBound, false ); + return versionDetails.getVersions( restriction, includeSnapshots ); } catch ( InvalidSegmentException e ) { @@ -120,10 +128,14 @@ public ArtifactVersion getOldestUpdate( VersionDetails versionDetails, ArtifactV VersionComparator versionComparator = versionDetails.getVersionComparator(); try { - return versionComparator.getSegmentCount( currentVersion ) < 3 ? null - : versionDetails.getOldestVersion( versionComparator.incrementSegment( currentVersion, 2 ), - versionComparator.incrementSegment( currentVersion, 1 ), - includeSnapshots, true, false ); + if ( versionComparator.getSegmentCount( currentVersion ) < 3 ) + { + return null; + } + ArtifactVersion lowerBound = versionComparator.incrementSegment( currentVersion, 2 ); + ArtifactVersion upperBound = versionComparator.incrementSegment( currentVersion, 1 ); + Restriction restriction = new Restriction( lowerBound, true, upperBound, false ); + return versionDetails.getOldestVersion( restriction, includeSnapshots ); } catch ( InvalidSegmentException e ) { @@ -138,10 +150,14 @@ public ArtifactVersion getNewestUpdate( VersionDetails versionDetails, ArtifactV VersionComparator versionComparator = versionDetails.getVersionComparator(); try { - return versionComparator.getSegmentCount( currentVersion ) < 3 ? null - : versionDetails.getNewestVersion( versionComparator.incrementSegment( currentVersion, 2 ), - versionComparator.incrementSegment( currentVersion, 1 ), - includeSnapshots, true, false ); + if ( versionComparator.getSegmentCount( currentVersion ) < 3 ) + { + return null; + } + ArtifactVersion lowerBound = versionComparator.incrementSegment( currentVersion, 2 ); + ArtifactVersion upperBound = versionComparator.incrementSegment( currentVersion, 1 ); + Restriction restriction = new Restriction( lowerBound, true, upperBound, false ); + return versionDetails.getNewestVersion( restriction, includeSnapshots ); } catch ( InvalidSegmentException e ) { @@ -156,10 +172,14 @@ public ArtifactVersion[] getAllUpdates( VersionDetails versionDetails, ArtifactV VersionComparator versionComparator = versionDetails.getVersionComparator(); try { - return versionComparator.getSegmentCount( currentVersion ) < 3 ? null - : versionDetails.getVersions( versionComparator.incrementSegment( currentVersion, 2 ), - versionComparator.incrementSegment( currentVersion, 1 ), includeSnapshots, true, - false ); + if ( versionComparator.getSegmentCount( currentVersion ) < 3 ) + { + return null; + } + ArtifactVersion lowerBound = versionComparator.incrementSegment( currentVersion, 2 ); + ArtifactVersion upperBound = versionComparator.incrementSegment( currentVersion, 1 ); + Restriction restriction = new Restriction( lowerBound, true, upperBound, false ); + return versionDetails.getVersions( restriction, includeSnapshots ); } catch ( InvalidSegmentException e ) { @@ -184,10 +204,14 @@ public ArtifactVersion getOldestUpdate( VersionDetails versionDetails, ArtifactV VersionComparator versionComparator = versionDetails.getVersionComparator(); try { - return versionComparator.getSegmentCount( currentVersion ) < 2 ? null - : versionDetails.getOldestVersion( versionComparator.incrementSegment( currentVersion, 1 ), - versionComparator.incrementSegment( currentVersion, 0 ), - includeSnapshots, true, false ); + if ( versionComparator.getSegmentCount( currentVersion ) < 2 ) + { + return null; + } + ArtifactVersion lowerBound = versionComparator.incrementSegment( currentVersion, 1 ); + ArtifactVersion upperBound = versionComparator.incrementSegment( currentVersion, 0 ); + Restriction restriction = new Restriction( lowerBound, true, upperBound, false ); + return versionDetails.getOldestVersion( restriction, includeSnapshots ); } catch ( InvalidSegmentException e ) { @@ -202,10 +226,14 @@ public ArtifactVersion getNewestUpdate( VersionDetails versionDetails, ArtifactV VersionComparator versionComparator = versionDetails.getVersionComparator(); try { - return versionComparator.getSegmentCount( currentVersion ) < 2 ? null - : versionDetails.getNewestVersion( versionComparator.incrementSegment( currentVersion, 1 ), - versionComparator.incrementSegment( currentVersion, 0 ), includeSnapshots, true, - false ); + if ( versionComparator.getSegmentCount( currentVersion ) < 2 ) + { + return null; + } + ArtifactVersion lowerBound = versionComparator.incrementSegment( currentVersion, 1 ); + ArtifactVersion upperBound = versionComparator.incrementSegment( currentVersion, 0 ); + Restriction restriction = new Restriction( lowerBound, true, upperBound, false ); + return versionDetails.getNewestVersion( restriction, includeSnapshots ); } catch ( InvalidSegmentException e ) { @@ -220,10 +248,14 @@ public ArtifactVersion[] getAllUpdates( VersionDetails versionDetails, ArtifactV VersionComparator versionComparator = versionDetails.getVersionComparator(); try { - return versionComparator.getSegmentCount( currentVersion ) < 2 ? null - : versionDetails.getVersions( versionComparator.incrementSegment( currentVersion, 1 ), - versionComparator.incrementSegment( currentVersion, 0 ), - includeSnapshots, true, false ); + if ( versionComparator.getSegmentCount( currentVersion ) < 2 ) + { + return null; + } + ArtifactVersion lowerBound = versionComparator.incrementSegment( currentVersion, 1 ); + ArtifactVersion upperBound = versionComparator.incrementSegment( currentVersion, 0 ); + Restriction restriction = new Restriction( lowerBound, true, upperBound, false ); + return versionDetails.getVersions( restriction, includeSnapshots ); } catch ( InvalidSegmentException e ) { @@ -248,9 +280,13 @@ public ArtifactVersion getOldestUpdate( VersionDetails versionDetails, ArtifactV VersionComparator versionComparator = versionDetails.getVersionComparator(); try { - return versionComparator.getSegmentCount( currentVersion ) < 1 ? null - : versionDetails.getOldestVersion( versionComparator.incrementSegment( currentVersion, 0 ), - null, includeSnapshots, true, false ); + if ( versionComparator.getSegmentCount( currentVersion ) < 1 ) + { + return null; + } + ArtifactVersion lowerBound = versionComparator.incrementSegment( currentVersion, 0 ); + Restriction restriction = new Restriction( lowerBound, true, null, false ); + return versionDetails.getOldestVersion( restriction, includeSnapshots ); } catch ( InvalidSegmentException e ) { @@ -265,9 +301,13 @@ public ArtifactVersion getNewestUpdate( VersionDetails versionDetails, ArtifactV VersionComparator versionComparator = versionDetails.getVersionComparator(); try { - return versionComparator.getSegmentCount( currentVersion ) < 1 ? null - : versionDetails.getNewestVersion( versionComparator.incrementSegment( currentVersion, 0 ), - null, includeSnapshots, true, false ); + if ( versionComparator.getSegmentCount( currentVersion ) < 1 ) + { + return null; + } + ArtifactVersion lowerBound = versionComparator.incrementSegment( currentVersion, 0 ); + Restriction restriction = new Restriction( lowerBound, true, null, false ); + return versionDetails.getNewestVersion( restriction, includeSnapshots ); } catch ( InvalidSegmentException e ) { @@ -282,10 +322,13 @@ public ArtifactVersion[] getAllUpdates( VersionDetails versionDetails, ArtifactV VersionComparator versionComparator = versionDetails.getVersionComparator(); try { - return versionComparator.getSegmentCount( currentVersion ) < 1 - ? null - : versionDetails.getVersions( versionComparator.incrementSegment( currentVersion, 0 ), - null, includeSnapshots, true, false ); + if ( versionComparator.getSegmentCount( currentVersion ) < 1 ) + { + return null; + } + ArtifactVersion lowerBound = versionComparator.incrementSegment( currentVersion, 0 ); + Restriction restriction = new Restriction( lowerBound, true, null, false ); + return versionDetails.getVersions( restriction, includeSnapshots ); } catch ( InvalidSegmentException e ) { @@ -306,21 +349,24 @@ public ArtifactVersion[] getAllUpdates( VersionDetails versionDetails, ArtifactV public ArtifactVersion getOldestUpdate( VersionDetails versionDetails, ArtifactVersion currentVersion, boolean includeSnapshots ) { - return versionDetails.getOldestVersion( currentVersion, null, includeSnapshots, false, false ); + Restriction restriction = new Restriction( currentVersion, false, null, false ); + return versionDetails.getOldestVersion( restriction, includeSnapshots ); } /** {@inheritDoc} */ public ArtifactVersion getNewestUpdate( VersionDetails versionDetails, ArtifactVersion currentVersion, boolean includeSnapshots ) { - return versionDetails.getNewestVersion( currentVersion, null, includeSnapshots, false, false ); + Restriction restriction = new Restriction( currentVersion, false, null, false ); + return versionDetails.getNewestVersion( restriction, includeSnapshots ); } /** {@inheritDoc} */ public ArtifactVersion[] getAllUpdates( VersionDetails versionDetails, ArtifactVersion currentVersion, boolean includeSnapshots ) { - return versionDetails.getVersions( currentVersion, null, includeSnapshots, false, false ); + Restriction restriction = new Restriction( currentVersion, false, null, false ); + return versionDetails.getVersions( restriction, includeSnapshots ); } }; diff --git a/src/main/java/org/codehaus/mojo/versions/api/VersionDetails.java b/src/main/java/org/codehaus/mojo/versions/api/VersionDetails.java index 65ea069672..399efbedaa 100644 --- a/src/main/java/org/codehaus/mojo/versions/api/VersionDetails.java +++ b/src/main/java/org/codehaus/mojo/versions/api/VersionDetails.java @@ -20,6 +20,7 @@ */ import org.apache.maven.artifact.versioning.ArtifactVersion; +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.VersionComparator; @@ -100,32 +101,24 @@ public interface VersionDetails /** * Returns all available versions within the specified bounds. * - * @param lowerBound the lower bound or null if the lower limit is unbounded. - * @param upperBound the upper bound or null if the upper limit is unbounded. + * @param restriction version criteria. * @param includeSnapshots true if snapshots are to be included. - * @param includeLower true if the lower bound is inclusive. - * @param includeUpper true if the upper bound is inclusive. * @return all available versions within the specified version range. * @since 1.0-beta-1 */ - ArtifactVersion[] getVersions( ArtifactVersion lowerBound, ArtifactVersion upperBound, boolean includeSnapshots, - boolean includeLower, boolean includeUpper ); + ArtifactVersion[] getVersions( Restriction restriction, boolean includeSnapshots ); /** * Returns all available versions within the specified bounds. * * @param versionRange The version range within which the version must exist where null imples * [,). - * @param lowerBound the lower bound or null if the lower limit is unbounded. - * @param upperBound the upper bound or null if the upper limit is unbounded. + * @param restriction version criteria. * @param includeSnapshots true if snapshots are to be included. - * @param includeLower true if the lower bound is inclusive. - * @param includeUpper true if the upper bound is inclusive. * @return all available versions within the specified version range. * @since 1.0-beta-1 */ - ArtifactVersion[] getVersions( VersionRange versionRange, ArtifactVersion lowerBound, ArtifactVersion upperBound, - boolean includeSnapshots, boolean includeLower, boolean includeUpper ); + ArtifactVersion[] getVersions( VersionRange versionRange, Restriction restriction, boolean includeSnapshots ); /** * Returns the latest version newer than the specified lowerBound, but less than the specified upper bound or @@ -155,16 +148,12 @@ ArtifactVersion getNewestVersion( ArtifactVersion lowerBound, ArtifactVersion up * Returns the latest version newer than the specified current version, but less than the specified upper bound or * null if no such version exists. * - * @param lowerBound the lower bound or null if the lower limit is unbounded. - * @param upperBound the upper bound or null if the upper limit is unbounded. + * @param restriction version criteria. * @param includeSnapshots true if snapshots are to be included. - * @param includeLower true if the lower bound is inclusive. - * @param includeUpper true if the upper bound is inclusive. * @return the latest version between lowerBound and upperBound or null if no version is available. * @since 1.0-alpha-3 */ - ArtifactVersion getNewestVersion( ArtifactVersion lowerBound, ArtifactVersion upperBound, boolean includeSnapshots, - boolean includeLower, boolean includeUpper ); + ArtifactVersion getNewestVersion( Restriction restriction, boolean includeSnapshots ); /** * Returns the latest version newer than the specified current version, but less than the specified upper bound or @@ -172,16 +161,12 @@ ArtifactVersion getNewestVersion( ArtifactVersion lowerBound, ArtifactVersion up * * @param versionRange The version range within which the version must exist where null imples * [,). - * @param lowerBound the lower bound or null if the lower limit is unbounded. - * @param upperBound the upper bound or null if the upper limit is unbounded. + * @param restriction version criteria. * @param includeSnapshots true if snapshots are to be included. - * @param includeLower true if the lower bound is inclusive. - * @param includeUpper true if the upper bound is inclusive. * @return the latest version between lowerBound and upperBound or null if no version is available. * @since 1.0-alpha-3 */ - ArtifactVersion getNewestVersion( VersionRange versionRange, ArtifactVersion lowerBound, ArtifactVersion upperBound, - boolean includeSnapshots, boolean includeLower, boolean includeUpper ); + ArtifactVersion getNewestVersion( VersionRange versionRange, Restriction restriction, boolean includeSnapshots ); /** * Returns the latest version within the specified version range or null if no such version exists. @@ -230,32 +215,24 @@ ArtifactVersion getOldestVersion( ArtifactVersion lowerBound, ArtifactVersion up /** * Returns the oldest version within the specified bounds or null if no such version exists. * - * @param lowerBound the lower bound or null if the lower limit is unbounded. - * @param upperBound the upper bound or null if the upper limit is unbounded. + * @param restriction version criteria. * @param includeSnapshots true if snapshots are to be included. - * @param includeLower true if the lower bound is inclusive. - * @param includeUpper true if the upper bound is inclusive. * @return the oldest version between lowerBound and upperBound or null if no version is available. * @since 1.0-beta-1 */ - ArtifactVersion getOldestVersion( ArtifactVersion lowerBound, ArtifactVersion upperBound, boolean includeSnapshots, - boolean includeLower, boolean includeUpper ); + ArtifactVersion getOldestVersion( Restriction restriction, boolean includeSnapshots ); /** * Returns the oldest version within the specified bounds or null if no such version exists. * * @param versionRange The version range within which the version must exist where null imples * [,). - * @param lowerBound the lower bound or null if the lower limit is unbounded. - * @param upperBound the upper bound or null if the upper limit is unbounded. + * @param restriction version criteria. * @param includeSnapshots true if snapshots are to be included. - * @param includeLower true if the lower bound is inclusive. - * @param includeUpper true if the upper bound is inclusive. * @return the oldest version between lowerBound and upperBound or null if no version is available. * @since 1.0-beta-1 */ - ArtifactVersion getOldestVersion( VersionRange versionRange, ArtifactVersion lowerBound, ArtifactVersion upperBound, - boolean includeSnapshots, boolean includeLower, boolean includeUpper ); + ArtifactVersion getOldestVersion( VersionRange versionRange, Restriction restriction, boolean includeSnapshots ); /** * Returns the oldest version newer than the specified current version, but within the the specified update scope or From eaac20e85b5e0ac28abbfcb2220fddd4b8a70a32 Mon Sep 17 00:00:00 2001 From: Swell <5782559+sultan@users.noreply.github.com> Date: Thu, 15 Sep 2022 20:33:02 +0200 Subject: [PATCH 2/2] Restriction as parameter to prevent more than 7 parameters in methods in the next PRs. --- .../versions/api/AbstractVersionDetails.java | 37 +++++++++++++++++-- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java b/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java index bb2a375911..aee7fd9d6c 100644 --- a/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java +++ b/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java @@ -147,7 +147,7 @@ public final ArtifactVersion[] getVersions( ArtifactVersion lowerBound, Artifact private ArtifactVersion[] getNewerVersions( ArtifactVersion version, boolean includeSnapshots ) { - Restriction restriction = new Restriction( version, false, null, false ); + Restriction restriction = new Restriction( version, false, null, false ); return getVersions( restriction, includeSnapshots ); } @@ -187,7 +187,7 @@ public final ArtifactVersion getNewestVersion( VersionRange versionRange, Restri { continue; } - if ( restriction != null && !restriction.containsVersion( candidate ) ) + if ( restriction != null && !isVersionInRestriction( restriction, candidate ) ) { continue; } @@ -307,7 +307,7 @@ public final ArtifactVersion getOldestVersion( VersionRange versionRange, Restri { continue; } - if ( restriction != null && !restriction.containsVersion( candidate ) ) + if ( restriction != null && !isVersionInRestriction( restriction, candidate ) ) { continue; } @@ -343,7 +343,7 @@ public final ArtifactVersion[] getVersions( VersionRange versionRange, Restricti { continue; } - if ( restriction != null && !restriction.containsVersion( candidate ) ) + if ( restriction != null && !isVersionInRestriction( restriction, candidate ) ) { continue; } @@ -571,4 +571,33 @@ protected Optional getLowerBound( ArtifactVersion version, int unchanged } return of( newVersion.toString() ); } + + /** + * Checks if the candidate version is in the range of the restriction. + * a custom comparator is/can be used to have milestones and rcs before final releases, + * which is not yet possible with {@link Restriction#containsVersion(ArtifactVersion)}. + * @param restriction the range to check against. + * @param candidate the version to check. + * @return true if the candidate version is within the range of the restriction parameter. + */ + private boolean isVersionInRestriction( Restriction restriction, ArtifactVersion candidate ) + { + ArtifactVersion lowerBound = restriction.getLowerBound(); + ArtifactVersion upperBound = restriction.getUpperBound(); + boolean includeLower = restriction.isLowerBoundInclusive(); + boolean includeUpper = restriction.isUpperBoundInclusive(); + final VersionComparator versionComparator = getVersionComparator(); + int lower = lowerBound == null ? -1 : versionComparator.compare( lowerBound, candidate ); + int upper = upperBound == null ? +1 : versionComparator.compare( upperBound, candidate ); + if ( lower > 0 || upper < 0 ) + { + return false; + } + if ( ( !includeLower && lower == 0 ) || ( !includeUpper && upper == 0 ) ) + { + return false; + } + return true; + } + }