Do not create temporary array for module sorting
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / util / ModuleDependencySort.java
index b74b9fcf696e8560c7f4329084f5411f207fdd79..bc980c03a0f6cdd5510fa3f61ad3b44aea40fb5c 100644 (file)
@@ -7,15 +7,14 @@
  */
 package org.opendaylight.yangtools.yang.parser.util;
 
-import static java.util.Arrays.asList;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Function;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
 import java.net.URI;
+import java.util.Arrays;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
@@ -25,16 +24,14 @@ import java.util.Set;
 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
 import org.opendaylight.yangtools.yang.parser.util.TopologicalSort.Node;
 import org.opendaylight.yangtools.yang.parser.util.TopologicalSort.NodeImpl;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * Creates a module dependency graph from provided {@link ModuleBuilder}s and
- * provides a {@link #sort(ModuleBuilder...)} method. It is topological sort and
+ * Creates a module dependency graph from provided {@link Module}s and
+ * provides a {@link #sort(Module...)} method. It is topological sort and
  * returns modules in order in which they should be processed (e.g. if A imports
  * B, sort returns {B, A}).
  */
@@ -42,15 +39,11 @@ public final class ModuleDependencySort {
 
     private static final Date DEFAULT_REVISION = SimpleDateFormatUtil.DEFAULT_DATE_REV;
     private static final Logger LOGGER = LoggerFactory.getLogger(ModuleDependencySort.class);
-    private static final Function<Node, Module> TOPOLOGY_FUNCTION = new Function<TopologicalSort.Node, Module>() {
-        @Override
-        public Module apply(final TopologicalSort.Node input) {
-            if (input == null) {
-                return null;
-            }
-            ModuleOrModuleBuilder moduleOrModuleBuilder = ((ModuleNodeImpl) input).getReference();
-            return moduleOrModuleBuilder.getModule();
+    private static final Function<Node, Module> TOPOLOGY_FUNCTION = input -> {
+        if (input == null) {
+            return null;
         }
+        return ((ModuleNodeImpl) input).getReference();
     };
 
     /**
@@ -59,65 +52,15 @@ public final class ModuleDependencySort {
     private ModuleDependencySort() {
     }
 
-
     /**
-     * Extracts {@link ModuleBuilder} from a {@link ModuleNodeImpl}.
-     */
-    private static final Function<TopologicalSort.Node, ModuleBuilder> NODE_TO_MODULEBUILDER = new Function<TopologicalSort.Node, ModuleBuilder>() {
-        @Override
-        public ModuleBuilder apply(final TopologicalSort.Node input) {
-            // Cast to ModuleBuilder from Node and return
-            if (input == null) {
-                return null;
-            }
-            ModuleOrModuleBuilder moduleOrModuleBuilder = ((ModuleNodeImpl) input).getReference();
-            return moduleOrModuleBuilder.getModuleBuilder();
-        }
-    };
-
-    /**
-     * Topological sort of module builder dependency graph.
+     * Topological sort of module dependency graph.
      *
-     * @param builders builders of Module object
-     * @return Sorted list of Module builders. Modules can be further processed
-     *         in returned order.
-     * @deprecated Pre-Beryllium implementation, scheduled for removal.
-     */
-    @Deprecated
-    public static List<ModuleBuilder> sort(final ModuleBuilder... builders) {
-        return sort(asList(builders));
-    }
-
-    /**
-     * @deprecated Pre-Beryllium implementation, scheduled for removal.
+     * @param modules YANG modules
+     * @return Sorted list of Modules. Modules can be further processed in
+     *         returned order.
      */
-    @Deprecated
-    public static List<ModuleBuilder> sort(final Collection<ModuleBuilder> builders) {
-        List<TopologicalSort.Node> sorted = sortInternal(ModuleOrModuleBuilder.fromAll(
-                Collections.emptySet(),builders));
-        return Lists.transform(sorted, NODE_TO_MODULEBUILDER);
-    }
-
-    public static List<ModuleBuilder> sortWithContext(final SchemaContext context, final ModuleBuilder... builders) {
-        List<ModuleOrModuleBuilder> all = ModuleOrModuleBuilder.fromAll(context.getModules(), asList(builders));
-
-        List<TopologicalSort.Node> sorted = sortInternal(all);
-        // Cast to ModuleBuilder from Node if possible and return
-        return Lists.transform(sorted, new Function<TopologicalSort.Node, ModuleBuilder>() {
-
-            @Override
-            public ModuleBuilder apply(final TopologicalSort.Node input) {
-                if (input == null) {
-                    return null;
-                }
-                ModuleOrModuleBuilder moduleOrModuleBuilder = ((ModuleNodeImpl) input).getReference();
-                if (moduleOrModuleBuilder.isModuleBuilder()) {
-                    return moduleOrModuleBuilder.getModuleBuilder();
-                } else {
-                    return null;
-                }
-            }
-        });
+    public static List<Module> sort(final Module... modules) {
+        return sort(Arrays.asList(modules));
     }
 
     /**
@@ -127,14 +70,13 @@ public final class ModuleDependencySort {
      * @return Sorted list of Modules. Modules can be further processed in
      *         returned order.
      */
-    public static List<Module> sort(final Module... modules) {
-        List<TopologicalSort.Node> sorted = sortInternal(ModuleOrModuleBuilder.fromAll(asList(modules),
-                Collections.emptyList()));
+    public static List<Module> sort(final Iterable<Module> modules) {
+        final List<TopologicalSort.Node> sorted = sortInternal(modules);
         // Cast to Module from Node and return
         return Lists.transform(sorted, TOPOLOGY_FUNCTION);
     }
 
-    private static List<TopologicalSort.Node> sortInternal(final Iterable<ModuleOrModuleBuilder> modules) {
+    private static List<TopologicalSort.Node> sortInternal(final Iterable<Module> modules) {
         Map<String, Map<Date, ModuleNodeImpl>> moduleGraph = createModuleGraph(modules);
 
         Set<TopologicalSort.Node> nodes = Sets.newHashSet();
@@ -148,7 +90,7 @@ public final class ModuleDependencySort {
     }
 
     @VisibleForTesting
-    static Map<String, Map<Date, ModuleNodeImpl>> createModuleGraph(final Iterable<ModuleOrModuleBuilder> builders) {
+    static Map<String, Map<Date, ModuleNodeImpl>> createModuleGraph(final Iterable<Module> builders) {
         Map<String, Map<Date, ModuleNodeImpl>> moduleGraph = Maps.newHashMap();
 
         processModules(moduleGraph, builders);
@@ -161,11 +103,11 @@ public final class ModuleDependencySort {
      * Extract module:revision from module builders
      */
     private static void processDependencies(final Map<String, Map<Date, ModuleNodeImpl>> moduleGraph,
-            final Iterable<ModuleOrModuleBuilder> mmbs) {
-        Map<URI, ModuleOrModuleBuilder> allNS = new HashMap<>();
+            final Iterable<Module> mmbs) {
+        Map<URI, Module> allNS = new HashMap<>();
 
         // Create edges in graph
-        for (ModuleOrModuleBuilder mmb : mmbs) {
+        for (Module module : mmbs) {
             Map<String, Date> imported = Maps.newHashMap();
 
             String fromName;
@@ -173,39 +115,23 @@ public final class ModuleDependencySort {
             Collection<ModuleImport> imports;
             URI ns;
 
-            if (mmb.isModule()) {
-                Module module = mmb.getModule();
-                fromName = module.getName();
-                fromRevision = module.getRevision();
-                imports = module.getImports();
-                ns = module.getNamespace();
-            } else {
-                ModuleBuilder moduleBuilder = mmb.getModuleBuilder();
-                fromName = moduleBuilder.getName();
-                fromRevision = moduleBuilder.getRevision();
-                imports = moduleBuilder.getImports().values();
-                ns = moduleBuilder.getNamespace();
-            }
+            fromName = module.getName();
+            fromRevision = module.getRevision();
+            imports = module.getImports();
+            ns = module.getNamespace();
 
             // check for existence of module with same namespace
             if (allNS.containsKey(ns)) {
-                ModuleOrModuleBuilder mod = allNS.get(ns);
-                String name = null;
-                Date revision = null;
-                if (mod.isModule()) {
-                    name = mod.getModule().getName();
-                    revision = mod.getModule().getRevision();
-                } else if (mod.isModuleBuilder()) {
-                    name = mod.getModuleBuilder().getName();
-                    revision = mod.getModuleBuilder().getRevision();
-                }
-                if (!(fromName.equals(name))) {
+                final Module mod = allNS.get(ns);
+                final String name = mod.getName();
+                final Date revision = mod.getRevision();
+                if (!fromName.equals(name)) {
                     LOGGER.warn(
                             "Error while sorting module [{}, {}]: module with same namespace ({}) already loaded: [{}, {}]",
                             fromName, fromRevision, ns, name, revision);
                 }
             } else {
-                allNS.put(ns, mmb);
+                allNS.put(ns, module);
             }
 
             // no need to check if other Type of object, check is performed in
@@ -279,22 +205,13 @@ public final class ModuleDependencySort {
      * graph
      */
     private static void processModules(final Map<String, Map<Date, ModuleNodeImpl>> moduleGraph,
-            final Iterable<ModuleOrModuleBuilder> builders) {
+            final Iterable<Module> modules) {
 
         // Process nodes
-        for (ModuleOrModuleBuilder momb : builders) {
-
-            String name;
-            Date rev;
-
-            if (momb.isModule()) {
-                name = momb.getModule().getName();
-                rev = momb.getModule().getRevision();
-            } else {
-                name = momb.getModuleBuilder().getName();
-                rev = momb.getModuleBuilder().getRevision();
-            }
+        for (Module momb : modules) {
 
+            String name = momb.getName();
+            Date rev = momb.getRevision();
             if (rev == null) {
                 rev = DEFAULT_REVISION;
             }
@@ -319,12 +236,12 @@ public final class ModuleDependencySort {
     static class ModuleNodeImpl extends NodeImpl {
         private final String name;
         private final Date revision;
-        private final ModuleOrModuleBuilder originalObject;
+        private final Module originalObject;
 
-        public ModuleNodeImpl(final String name, final Date revision, final ModuleOrModuleBuilder builder) {
+        public ModuleNodeImpl(final String name, final Date revision, final Module module) {
             this.name = name;
             this.revision = revision;
-            this.originalObject = builder;
+            this.originalObject = module;
         }
 
         public String getName() {
@@ -378,7 +295,7 @@ public final class ModuleDependencySort {
             return "Module [name=" + name + ", revision=" + formatRevDate(revision) + "]";
         }
 
-        public ModuleOrModuleBuilder getReference() {
+        public Module getReference() {
             return originalObject;
         }