Skip to content

Commit

Permalink
Add caching to getNewestUpdates in report renderers. Use streams API …
Browse files Browse the repository at this point in the history
…in AbstractVersionDetails
  • Loading branch information
andrzejj0 committed Oct 17, 2022
1 parent ca5e347 commit bf37bea
Showing 1 changed file with 17 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@
*/

import java.util.Arrays;
import java.util.Collections;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;

import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.artifact.versioning.ArtifactVersion;
Expand All @@ -35,6 +33,7 @@
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
import org.codehaus.mojo.versions.ordering.VersionComparator;

import static java.util.Collections.reverseOrder;
import static java.util.Optional.empty;
import static java.util.Optional.of;
import static org.codehaus.mojo.versions.api.Segment.SUBINCREMENTAL;
Expand Down Expand Up @@ -173,34 +172,20 @@ public final ArtifactVersion getNewestVersion( VersionRange versionRange, Restri
return getNewestVersion( versionRange, restriction, includeSnapshots, false );
}

private static <T> Iterable<T> reverse( T[] array )
{
return Arrays.stream( array ).sorted( Collections.reverseOrder() ).collect( Collectors.toList() );
}

@Override
public final ArtifactVersion getNewestVersion( VersionRange versionRange, Restriction restriction,
boolean includeSnapshots, boolean allowDowngrade )
{
// reverse( getVersions( ... ) ) will contain versions sorted from latest to oldest,
// reverseOrder( getVersions( ... ) ) will contain versions sorted from latest to oldest,
// so we only need to find the first candidate fulfilling the criteria
for ( ArtifactVersion candidate : reverse( getVersions( includeSnapshots ) ) )
{
if ( allowDowngrade || versionRange == null
|| ArtifactVersions.isVersionInRange( candidate, versionRange ) )
{
if ( restriction != null && !isVersionInRestriction( restriction, candidate ) )
{
continue;
}
if ( !includeSnapshots && ArtifactUtils.isSnapshot( candidate.toString() ) )
{
continue;
}
return candidate;
}
}
return null;
return Arrays.stream( getVersions( includeSnapshots ) )
.sorted( reverseOrder() )
.filter( candidate -> ( allowDowngrade || versionRange == null
|| ArtifactVersions.isVersionInRange( candidate, versionRange ) )
&& ( restriction == null || isVersionInRestriction( restriction, candidate ) )
&& ( includeSnapshots || !ArtifactUtils.isSnapshot( candidate.toString() ) ) )
.findAny()
.orElse( null );
}

@Override
Expand Down Expand Up @@ -284,25 +269,13 @@ public final ArtifactVersion[] getVersions( Restriction restriction, boolean inc
public final ArtifactVersion[] getVersions( VersionRange versionRange, Restriction restriction,
boolean includeSnapshots )
{
final VersionComparator versionComparator = getVersionComparator();
Set<ArtifactVersion> result = new TreeSet<>( versionComparator );
for ( ArtifactVersion candidate : getVersions( includeSnapshots ) )
{
if ( versionRange != null && !ArtifactVersions.isVersionInRange( candidate, versionRange ) )
{
continue;
}
if ( restriction != null && !isVersionInRestriction( restriction, candidate ) )
{
continue;
}
if ( !includeSnapshots && ArtifactUtils.isSnapshot( candidate.toString() ) )
{
continue;
}
result.add( candidate );
}
return result.toArray( new ArtifactVersion[0] );
return Arrays.stream( getVersions( includeSnapshots ) )
.filter( candidate ->
( versionRange == null || ArtifactVersions.isVersionInRange( candidate, versionRange ) )
&& ( restriction == null || isVersionInRestriction( restriction, candidate ) )
&& ( includeSnapshots || !ArtifactUtils.isSnapshot( candidate.toString() ) ) )
.collect( () -> new TreeSet<>( getVersionComparator() ), TreeSet::add, Set::addAll )
.toArray( new ArtifactVersion[0] );
}

@Override
Expand Down

0 comments on commit bf37bea

Please sign in to comment.