|
26 | 26 | import org.apache.maven.artifact.versioning.ArtifactVersion;
|
27 | 27 | import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
|
28 | 28 | import org.apache.maven.artifact.versioning.VersionRange;
|
| 29 | +import org.codehaus.mojo.versions.ordering.InvalidSegmentException; |
29 | 30 | import org.codehaus.mojo.versions.ordering.VersionComparator;
|
30 | 31 |
|
31 | 32 | /**
|
@@ -135,30 +136,6 @@ public final ArtifactVersion[] getVersions( ArtifactVersion currentVersion, Arti
|
135 | 136 | return getVersions( currentVersion, upperBound, includeSnapshots, false, false );
|
136 | 137 | }
|
137 | 138 |
|
138 |
| - /** |
139 |
| - * Gets newer versions of the specified artifact version. |
140 |
| - * |
141 |
| - * @param version The current version of the artifact. |
142 |
| - * @param upperBoundFixedSegment Indicates the segment in the version number that cannot be changed. For example, a |
143 |
| - * value of 0 indicates that the major version number cannot be changed. A value of -1 indicates any |
144 |
| - * segment value can be changed. |
145 |
| - * @param includeSnapshots Whether to include snapshot versions. |
146 |
| - * @return Returns the newer artifact versions. |
147 |
| - */ |
148 |
| - private ArtifactVersion[] getNewerVersions( ArtifactVersion version, int upperBoundFixedSegment, |
149 |
| - boolean includeSnapshots ) |
150 |
| - { |
151 |
| - ArtifactVersion lowerBound = version; |
152 |
| - ArtifactVersion upperBound = null; |
153 |
| - |
154 |
| - if ( upperBoundFixedSegment != -1 ) |
155 |
| - { |
156 |
| - upperBound = getVersionComparator().incrementSegment( lowerBound, upperBoundFixedSegment ); |
157 |
| - } |
158 |
| - |
159 |
| - return getVersions( version, upperBound, includeSnapshots, false, false ); |
160 |
| - } |
161 |
| - |
162 | 139 | private ArtifactVersion[] getNewerVersions( ArtifactVersion version, boolean includeSnapshots )
|
163 | 140 | {
|
164 | 141 | return getVersions( version, null, includeSnapshots, false, true );
|
@@ -243,9 +220,43 @@ public final ArtifactVersion[] getNewerVersions( String version, boolean include
|
243 | 220 | return getNewerVersions( new DefaultArtifactVersion( version ), includeSnapshots );
|
244 | 221 | }
|
245 | 222 |
|
| 223 | + /** |
| 224 | + * Returns an array of newer versions than the given version, given the upper bound segment and whether snapshots |
| 225 | + * should be included. |
| 226 | + * |
| 227 | + * @param version current version |
| 228 | + * @param upperBoundSegment the upper bound segment |
| 229 | + * @param includeSnapshots whether snapshot versions should be included |
| 230 | + * @deprecated please use {@link AbstractVersionDetails#getNewerVersions(String, int, boolean, boolean)} instead |
| 231 | + * @return array of newer versions fulfilling the criteria |
| 232 | + */ |
| 233 | + @Deprecated |
246 | 234 | public final ArtifactVersion[] getNewerVersions( String version, int upperBoundSegment, boolean includeSnapshots )
|
247 | 235 | {
|
248 |
| - return getNewerVersions( new DefaultArtifactVersion( version ), upperBoundSegment, includeSnapshots ); |
| 236 | + return getNewerVersions( version, upperBoundSegment, includeSnapshots, false ); |
| 237 | + } |
| 238 | + |
| 239 | + /** |
| 240 | + * Returns an array of newer versions than the given version, given the upper bound segment and whether snapshots |
| 241 | + * should be included. |
| 242 | + * |
| 243 | + * @param versionString current version |
| 244 | + * @param upperBoundSegment the upper bound segment |
| 245 | + * @param includeSnapshots whether snapshot versions should be included |
| 246 | + * @param allowDowngrade whether to allow downgrading if the current version is a snapshots and snapshots |
| 247 | + * are disallowed |
| 248 | + * @return array of newer versions fulfilling the criteria |
| 249 | + */ |
| 250 | + public final ArtifactVersion[] getNewerVersions( String versionString, int upperBoundSegment, |
| 251 | + boolean includeSnapshots, boolean allowDowngrade ) |
| 252 | + { |
| 253 | + ArtifactVersion currentVersion = new DefaultArtifactVersion( versionString ); |
| 254 | + ArtifactVersion lowerBound = |
| 255 | + allowDowngrade ? getLowerBoundArtifactVersion( currentVersion, upperBoundSegment ) : currentVersion; |
| 256 | + ArtifactVersion upperBound = upperBoundSegment == -1 ? null |
| 257 | + : getVersionComparator().incrementSegment( lowerBound, upperBoundSegment ); |
| 258 | + |
| 259 | + return getVersions( lowerBound, upperBound, includeSnapshots, allowDowngrade, allowDowngrade ); |
249 | 260 | }
|
250 | 261 |
|
251 | 262 | public final ArtifactVersion getOldestVersion( ArtifactVersion lowerBound, ArtifactVersion upperBound )
|
@@ -483,4 +494,52 @@ public ArtifactVersion[] getAllUpdates( VersionRange versionRange, boolean inclu
|
483 | 494 | {
|
484 | 495 | return getVersions( versionRange, getCurrentVersion(), null, includeSnapshots, false, true );
|
485 | 496 | }
|
| 497 | + |
| 498 | + protected ArtifactVersion getLowerBoundArtifactVersion( ArtifactVersion version, int segment ) |
| 499 | + { |
| 500 | + String lowerBound = getLowerBound( version, segment ); |
| 501 | + return lowerBound != null ? new DefaultArtifactVersion( lowerBound ) : null; |
| 502 | + } |
| 503 | + |
| 504 | + protected String getLowerBound( ArtifactVersion version, int segment ) |
| 505 | + { |
| 506 | + if ( segment < 0 ) |
| 507 | + { |
| 508 | + return null; |
| 509 | + } |
| 510 | + |
| 511 | + int segmentCount = getVersionComparator().getSegmentCount( version ); |
| 512 | + if ( segment > segmentCount ) |
| 513 | + { |
| 514 | + throw new InvalidSegmentException( segment, segmentCount, |
| 515 | + version.toString() ); |
| 516 | + } |
| 517 | + |
| 518 | + StringBuilder newVersion = new StringBuilder(); |
| 519 | + newVersion.append( version.getMajorVersion() ); |
| 520 | + if ( segmentCount > 0 ) |
| 521 | + { |
| 522 | + newVersion.append( "." ) |
| 523 | + .append( segment >= 1 ? version.getMinorVersion() : 0 ); |
| 524 | + } |
| 525 | + if ( segmentCount > 1 ) |
| 526 | + { |
| 527 | + newVersion.append( "." ) |
| 528 | + .append( segment >= 2 ? version.getIncrementalVersion() : 0 ); |
| 529 | + } |
| 530 | + if ( segmentCount > 2 ) |
| 531 | + { |
| 532 | + if ( version.getQualifier() != null ) |
| 533 | + { |
| 534 | + newVersion.append( "-" ) |
| 535 | + .append( segment >= 3 ? version.getQualifier() : "0" ); |
| 536 | + } |
| 537 | + else |
| 538 | + { |
| 539 | + newVersion.append( "-" ) |
| 540 | + .append( segment >= 3 ? version.getBuildNumber() : "0" ); |
| 541 | + } |
| 542 | + } |
| 543 | + return newVersion.toString(); |
| 544 | + } |
486 | 545 | }
|
0 commit comments