[MNG-6855] Simplify code - computeIfAbsent()

This commit is contained in:
Sylwester Lachiewicz 2020-01-25 21:10:01 +01:00
parent 1697e7a06b
commit ace32fdbe0
8 changed files with 12 additions and 66 deletions

View File

@ -396,13 +396,7 @@ public class LegacyRepositorySystem
{ {
String key = repository.getId(); String key = repository.getId();
List<ArtifactRepository> aliasedRepos = reposByKey.get( key ); List<ArtifactRepository> aliasedRepos = reposByKey.computeIfAbsent( key, k -> new ArrayList<>() );
if ( aliasedRepos == null )
{
aliasedRepos = new ArrayList<>();
reposByKey.put( key, aliasedRepos );
}
aliasedRepos.add( repository ); aliasedRepos.add( repository );
} }

View File

@ -331,24 +331,14 @@ public class MetadataGraph
vFrom.setCompareVersion( versionedVertices ); vFrom.setCompareVersion( versionedVertices );
vFrom.setCompareScope( scopedVertices ); vFrom.setCompareScope( scopedVertices );
List<MetadataGraphEdge> exList = excidentEdges.get( vFrom ); List<MetadataGraphEdge> exList = excidentEdges.computeIfAbsent( vFrom, k -> new ArrayList<>() );
if ( exList == null )
{
exList = new ArrayList<>();
excidentEdges.put( vFrom, exList );
}
if ( !exList.contains( e ) ) if ( !exList.contains( e ) )
{ {
exList.add( e ); exList.add( e );
} }
List<MetadataGraphEdge> inList = incidentEdges.get( vTo ); List<MetadataGraphEdge> inList = incidentEdges.computeIfAbsent( vTo, k -> new ArrayList<>() );
if ( inList == null )
{
inList = new ArrayList<>();
incidentEdges.put( vTo, inList );
}
if ( !inList.contains( e ) ) if ( !inList.contains( e ) )
{ {

View File

@ -73,13 +73,7 @@ class ReactorReader
{ {
String key = ArtifactUtils.versionlessKey( project.getGroupId(), project.getArtifactId() ); String key = ArtifactUtils.versionlessKey( project.getGroupId(), project.getArtifactId() );
List<MavenProject> projects = projectsByGA.get( key ); List<MavenProject> projects = projectsByGA.computeIfAbsent( key, k -> new ArrayList<>( 1 ) );
if ( projects == null )
{
projects = new ArrayList<>( 1 );
projectsByGA.put( key, projects );
}
projects.add( project ); projects.add( project );
} }

View File

@ -621,13 +621,7 @@ public class MavenRepositorySystem
{ {
String key = repository.getId(); String key = repository.getId();
List<ArtifactRepository> aliasedRepos = reposByKey.get( key ); List<ArtifactRepository> aliasedRepos = reposByKey.computeIfAbsent( key, k -> new ArrayList<>() );
if ( aliasedRepos == null )
{
aliasedRepos = new ArrayList<>();
reposByKey.put( key, aliasedRepos );
}
aliasedRepos.add( repository ); aliasedRepos.add( repository );
} }

View File

@ -70,23 +70,10 @@ public class ReactorManager
public Map getPluginContext( PluginDescriptor plugin, MavenProject project ) public Map getPluginContext( PluginDescriptor plugin, MavenProject project )
{ {
Map<String, Map> pluginContextsByKey = pluginContextsByProjectAndPluginKey.get( project.getId() ); Map<String, Map> pluginContextsByKey =
pluginContextsByProjectAndPluginKey.computeIfAbsent( project.getId(), k -> new HashMap<>() );
if ( pluginContextsByKey == null ) return pluginContextsByKey.computeIfAbsent( plugin.getPluginLookupKey(), k -> new HashMap<>() );
{
pluginContextsByKey = new HashMap<>();
pluginContextsByProjectAndPluginKey.put( project.getId(), pluginContextsByKey );
}
Map pluginContext = pluginContextsByKey.get( plugin.getPluginLookupKey() );
if ( pluginContext == null )
{
pluginContext = new HashMap<>();
pluginContextsByKey.put( plugin.getPluginLookupKey(), pluginContext );
}
return pluginContext;
} }
public void setFailureBehavior( String failureBehavior ) public void setFailureBehavior( String failureBehavior )

View File

@ -149,13 +149,7 @@ public class DefaultLifecycleMappingDelegate
private void addMojoExecution( Map<Integer, List<MojoExecution>> phaseBindings, MojoExecution mojoExecution, private void addMojoExecution( Map<Integer, List<MojoExecution>> phaseBindings, MojoExecution mojoExecution,
int priority ) int priority )
{ {
List<MojoExecution> mojoExecutions = phaseBindings.get( priority ); List<MojoExecution> mojoExecutions = phaseBindings.computeIfAbsent( priority, k -> new ArrayList<>() );
if ( mojoExecutions == null )
{
mojoExecutions = new ArrayList<>();
phaseBindings.put( priority, mojoExecutions );
}
mojoExecutions.add( mojoExecution ); mojoExecutions.add( mojoExecution );
} }

View File

@ -99,12 +99,8 @@ public class ProjectSorter
String projectKey = ArtifactUtils.versionlessKey( project.getGroupId(), project.getArtifactId() ); String projectKey = ArtifactUtils.versionlessKey( project.getGroupId(), project.getArtifactId() );
Map<String, Vertex> vertices = vertexMap.get( projectKey ); Map<String, Vertex> vertices = vertexMap.computeIfAbsent( projectKey, k -> new HashMap<>( 2, 1 ) );
if ( vertices == null )
{
vertices = new HashMap<>( 2, 1 );
vertexMap.put( projectKey, vertices );
}
vertices.put( project.getVersion(), dag.addVertex( projectId ) ); vertices.put( project.getVersion(), dag.addVertex( projectId ) );
} }

View File

@ -708,10 +708,7 @@ public class DefaultModelBuilder
for ( Plugin plugin : mgmt.getPlugins() ) for ( Plugin plugin : mgmt.getPlugins() )
{ {
String key = plugin.getKey(); String key = plugin.getKey();
if ( managedVersions.get( key ) == null ) managedVersions.computeIfAbsent( key, k -> plugin.getVersion() );
{
managedVersions.put( key, plugin.getVersion() );
}
} }
} }
} }