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
jarmoniuk committed Oct 17, 2022
1 parent 87d5f5b commit 157c25c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 78 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 @@ -291,32 +276,14 @@ public final ArtifactVersion getOldestVersion( Restriction restriction,
public final ArtifactVersion getOldestVersion( VersionRange versionRange, Restriction restriction,
boolean includeSnapshots )
{
ArtifactVersion oldest = null;
final VersionComparator versionComparator = getVersionComparator();
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;
}
if ( oldest == null )
{
oldest = candidate;
}
else if ( versionComparator.compare( oldest, candidate ) > 0 )
{
oldest = candidate;
}
}
return oldest;
return Arrays.stream( getVersions( includeSnapshots ) )
.sorted()
.filter( candidate -> ( versionRange == null
|| ArtifactVersions.isVersionInRange( candidate, versionRange ) )
&& ( restriction == null || isVersionInRestriction( restriction, candidate ) )
&& ( includeSnapshots || !ArtifactUtils.isSnapshot( candidate.toString() ) ) )
.findAny()
.orElse( null );
}

@Override
Expand All @@ -329,25 +296,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
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public abstract class AbstractVersionsReportRenderer<T> extends VersionsReportRe
protected ArtifactVersionsCache oldestUpdateCache
= new ArtifactVersionsCache( AbstractVersionDetails::getOldestUpdate );

protected ArtifactVersionsCache newestUpdateCache
= new ArtifactVersionsCache( AbstractVersionDetails::getNewestUpdate );

protected ArtifactVersionsCache allUpdatesCache
= new ArtifactVersionsCache( AbstractVersionDetails::getAllUpdates );

Expand Down Expand Up @@ -275,37 +278,37 @@ protected void renderSummaryTableRow( Dependency artifact, ArtifactVersions arti
sink.tableCell_();

sink.tableCell();
if ( artifactVersions.getNewestUpdate( of( SUBINCREMENTAL ) ) != null )
if ( newestUpdateCache.get( artifactVersions, of( SUBINCREMENTAL ) ) != null )
{
safeBold();
sink.text( artifactVersions.getNewestUpdate( of( SUBINCREMENTAL ) ).toString() );
sink.text( newestUpdateCache.get( artifactVersions, of( SUBINCREMENTAL ) ).toString() );
safeBold_();
}
sink.tableCell_();

sink.tableCell();
if ( artifactVersions.getNewestUpdate( of( INCREMENTAL ) ) != null )
if ( newestUpdateCache.get( artifactVersions, of( INCREMENTAL ) ) != null )
{
safeBold();
sink.text( artifactVersions.getNewestUpdate( of( INCREMENTAL ) ).toString() );
sink.text( newestUpdateCache.get( artifactVersions, of( INCREMENTAL ) ).toString() );
safeBold_();
}
sink.tableCell_();

sink.tableCell();
if ( artifactVersions.getNewestUpdate( of( MINOR ) ) != null )
if ( newestUpdateCache.get( artifactVersions, of( MINOR ) ) != null )
{
safeBold();
sink.text( artifactVersions.getNewestUpdate( of( MINOR ) ).toString() );
sink.text( newestUpdateCache.get( artifactVersions, of( MINOR ) ).toString() );
safeBold_();
}
sink.tableCell_();

sink.tableCell();
if ( artifactVersions.getNewestUpdate( of( MAJOR ) ) != null )
if ( newestUpdateCache.get( artifactVersions, of( MAJOR ) ) != null )
{
safeBold();
sink.text( artifactVersions.getNewestUpdate( of( MAJOR ) ).toString() );
sink.text( newestUpdateCache.get( artifactVersions, of( MAJOR ) ).toString() );
safeBold_();
}
sink.tableCell_();
Expand Down

0 comments on commit 157c25c

Please sign in to comment.