michael-o commented on a change in pull request #286: [MNG-6656] Introduce base 
for build/consumer process
URL: https://github.com/apache/maven/pull/286#discussion_r386397384
 
 

 ##########
 File path: 
maven-core/src/main/java/org/apache/maven/project/ReactorModelPool.java
 ##########
 @@ -19,53 +19,120 @@
  * under the License.
  */
 
-import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Collections;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.Objects;
+import java.util.Set;
+
+import org.apache.maven.model.Model;
 
 /**
- * Holds all POM files that are known to the reactor. This allows the project 
builder to resolve imported POMs from the
+ * Holds all Models that are known to the reactor. This allows the project 
builder to resolve imported Models from the
  * reactor when building another project's effective model.
  *
  * @author Benjamin Bentmann
+ * @Robert Scholte
  */
 class ReactorModelPool
 {
+    private final Map<GAKey, Set<Model>> modelsByGa = new HashMap<>();
+
+    private final Map<Path, Model> modelsByPath = new HashMap<>();
+
+    /**
+     * This used to be the only method, which  
+     *  
+     * @param groupId, never {@code null}
+     * @param artifactId, never {@code null}
+     * @param version, can be {@code null}
+     * @return the matching model
+     * @throws IllegalStateException if version was null and multiple modules 
share the same groupId + artifactId
+     * @throws NoSuchElementException if model could not be found
+     */
+    public Model get( String groupId, String artifactId, String version )
+        throws IllegalStateException, NoSuchElementException
+    {
+        return modelsByGa.getOrDefault( new GAKey( groupId, artifactId ), 
Collections.emptySet() ).stream()
+                        .filter( m -> version == null || version.equals( 
getVersion( m ) ) )
+                        .reduce( ( a, b ) -> 
+                        {
+                            throw new IllegalStateException( "Multiple modules 
with key "
+                                + a.getGroupId() + ':' + a.getArtifactId() );
+                        } ).orElse( null );
+    }
 
-    private final Map<CacheKey, File> pomFiles = new HashMap<>();
-
-    public File get( String groupId, String artifactId, String version )
+    public Model get( Path path )
     {
-        return pomFiles.get( new CacheKey( groupId, artifactId, version ) );
+        final Path pomFile;
+        if ( Files.isDirectory( path ) )
+        {
+            pomFile = path.resolve( "pom.xml" );
+        }
+        else
+        {
+            pomFile = path;
+        }
+        return modelsByPath.get( pomFile );
+    }
+    
+    private String getVersion( Model model )
+    {
+        String version = model.getVersion();
+        if ( version == null && model.getParent() != null )
+        {
+            version = model.getParent().getVersion();
+        }
+        return version;
     }
 
-    public void put( String groupId, String artifactId, String version, File 
pomFile )
+    static class Builder
     {
-        pomFiles.put( new CacheKey( groupId, artifactId, version ), pomFile );
+        private ReactorModelPool pool = new ReactorModelPool();
+        
+        Builder put( Path pomFile, Model model )
+        {
+            pool.modelsByPath.put( pomFile, model );
+            pool.modelsByGa.computeIfAbsent( new GAKey( getGroupId( model ), 
model.getArtifactId() ),
+                                             k -> new HashSet<Model>() ).add( 
model );
+            return this;
+        }
+        
+        ReactorModelPool build() 
+        {
+            return pool;
+        }
+
+        private static String getGroupId( Model model )
+        {
+            String groupId = model.getGroupId();
+            if ( groupId == null && model.getParent() != null )
+            {
+                groupId = model.getParent().getGroupId();
+            }
+            return groupId;
+        }
     }
 
-    private static final class CacheKey
+    private static final class GAKey
     {
 
         private final String groupId;
 
         private final String artifactId;
 
-        private final String version;
-
         private final int hashCode;
 
-        CacheKey( String groupId, String artifactId, String version )
+        GAKey( String groupId, String artifactId )
 
 Review comment:
   Why did you remove the version from here?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to