polygene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From paulmer...@apache.org
Subject [6/6] polygene-java git commit: Polish ApplicationModelFactoryImpl
Date Mon, 22 May 2017 15:33:53 GMT
Polish ApplicationModelFactoryImpl

Split long method into smaller ones, for readability


Project: http://git-wip-us.apache.org/repos/asf/polygene-java/repo
Commit: http://git-wip-us.apache.org/repos/asf/polygene-java/commit/c7f705bd
Tree: http://git-wip-us.apache.org/repos/asf/polygene-java/tree/c7f705bd
Diff: http://git-wip-us.apache.org/repos/asf/polygene-java/diff/c7f705bd

Branch: refs/heads/develop
Commit: c7f705bd2075ae6989b412ad294f9793a88188ad
Parents: 21eac24
Author: Paul Merlin <paulmerlin@apache.org>
Authored: Mon May 22 17:17:24 2017 +0200
Committer: Paul Merlin <paulmerlin@apache.org>
Committed: Mon May 22 17:17:24 2017 +0200

----------------------------------------------------------------------
 .../bootstrap/ApplicationModelFactoryImpl.java  | 125 +++++++++++++------
 1 file changed, 86 insertions(+), 39 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/polygene-java/blob/c7f705bd/core/runtime/src/main/java/org/apache/polygene/runtime/bootstrap/ApplicationModelFactoryImpl.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/polygene/runtime/bootstrap/ApplicationModelFactoryImpl.java
b/core/runtime/src/main/java/org/apache/polygene/runtime/bootstrap/ApplicationModelFactoryImpl.java
index 5d3b000..d2c788c 100644
--- a/core/runtime/src/main/java/org/apache/polygene/runtime/bootstrap/ApplicationModelFactoryImpl.java
+++ b/core/runtime/src/main/java/org/apache/polygene/runtime/bootstrap/ApplicationModelFactoryImpl.java
@@ -26,7 +26,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import org.apache.polygene.api.composite.ModelDescriptor;
-import org.apache.polygene.api.structure.Application;
 import org.apache.polygene.api.structure.ApplicationDescriptor;
 import org.apache.polygene.api.structure.Layer;
 import org.apache.polygene.api.util.HierarchicalVisitor;
@@ -53,30 +52,42 @@ public final class ApplicationModelFactoryImpl
 {
     @Override
     public ApplicationDescriptor newApplicationModel( ApplicationAssembly assembly )
-        throws AssemblyException
     {
         AssemblyHelper helper = createAssemblyHelper( assembly );
-
+        AssemblyMaps maps = new AssemblyMaps();
         ApplicationAssemblyImpl applicationAssembly = (ApplicationAssemblyImpl) assembly;
-        ActivatorsModel<Application> applicationActivators = new ActivatorsModel<>(
applicationAssembly.activators() );
-        List<LayerModel> layerModels = new ArrayList<>();
-        final ApplicationModel applicationModel = new ApplicationModel( applicationAssembly.name(),
-                                                                        applicationAssembly.version(),
-                                                                        applicationAssembly.mode(),
-                                                                        applicationAssembly.metaInfo(),
-                                                                        applicationActivators,
-                                                                        layerModels );
-        Map<LayerAssembly, LayerModel> mapAssemblyModel = new HashMap<>();
-        Map<LayerAssembly, List<LayerModel>> mapUsedLayers = new HashMap<>();
-
-        // Build all layers
+
         List<LayerAssemblyImpl> layerAssemblies = new ArrayList<>( applicationAssembly.layerAssemblies()
);
-        for( LayerAssemblyImpl layerAssembly : layerAssemblies )
+        List<LayerModel> layerModels = new ArrayList<>();
+
+        buildAllLayers( helper, maps, layerAssemblies, layerModels );
+        populateUsedLayerModels( maps, layerAssemblies );
+
+        ApplicationModel applicationModel = buildApplicationModel( applicationAssembly, layerModels
);
+        bindApplicationModel( applicationModel );
+        return applicationModel;
+    }
+
+    private AssemblyHelper createAssemblyHelper( ApplicationAssembly assembly )
+    {
+        if( assembly instanceof ApplicationAssemblyImpl )
         {
-            List<LayerModel> usedLayers = new ArrayList<>();
-            mapUsedLayers.put( layerAssembly, usedLayers );
+            ApplicationAssemblyImpl impl = (ApplicationAssemblyImpl) assembly;
+            AssemblyHelper helper = impl.metaInfo().get( AssemblyHelper.class );
+            if( helper != null )
+            {
+                return helper;
+            }
+        }
+        return new AssemblyHelper();
+    }
 
-            UsedLayersModel usedLayersModel = new UsedLayersModel( usedLayers );
+    private void buildAllLayers( AssemblyHelper helper, AssemblyMaps maps,
+                                 List<LayerAssemblyImpl> layerAssemblies, List<LayerModel>
layerModels )
+    {
+        for( LayerAssemblyImpl layerAssembly : layerAssemblies )
+        {
+            UsedLayersModel usedLayersModel = new UsedLayersModel( maps.usedLayersOf( layerAssembly
) );
             List<ModuleModel> moduleModels = new ArrayList<>();
             String name = layerAssembly.name();
             if( name == null )
@@ -84,55 +95,90 @@ public final class ApplicationModelFactoryImpl
                 throw new AssemblyException( "Layer must have name set" );
             }
             ActivatorsModel<Layer> layerActivators = new ActivatorsModel<>( layerAssembly.activators()
);
-            LayerModel layerModel = new LayerModel( name, layerAssembly.metaInfo(), usedLayersModel,
layerActivators, moduleModels );
+            LayerModel layerModel = new LayerModel( name,
+                                                    layerAssembly.metaInfo(),
+                                                    usedLayersModel,
+                                                    layerActivators,
+                                                    moduleModels );
 
             for( ModuleAssemblyImpl moduleAssembly : layerAssembly.moduleAssemblies() )
             {
                 moduleModels.add( moduleAssembly.assembleModule( layerModel, helper ) );
             }
-            mapAssemblyModel.put( layerAssembly, layerModel );
+            maps.addModel( layerAssembly, layerModel );
             layerModels.add( layerModel );
         }
+    }
 
-        // Populate used layer lists
+    private void populateUsedLayerModels( AssemblyMaps maps, List<LayerAssemblyImpl>
layerAssemblies )
+    {
         for( LayerAssemblyImpl layerAssembly : layerAssemblies )
         {
             Set<LayerAssembly> usesLayers = layerAssembly.uses();
-            List<LayerModel> usedLayers = mapUsedLayers.get( layerAssembly );
+            List<LayerModel> usedLayers = maps.usedLayersOf( layerAssembly );
             for( LayerAssembly usesLayer : usesLayers )
             {
-                LayerModel layerModel = mapAssemblyModel.get( usesLayer );
-                usedLayers.add( layerModel );
+                usedLayers.add( maps.modelOf( usesLayer ) );
             }
         }
+    }
+
 
-        // Bind model
+    private ApplicationModel buildApplicationModel( ApplicationAssemblyImpl applicationAssembly,
+                                                    List<LayerModel> layerModels )
+    {
+        return new ApplicationModel( applicationAssembly.name(),
+                                     applicationAssembly.version(),
+                                     applicationAssembly.mode(),
+                                     applicationAssembly.metaInfo(),
+                                     new ActivatorsModel<>( applicationAssembly.activators()
),
+                                     layerModels );
+    }
+
+    private void bindApplicationModel( ApplicationModel applicationModel )
+    {
         // This will resolve all dependencies
         try
         {
-//            applicationModel.bind();
             applicationModel.accept( new BindingVisitor( applicationModel ) );
         }
         catch( BindingException e )
         {
             throw new AssemblyException( "Unable to bind: " + applicationModel, e );
         }
-
-        return applicationModel;
     }
 
-    private AssemblyHelper createAssemblyHelper( ApplicationAssembly assembly )
+    private static class AssemblyMaps
     {
-        if( assembly instanceof ApplicationAssemblyImpl )
+        private final Map<LayerAssembly, LayerModel> mapAssemblyModel = new HashMap<>();
+        private final Map<LayerModel, LayerAssembly> mapModelAssembly = new HashMap<>();
+        private final Map<LayerAssembly, List<LayerModel>> mapUsedLayers = new
HashMap<>();
+
+        void addModel( LayerAssembly assembly, LayerModel model )
         {
-            ApplicationAssemblyImpl impl = (ApplicationAssemblyImpl) assembly;
-            AssemblyHelper helper = impl.metaInfo().get( AssemblyHelper.class );
-            if( helper != null )
+            mapAssemblyModel.put( assembly, model );
+            mapModelAssembly.put( model, assembly );
+            usedLayersOf( assembly );
+        }
+
+        LayerAssembly assemblyOf( LayerModel model )
+        {
+            return mapModelAssembly.get( model );
+        }
+
+        LayerModel modelOf( LayerAssembly assembly )
+        {
+            return mapAssemblyModel.get( assembly );
+        }
+
+        List<LayerModel> usedLayersOf( LayerAssembly assembly )
+        {
+            if( !mapUsedLayers.containsKey( assembly ) )
             {
-                return helper;
+                mapUsedLayers.put( assembly, new ArrayList<>() );
             }
+            return mapUsedLayers.get( assembly );
         }
-        return new AssemblyHelper();
     }
 
     private static class BindingVisitor
@@ -165,12 +211,14 @@ public final class ApplicationModelFactoryImpl
             else if( visited instanceof CompositeMethodModel )
             {
                 compositeMethodModel = (CompositeMethodModel) visited;
-                resolution = new Resolution( applicationModel, layer, module, objectDescriptor,
compositeMethodModel, null );
+                resolution = new Resolution( applicationModel, layer, module,
+                                             objectDescriptor, compositeMethodModel, null
);
             }
             else if( visited instanceof ModelDescriptor )
             {
                 objectDescriptor = (ModelDescriptor) visited;
-                resolution = new Resolution( applicationModel, layer, module, objectDescriptor,
null, null );
+                resolution = new Resolution( applicationModel, layer, module,
+                                             objectDescriptor, null, null );
             }
             else if( visited instanceof InjectedFieldModel )
             {
@@ -191,7 +239,6 @@ public final class ApplicationModelFactoryImpl
 
         @Override
         public boolean visitLeave( Object visited )
-            throws BindingException
         {
             return true;
         }


Mime
View raw message