You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by kh...@apache.org on 2021/10/05 15:37:46 UTC

[maven] 01/02: [MNG-7276] - Refactoring - Umbrella

This is an automated email from the ASF dual-hosted git repository.

khmarbaise pushed a commit to branch MNG-7276
in repository https://gitbox.apache.org/repos/asf/maven.git

commit d18220d54bebf8650bf1b5c75fd4e7787be9b6e0
Author: Karl Heinz Marbaise <kh...@apache.org>
AuthorDate: Thu Sep 30 19:10:44 2021 +0200

    [MNG-7276] - Refactoring - Umbrella
---
 .../main/java/org/apache/maven/ReactorReader.java  | 73 ++++++++--------------
 .../java/org/apache/maven/RepositoryUtils.java     | 62 +++++++-----------
 .../resolver/filter/ExclusionArtifactFilter.java   | 49 +++++++--------
 .../org/apache/maven/execution/MavenSession.java   | 12 +---
 4 files changed, 76 insertions(+), 120 deletions(-)

diff --git a/maven-core/src/main/java/org/apache/maven/ReactorReader.java b/maven-core/src/main/java/org/apache/maven/ReactorReader.java
index 247632a..07f61e0 100644
--- a/maven-core/src/main/java/org/apache/maven/ReactorReader.java
+++ b/maven-core/src/main/java/org/apache/maven/ReactorReader.java
@@ -24,20 +24,19 @@ import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
-import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.function.Function;
+import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
-import javax.inject.Inject;
-import javax.inject.Named;
-
 import org.apache.maven.artifact.ArtifactUtils;
 import org.apache.maven.execution.MavenSession;
 import org.apache.maven.model.Model;
@@ -49,6 +48,9 @@ import org.eclipse.aether.util.artifact.ArtifactIdUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import javax.inject.Inject;
+import javax.inject.Named;
+
 /**
  * An implementation of a workspace reader that knows how to search the Maven reactor for artifacts, either as packaged
  * jar if it has been built, or only compile output directory if packaging hasn't happened yet.
@@ -72,26 +74,22 @@ class ReactorReader
     private final Map<String, List<MavenProject>> projectsByGA;
     private final WorkspaceRepository repository;
 
+    private Function<MavenProject, String> projectIntoKey =
+            s -> ArtifactUtils.key( s.getGroupId(), s.getArtifactId(), s.getVersion() );
+
+    private Function<MavenProject, String> projectIntoVersionlessKey =
+            s -> ArtifactUtils.versionlessKey( s.getGroupId(), s.getArtifactId() );
+
     @Inject
     ReactorReader( MavenSession session )
     {
         this.session = session;
-        this.projectsByGAV = new HashMap<>( session.getAllProjects().size() * 2 );
-        session.getAllProjects().forEach( project ->
-        {
-            String projectId = ArtifactUtils.key( project.getGroupId(), project.getArtifactId(), project.getVersion() );
-            this.projectsByGAV.put( projectId, project );
-        } );
-
-        projectsByGA = new HashMap<>( projectsByGAV.size() * 2 );
-        for ( MavenProject project : projectsByGAV.values() )
-        {
-            String key = ArtifactUtils.versionlessKey( project.getGroupId(), project.getArtifactId() );
+        this.projectsByGAV =
+                session.getAllProjects().stream()
+                        .collect( Collectors.toMap( projectIntoKey, Function.identity() ) );
 
-            List<MavenProject> projects = projectsByGA.computeIfAbsent( key, k -> new ArrayList<>( 1 ) );
-
-            projects.add( project );
-        }
+        this.projectsByGA = projectsByGAV.values().stream()
+                .collect( Collectors.groupingBy( projectIntoVersionlessKey ) );
 
         repository = new WorkspaceRepository( "reactor", new HashSet<>( projectsByGAV.keySet() ) );
     }
@@ -128,23 +126,11 @@ class ReactorReader
     {
         String key = ArtifactUtils.versionlessKey( artifact.getGroupId(), artifact.getArtifactId() );
 
-        List<MavenProject> projects = projectsByGA.get( key );
-        if ( projects == null || projects.isEmpty() )
-        {
-            return Collections.emptyList();
-        }
-
-        List<String> versions = new ArrayList<>();
-
-        for ( MavenProject project : projects )
-        {
-            if ( find( project, artifact ) != null )
-            {
-                versions.add( project.getVersion() );
-            }
-        }
-
-        return Collections.unmodifiableList( versions );
+        return Optional.ofNullable( projectsByGA.get( key ) )
+                .orElse( Collections.emptyList() ).stream()
+                .filter( s -> Objects.nonNull( find( s, artifact ) ) )
+                .map( MavenProject::getVersion )
+                .collect( Collectors.collectingAndThen( Collectors.toList(), Collections::unmodifiableList ) );
     }
 
     @Override
@@ -334,15 +320,10 @@ class ReactorReader
             return mainArtifact;
         }
 
-        for ( Artifact attachedArtifact : RepositoryUtils.toArtifacts( project.getAttachedArtifacts() ) )
-        {
-            if ( attachedArtifactComparison( requestedArtifact, attachedArtifact ) )
-            {
-                return attachedArtifact;
-            }
-        }
-
-        return null;
+        return RepositoryUtils.toArtifacts( project.getAttachedArtifacts() ).stream()
+                .filter( artifact -> attachedArtifactComparison( requestedArtifact, artifact ) )
+                .findFirst()
+                .orElse( null );
     }
 
     private boolean attachedArtifactComparison( Artifact requested, Artifact attached )
diff --git a/maven-core/src/main/java/org/apache/maven/RepositoryUtils.java b/maven-core/src/main/java/org/apache/maven/RepositoryUtils.java
index a49b670..b8c8837 100644
--- a/maven-core/src/main/java/org/apache/maven/RepositoryUtils.java
+++ b/maven-core/src/main/java/org/apache/maven/RepositoryUtils.java
@@ -26,6 +26,8 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
+import java.util.Optional;
+import java.util.stream.Collectors;
 
 import org.apache.maven.artifact.handler.ArtifactHandler;
 import org.apache.maven.artifact.handler.DefaultArtifactHandler;
@@ -121,7 +123,7 @@ public class RepositoryUtils
             nodeTrail.addAll( trail );
             nodeTrail.add( artifact.getId() );
 
-            if ( filter == null || filter.accept( node, Collections.<DependencyNode>emptyList() ) )
+            if ( filter == null || filter.accept( node, Collections.emptyList() ) )
             {
                 artifact.setDependencyTrail( nodeTrail );
                 artifacts.add( artifact );
@@ -170,32 +172,21 @@ public class RepositoryUtils
 
         Artifact result = toArtifact( artifact );
 
-        List<Exclusion> excl = null;
-        if ( exclusions != null )
-        {
-            excl = new ArrayList<>( exclusions.size() );
-            for ( org.apache.maven.model.Exclusion exclusion : exclusions )
-            {
-                excl.add( toExclusion( exclusion ) );
-            }
-        }
-
+        List<Exclusion> excl = Optional.ofNullable( exclusions )
+                .orElse( Collections.emptyList() )
+                .stream()
+                .map( RepositoryUtils::toExclusion )
+                .collect( Collectors.toList() );
         return new Dependency( result, artifact.getScope(), artifact.isOptional(), excl );
     }
 
     public static List<RemoteRepository> toRepos( List<ArtifactRepository> repos )
     {
-        if ( repos == null )
-        {
-            return null;
-        }
-
-        List<RemoteRepository> results = new ArrayList<>( repos.size() );
-        for ( ArtifactRepository repo : repos )
-        {
-            results.add( toRepo( repo ) );
-        }
-        return results;
+        return Optional.ofNullable( repos )
+                .orElse( Collections.emptyList() )
+                .stream()
+                .map( RepositoryUtils::toRepo )
+                .collect( Collectors.toList() );
     }
 
     public static RemoteRepository toRepo( ArtifactRepository repo )
@@ -318,20 +309,16 @@ public class RepositoryUtils
             new DefaultArtifact( dependency.getGroupId(), dependency.getArtifactId(), dependency.getClassifier(), null,
                                  dependency.getVersion(), props, stereotype );
 
-        List<Exclusion> exclusions = new ArrayList<>( dependency.getExclusions().size() );
-        for ( org.apache.maven.model.Exclusion exclusion : dependency.getExclusions() )
-        {
-            exclusions.add( toExclusion( exclusion ) );
-        }
+        List<Exclusion> exclusions =
+                dependency.getExclusions().stream().map( RepositoryUtils::toExclusion ).collect( Collectors.toList() );
 
-        Dependency result = new Dependency( artifact,
-                                            dependency.getScope(),
-                                            dependency.getOptional() != null
-                                                ? dependency.isOptional()
-                                                : null,
-                                            exclusions );
+        return new Dependency( artifact,
+                dependency.getScope(),
+                dependency.getOptional() != null
+                        ? dependency.isOptional()
+                        : null,
+                exclusions );
 
-        return result;
     }
 
     private static Exclusion toExclusion( org.apache.maven.model.Exclusion exclusion )
@@ -365,12 +352,7 @@ public class RepositoryUtils
 
     public static Collection<Artifact> toArtifacts( Collection<org.apache.maven.artifact.Artifact> artifactsToConvert )
     {
-        List<Artifact> artifacts = new ArrayList<>();
-        for ( org.apache.maven.artifact.Artifact a : artifactsToConvert )
-        {
-            artifacts.add( toArtifact( a ) );
-        }
-        return artifacts;
+        return artifactsToConvert.stream().map( RepositoryUtils::toArtifact ).collect( Collectors.toList() );
     }
 
     public static WorkspaceRepository getWorkspace( RepositorySystemSession session )
diff --git a/maven-core/src/main/java/org/apache/maven/artifact/resolver/filter/ExclusionArtifactFilter.java b/maven-core/src/main/java/org/apache/maven/artifact/resolver/filter/ExclusionArtifactFilter.java
index dd50c31..d58c4a7 100644
--- a/maven-core/src/main/java/org/apache/maven/artifact/resolver/filter/ExclusionArtifactFilter.java
+++ b/maven-core/src/main/java/org/apache/maven/artifact/resolver/filter/ExclusionArtifactFilter.java
@@ -19,11 +19,12 @@ package org.apache.maven.artifact.resolver.filter;
  * under the License.
  */
 
+import java.util.List;
+import java.util.function.Predicate;
+
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.model.Exclusion;
 
-import java.util.List;
-
 /**
  * Filter to exclude from a list of artifact patterns.
  */
@@ -38,31 +39,29 @@ public class ExclusionArtifactFilter implements ArtifactFilter
         this.exclusions = exclusions;
     }
 
+    private Predicate<Exclusion> sameArtifactId( Artifact artifact )
+    {
+        return exclusion -> exclusion.getArtifactId().equals( artifact.getArtifactId() );
+    }
+
+    private Predicate<Exclusion> sameGroupId( Artifact artifact )
+    {
+        return exclusion -> exclusion.getGroupId().equals( artifact.getGroupId() );
+    }
+
+    private Predicate<Exclusion> groupIdIsWildcard = exclusion -> WILDCARD.equals( exclusion.getGroupId() );
+    private Predicate<Exclusion> artifactIdIsWildcard = exclusion -> WILDCARD.equals( exclusion.getArtifactId() );
+
+    Predicate<Exclusion> groupIdAndArtifactIdIsWildcard = groupIdIsWildcard.and( artifactIdIsWildcard );
+
     @Override
     public boolean include( Artifact artifact )
     {
-        for ( Exclusion exclusion : exclusions )
-        {
-            if ( WILDCARD.equals( exclusion.getGroupId() ) && WILDCARD.equals( exclusion.getArtifactId() ) )
-            {
-                return false;
-            }
-            if ( WILDCARD.equals( exclusion.getGroupId() )
-                && exclusion.getArtifactId().equals( artifact.getArtifactId() ) )
-            {
-                return false;
-            }
-            if ( WILDCARD.equals( exclusion.getArtifactId() )
-                && exclusion.getGroupId().equals( artifact.getGroupId() ) )
-            {
-                return false;
-            }
-            if ( exclusion.getGroupId().equals( artifact.getGroupId() )
-                && exclusion.getArtifactId().equals( artifact.getArtifactId() ) )
-            {
-                return false;
-            }
-        }
-        return true;
+        return !exclusions.stream().anyMatch(
+                groupIdAndArtifactIdIsWildcard
+                        .or( groupIdIsWildcard.and( sameArtifactId( artifact ) ) )
+                        .or( artifactIdIsWildcard.and( sameGroupId( artifact ) ) )
+                        .or( sameGroupId( artifact ).and( sameArtifactId( artifact ) ) )
+        );
     }
 }
diff --git a/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java b/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java
index f8dbb03..adde001 100644
--- a/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java
+++ b/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java
@@ -84,15 +84,9 @@ public class MavenSession
         if ( !projects.isEmpty() )
         {
             this.currentProject = projects.get( 0 );
-            this.topLevelProject = currentProject;
-            for ( MavenProject project : projects )
-            {
-                if ( project.isExecutionRoot() )
-                {
-                    topLevelProject = project;
-                    break;
-                }
-            }
+            this.topLevelProject =
+                    projects.stream().filter( project -> project.isExecutionRoot() ).findFirst()
+                            .orElse( currentProject );
         }
         else
         {