31
31
import org .apache .maven .artifact .versioning .DefaultArtifactVersion ;
32
32
import org .apache .maven .artifact .versioning .Restriction ;
33
33
import org .apache .maven .artifact .versioning .VersionRange ;
34
+ import org .codehaus .mojo .versions .ordering .BoundArtifactVersion ;
34
35
import org .codehaus .mojo .versions .ordering .InvalidSegmentException ;
35
36
import org .codehaus .mojo .versions .ordering .VersionComparator ;
36
37
37
38
import static java .util .Optional .empty ;
38
39
import static java .util .Optional .of ;
40
+ import static org .codehaus .mojo .versions .api .Segment .SUBINCREMENTAL ;
39
41
40
42
/**
41
43
* Base class for {@link org.codehaus.mojo.versions.api.VersionDetails}.
@@ -171,7 +173,6 @@ private static <T> Iterable<T> reverse( T[] array )
171
173
public final ArtifactVersion getNewestVersion ( VersionRange versionRange , Restriction restriction ,
172
174
boolean includeSnapshots , boolean allowDowngrade )
173
175
{
174
- final VersionComparator versionComparator = getVersionComparator ();
175
176
// reverse( getVersions( ... ) ) will contain versions sorted from latest to oldest,
176
177
// so we only need to find the first candidate fulfilling the criteria
177
178
for ( ArtifactVersion candidate : reverse ( getVersions ( includeSnapshots ) ) )
@@ -260,13 +261,22 @@ public final ArtifactVersion[] getNewerVersions( String versionString, Optional<
260
261
throws InvalidSegmentException
261
262
{
262
263
ArtifactVersion currentVersion = new DefaultArtifactVersion ( versionString );
263
- ArtifactVersion lowerBound =
264
- allowDowngrade ? getLowerBoundArtifactVersion ( currentVersion , upperBoundSegment ) : currentVersion ;
265
- ArtifactVersion upperBound = !upperBoundSegment .isPresent () ? null
266
- : getVersionComparator ().incrementSegment ( lowerBound , upperBoundSegment .get () );
264
+ ArtifactVersion lowerBound = allowDowngrade
265
+ ? getLowerBound ( currentVersion , upperBoundSegment )
266
+ .map ( DefaultArtifactVersion ::new )
267
+ .orElse ( null )
268
+ : currentVersion ;
269
+ ArtifactVersion upperBound =
270
+ !upperBoundSegment .isPresent ()
271
+ ? null
272
+ : upperBoundSegment
273
+ .map ( s -> (ArtifactVersion ) new BoundArtifactVersion ( currentVersion ,
274
+ s .isMajorTo ( SUBINCREMENTAL )
275
+ ? Segment .of ( s .value () + 1 )
276
+ : s ) )
277
+ .orElse ( null );
267
278
268
279
Restriction restriction = new Restriction ( lowerBound , allowDowngrade , upperBound , allowDowngrade );
269
- // TODO shouldn't allowDowngrade boolean be passed to this call ?
270
280
return getVersions ( restriction , includeSnapshots );
271
281
}
272
282
@@ -346,7 +356,8 @@ public final ArtifactVersion getOldestUpdate( ArtifactVersion currentVersion, Op
346
356
{
347
357
try
348
358
{
349
- return getOldestVersion ( restrictionFor ( currentVersion , updateScope ), includeSnapshots );
359
+ return getOldestVersion ( getVersionComparator ().restrictionFor ( currentVersion , updateScope ),
360
+ includeSnapshots );
350
361
}
351
362
catch ( InvalidSegmentException e )
352
363
{
@@ -359,7 +370,8 @@ public final ArtifactVersion getNewestUpdate( ArtifactVersion currentVersion, Op
359
370
{
360
371
try
361
372
{
362
- return getNewestVersion ( restrictionFor ( currentVersion , updateScope ), includeSnapshots );
373
+ return getNewestVersion ( getVersionComparator ().restrictionFor ( currentVersion , updateScope ),
374
+ includeSnapshots );
363
375
}
364
376
catch ( InvalidSegmentException e )
365
377
{
@@ -372,7 +384,8 @@ public final ArtifactVersion[] getAllUpdates( ArtifactVersion currentVersion, Op
372
384
{
373
385
try
374
386
{
375
- return getVersions ( restrictionFor ( currentVersion , updateScope ), includeSnapshots );
387
+ return getVersions ( getVersionComparator ().restrictionFor ( currentVersion , updateScope ),
388
+ includeSnapshots );
376
389
}
377
390
catch ( InvalidSegmentException e )
378
391
{
@@ -433,32 +446,12 @@ public ArtifactVersion[] getAllUpdates( VersionRange versionRange, boolean inclu
433
446
return getVersions ( versionRange , restriction , includeSnapshots );
434
447
}
435
448
436
- /**
437
- * Returns the lower bound version based on the given artifact version
438
- * and the lowest unchanged segment index (0-based); -1 means that the whole version string can be changed,
439
- * implying that there is also no string designation of the lower bound version.
440
- *
441
- * @param version {@link ArtifactVersion} object specyfing the verion for which the lower bound is being computed
442
- * @param unchangedSegment first segment not to be changed; empty() means any segment can change
443
- * @return {@link ArtifactVersion} the lowest artifact version with the given segment held or null if no such
444
- * version can be found
445
- * @throws InvalidSegmentException if the requested segment is outside the bounds (less than 1 or greater than
446
- * the segment count)
447
- */
448
- protected ArtifactVersion getLowerBoundArtifactVersion ( ArtifactVersion version ,
449
- Optional <Segment > unchangedSegment )
450
- throws InvalidSegmentException
451
- {
452
- Optional <String > lowerBound = getLowerBound ( version , unchangedSegment );
453
- return lowerBound .map ( DefaultArtifactVersion ::new ).orElse ( null );
454
- }
455
-
456
449
/**
457
450
* Returns the string designation of the lower bound version based on the given artifact version
458
451
* and the lowest unchanged segment index (0-based); -1 means that the whole version string can be changed,
459
452
* implying that there is also no string designation of the lower bound version.
460
453
*
461
- * @param version {@link ArtifactVersion} object specyfing the verion for which the lower bound is being computed
454
+ * @param version {@link ArtifactVersion} object specifying the version for which the lower bound is being computed
462
455
* @param unchangedSegment first segment not to be changed; empty() means anything can change
463
456
* @return {@link Optional} string containing the lowest artifact version with the given segment held
464
457
* @throws InvalidSegmentException if the requested segment is outside of the bounds (less than 1 or greater than
@@ -527,26 +520,4 @@ private boolean isVersionInRestriction( Restriction restriction, ArtifactVersion
527
520
}
528
521
return ( includeLower || lower != 0 ) && ( includeUpper || upper != 0 );
529
522
}
530
-
531
- /**
532
- * Helper method to get the artifact boundaries for computing updates
533
- *
534
- * @param currentVersion The current version.
535
- * @param scope scope of the restriction or Optional.empty() for no restriction
536
- * @return {@linkplain Restriction} object based on the arguments
537
- * @throws InvalidSegmentException if {@code segment} ∉ [0, segmentCount)
538
- */
539
- protected Restriction restrictionFor ( ArtifactVersion currentVersion , Optional <Segment > scope )
540
- throws InvalidSegmentException
541
- {
542
- VersionComparator versionComparator = getVersionComparator ();
543
- ArtifactVersion lowerBound = scope .isPresent () && scope .get ().value () < Segment .SUBINCREMENTAL .value ()
544
- ? versionComparator .incrementSegment ( currentVersion , scope .get () )
545
- : currentVersion ;
546
- ArtifactVersion upperBound = scope .isPresent () && scope .get ().value () > Segment .MAJOR .value ()
547
- ? versionComparator .incrementSegment ( currentVersion , Segment .of ( scope .get ().value () - 1 ) )
548
- : null ;
549
- return new Restriction ( lowerBound , lowerBound != currentVersion ,
550
- upperBound , false );
551
- }
552
523
}
0 commit comments