rfscholte commented on a change in pull request #391:
URL: https://github.com/apache/maven/pull/391#discussion_r542526519



##########
File path: 
maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java
##########
@@ -1707,4 +1786,132 @@ protected void mergePluginContainer_Plugins( 
PluginContainer target, PluginConta
             // don't merge
         }
     }
+    
+    /**
+     * Builds up the transformer context.
+     * After the buildplan is ready, the build()-method returns the immutable 
context useful during distribution.
+     * This is an inner class, as it must be able to call readRawModel() 
+     * 
+     * @author Robert Scholte
+     * @since 4.0.0
+     */
+    private class DefaultTransformerContextBuilder implements 
TransformerContextBuilder
+    {
+        private final DefaultTransformerContext context = new 
DefaultTransformerContext();
+
+        private final Map<DefaultTransformerContext.GAKey, Set<Source>> 
mappedSources
+                = new ConcurrentHashMap<>( 64 );
+
+        /**
+         * If an interface could be extracted, DefaultModelProblemCollector 
should be ModelProblemCollectorExt
+         * 
+         * @param request
+         * @param collector
+         * @return
+         */
+        @Override
+        public TransformerContext initialize( ModelBuildingRequest request, 
ModelProblemCollector collector )
+        {
+            // We must assume the TransformerContext was created using 
this.newTransformerContextBuilder()
+            DefaultModelProblemCollector problems = 
(DefaultModelProblemCollector) collector;
+            return new TransformerContext()
+            {
+                @Override
+                public String getUserProperty( String key )
+                {
+                    return context.userProperties.computeIfAbsent( key,
+                                                           k -> 
request.getUserProperties().getProperty( key ) );
+                }
+                
+                @Override
+                public Model getRawModel( String gId, String aId )
+                {
+                    return context.modelByGA.computeIfAbsent( new 
DefaultTransformerContext.GAKey( gId, aId ),
+                                                              k -> 
findRawModel( gId, aId ) );
+                }
+                
+                @Override
+                public Model getRawModel( Path path )
+                {
+                    return context.modelByPath.computeIfAbsent( path, k -> 
findRawModel( path ) );
+                }
+
+                private Model findRawModel( String groupId, String artifactId )
+                {
+                    Source source = getSource( groupId, artifactId );
+                    if ( source != null )
+                    {
+                        try
+                        {
+                            ModelBuildingRequest gaBuildingRequest = new 
FilterModelBuildingRequest( request ) 
+                            {
+                                @Override
+                                public ModelSource getModelSource()
+                                {
+                                    return (ModelSource) source;
+                                }
+                                
+                            };
+                            return readRawModel( gaBuildingRequest, problems );
+                        }
+                        catch ( ModelBuildingException e )
+                        {
+                            // gathered with problem collector
+                        }
+                    }
+                    return null;
+                }
+                
+                private Model findRawModel( Path p )
+                {
+                    if ( !Files.isRegularFile( p ) )
+                    {
+                        throw new IllegalArgumentException( "Not a regular 
file: " + p );
+                    }
+                    
+                    DefaultModelBuildingRequest req = new 
DefaultModelBuildingRequest( request )
+                                    .setPomFile( p.toFile() )
+                                    .setModelSource( new FileModelSource( 
p.toFile() ) );
+                    
+                    try
+                    {
+                        return readRawModel( req, problems );
+                    }
+                    catch ( ModelBuildingException e )
+                    {
+                        // gathered with problem collector
+                    }
+                    return null;
+                }
+            };
+        }
+
+        @Override
+        public TransformerContext build()
+        {
+            return context;
+        }
+
+        public Source getSource( String groupId, String artifactId )
+        {
+            Set<Source> sources = mappedSources.get( new 
DefaultTransformerContext.GAKey( groupId, artifactId ) );
+            if ( sources == null )
+            {
+                return null;
+            }
+            return sources.stream().reduce( ( a, b ) ->

Review comment:
       no, effectively this does:
   
       if (sources.size() == 0)
         return null;
       else if (sources.size() == 1)
           return sources.get(0);
       else
          throw new ISEx();
   
   reduce is a only used when there is something to reduce. In other words, 
there should be at least  2 items.




----------------------------------------------------------------
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]


Reply via email to