Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring UpdateScope, VersionDetails and related classes #702

Merged
merged 1 commit into from
Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@
import org.apache.maven.doxia.sink.Sink;
import org.apache.maven.model.Dependency;
import org.codehaus.mojo.versions.api.ArtifactVersions;
import org.codehaus.mojo.versions.api.UpdateScope;
import org.codehaus.mojo.versions.utils.DependencyComparator;
import org.codehaus.plexus.i18n.I18N;

import static java.util.Optional.of;
import static org.codehaus.mojo.versions.api.Segment.INCREMENTAL;
import static org.codehaus.mojo.versions.api.Segment.MAJOR;
import static org.codehaus.mojo.versions.api.Segment.MINOR;
import static org.codehaus.mojo.versions.api.Segment.SUBINCREMENTAL;

/**
* @since 1.0-beta-1
*/
Expand Down Expand Up @@ -118,19 +123,19 @@ private void renderSummaryTotalsTable( Map<Dependency, ArtifactVersions> allUpda
int numCur = 0;
for ( ArtifactVersions details : allUpdates.values() )
{
if ( details.getOldestUpdate( UpdateScope.SUBINCREMENTAL ) != null )
if ( details.getOldestUpdate( of( SUBINCREMENTAL ) ) != null )
{
numAny++;
}
else if ( details.getOldestUpdate( UpdateScope.INCREMENTAL ) != null )
else if ( details.getOldestUpdate( of( INCREMENTAL ) ) != null )
{
numInc++;
}
else if ( details.getOldestUpdate( UpdateScope.MINOR ) != null )
else if ( details.getOldestUpdate( of( MINOR ) ) != null )
{
numMin++;
}
else if ( details.getOldestUpdate( UpdateScope.MAJOR ) != null )
else if ( details.getOldestUpdate( of( MAJOR ) ) != null )
{
numMaj++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,23 @@
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.TreeMap;

import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.model.Dependency;
import org.apache.maven.reporting.MavenReportException;
import org.codehaus.mojo.versions.api.ArtifactVersions;
import org.codehaus.mojo.versions.api.UpdateScope;
import org.codehaus.mojo.versions.api.Segment;
import org.codehaus.mojo.versions.utils.DependencyComparator;

import static java.util.Optional.empty;
import static java.util.Optional.of;
import static org.codehaus.mojo.versions.api.Segment.INCREMENTAL;
import static org.codehaus.mojo.versions.api.Segment.MAJOR;
import static org.codehaus.mojo.versions.api.Segment.MINOR;
import static org.codehaus.mojo.versions.api.Segment.SUBINCREMENTAL;

/**
* XML renderer for DependencyUpdatesReport creates an xml file in target directory and writes report about available
* dependency/dependency management updates.
Expand Down Expand Up @@ -145,19 +153,19 @@ public static String getSummaryBlock( Collection<ArtifactVersions> allUpdates )
int numCur = 0;
for ( ArtifactVersions details : allUpdates )
{
if ( details.getOldestUpdate( UpdateScope.SUBINCREMENTAL ) != null )
if ( details.getOldestUpdate( of( SUBINCREMENTAL ) ) != null )
{
numAny++;
}
else if ( details.getOldestUpdate( UpdateScope.INCREMENTAL ) != null )
else if ( details.getOldestUpdate( of( INCREMENTAL ) ) != null )
{
numInc++;
}
else if ( details.getOldestUpdate( UpdateScope.MINOR ) != null )
else if ( details.getOldestUpdate( of( MINOR ) ) != null )
{
numMin++;
}
else if ( details.getOldestUpdate( UpdateScope.MAJOR ) != null )
else if ( details.getOldestUpdate( of( MAJOR ) ) != null )
{
numMaj++;
}
Expand Down Expand Up @@ -190,17 +198,17 @@ public static String getVersionsBlocks( ArtifactVersions versions )
sBuilder.append( TAB ).append( TAB ).append( TAB ).append( wrapElement( versions.isCurrentVersionDefined()
? versions.getCurrentVersion().toString() : versions.getArtifact().getVersionRange().toString(),
CURRENT_VERSION ) ).append( NL );
ArtifactVersion nextVersion = versions.getOldestUpdate( UpdateScope.ANY );
ArtifactVersion nextVersion = versions.getOldestUpdate( empty() );
if ( nextVersion != null )
{
sBuilder.append( TAB ).append( TAB ).append( TAB ).append( wrapElement( nextVersion.toString(),
NEXT_VERSION ) ).append( NL );

String incrementalsBlock = getVersionsInScopeBlock( versions, UpdateScope.INCREMENTAL );
String incrementalsBlock = getVersionsInScopeBlock( versions, of( INCREMENTAL ) );
sBuilder.append( incrementalsBlock );
String minorsBlock = getVersionsInScopeBlock( versions, UpdateScope.MINOR );
String minorsBlock = getVersionsInScopeBlock( versions, of( MINOR ) );
sBuilder.append( minorsBlock );
String majorsBlock = getVersionsInScopeBlock( versions, UpdateScope.MAJOR );
String majorsBlock = getVersionsInScopeBlock( versions, of( MAJOR ) );
sBuilder.append( majorsBlock );

String status = null;
Expand Down Expand Up @@ -259,9 +267,9 @@ private static String getDependencyInfoBlock( Map<Dependency, ArtifactVersions>
return sBuilder.toString();
}

private static String getVersionsInScopeBlock( ArtifactVersions av, UpdateScope scope )
private static String getVersionsInScopeBlock( ArtifactVersions av, Optional<Segment> scope )
{
String versionsTag = scope.toString().toLowerCase() + "s";
String versionsTag = scope.map( s -> s.name().toLowerCase() + "s" ).orElse( "any" );
StringBuilder sBuilder = new StringBuilder();

ArtifactVersion nextVersion = av.getOldestUpdate( scope );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
Expand All @@ -43,14 +44,19 @@
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.codehaus.mojo.versions.api.ArtifactVersions;
import org.codehaus.mojo.versions.api.UpdateScope;
import org.codehaus.mojo.versions.api.Segment;
import org.codehaus.mojo.versions.filtering.DependencyFilter;
import org.codehaus.mojo.versions.filtering.WildcardMatcher;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
import org.codehaus.mojo.versions.utils.DependencyComparator;
import org.codehaus.plexus.util.StringUtils;

import static java.util.Optional.empty;
import static java.util.Optional.of;
import static org.apache.commons.lang3.StringUtils.countMatches;
import static org.codehaus.mojo.versions.api.Segment.INCREMENTAL;
import static org.codehaus.mojo.versions.api.Segment.MAJOR;
import static org.codehaus.mojo.versions.api.Segment.MINOR;

/**
* Displays all dependencies that have newer versions available.
Expand Down Expand Up @@ -669,31 +675,14 @@ private DependencyManagement getProjectDependencyManagement( MavenProject projec
}
}

private UpdateScope calculateUpdateScope()
private Optional<Segment> calculateUpdateScope()
{
UpdateScope result = UpdateScope.ANY;
if ( !allowAnyUpdates )
{
if ( allowMajorUpdates )
{
result = UpdateScope.MAJOR;
}
else
{
if ( allowMinorUpdates )
{
result = UpdateScope.MINOR;
}
else
{
if ( allowIncrementalUpdates )
{
result = UpdateScope.INCREMENTAL;
}
}
}
}
return result;
return !allowAnyUpdates
? allowMajorUpdates ? of( MAJOR )
: allowMinorUpdates ? of( MINOR )
: allowIncrementalUpdates ? of( INCREMENTAL )
: empty()
: empty();
}

private void logUpdates( Map<Dependency, ArtifactVersions> updates, String section )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.model.Dependency;
import org.codehaus.mojo.versions.api.ArtifactVersions;
import org.codehaus.mojo.versions.api.UpdateScope;

import static java.util.Optional.empty;

/**
* Details of a plugin's updates.
Expand Down Expand Up @@ -65,7 +66,7 @@ public Map<Dependency, ArtifactVersions> getDependencyVersions()
*/
public boolean isArtifactUpdateAvailable()
{
return artifactVersions.getAllUpdates( UpdateScope.ANY, includeSnapshots ).length > 0;
return artifactVersions.getAllUpdates( empty(), includeSnapshots ).length > 0;
}

/**
Expand All @@ -77,7 +78,7 @@ public boolean isDependencyUpdateAvailable()
{
for ( ArtifactVersions versions : dependencyVersions.values() )
{
ArtifactVersion[] dependencyUpdates = versions.getAllUpdates( UpdateScope.ANY, includeSnapshots );
ArtifactVersion[] dependencyUpdates = versions.getAllUpdates( empty(), includeSnapshots );
if ( dependencyUpdates != null && dependencyUpdates.length > 0 )
{
return true;
Expand Down
Loading