Drop unneeded generic type specifiers
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / SchemaContextImpl.java
index 3eb87a9214e24bd84b7e51061f6e29e82aba4a87..c5e87a1053069fff018a7fb5cda871a05e622e17 100644 (file)
@@ -7,92 +7,88 @@
  */
 package org.opendaylight.yangtools.yang.parser.impl;
 
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.ImmutableSetMultimap;
+import com.google.common.collect.Multimaps;
+import com.google.common.collect.SetMultimap;
 import java.net.URI;
-import java.util.Date;
-import java.util.HashSet;
+import java.util.Collection;
+import java.util.Map;
 import java.util.Set;
-
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
+import java.util.TreeMap;
+import javax.annotation.concurrent.Immutable;
 import org.opendaylight.yangtools.yang.model.api.Module;
-import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
-import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
+import org.opendaylight.yangtools.yang.model.util.AbstractSchemaContext;
+import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort;
 
-final class SchemaContextImpl implements SchemaContext {
-    private final Set<Module> modules;
+@Immutable
+final class SchemaContextImpl extends AbstractSchemaContext {
 
-    SchemaContextImpl(final Set<Module> modules) {
-        this.modules = modules;
-    }
+    private  final Map<ModuleIdentifier, String> identifiersToSources;
+    private  final SetMultimap<URI, Module> namespaceToModules;
+    private  final SetMultimap<String, Module> nameToModules;
+    private  final Set<Module> modules;
+
+    SchemaContextImpl(final Set<Module> modules, final Map<ModuleIdentifier, String> identifiersToSources) {
+        this.identifiersToSources = ImmutableMap.copyOf(identifiersToSources);
+
+         /*
+         * Instead of doing this on each invocation of getModules(), pre-compute
+         * it once and keep it around -- better than the set we got in.
+         */
+        this.modules = ImmutableSet.copyOf(ModuleDependencySort.sort(modules.toArray(new Module[modules.size()])));
+
+         /*
+         * The most common lookup is from Namespace->Module.
+         *
+         * RESTCONF performs lookups based on module name only, where it wants
+         * to receive the latest revision
+         *
+         * Invest some quality time in building up lookup tables for both.
+         */
+        final SetMultimap<URI, Module> nsMap = Multimaps.newSetMultimap(
+                new TreeMap<>(), MODULE_SET_SUPPLIER);
+        final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(
+                new TreeMap<>(), MODULE_SET_SUPPLIER);
 
-    @Override
-    public Set<DataSchemaNode> getDataDefinitions() {
-        final Set<DataSchemaNode> dataDefs = new HashSet<DataSchemaNode>();
         for (Module m : modules) {
-            dataDefs.addAll(m.getChildNodes());
+            nameMap.put(m.getName(), m);
+            nsMap.put(m.getNamespace(), m);
         }
-        return dataDefs;
-    }
 
-    @Override
-    public Set<Module> getModules() {
-        return modules;
+        namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
+        nameToModules = ImmutableSetMultimap.copyOf(nameMap);
     }
 
     @Override
-    public Set<NotificationDefinition> getNotifications() {
-        final Set<NotificationDefinition> notifications = new HashSet<NotificationDefinition>();
-        for (Module m : modules) {
-            notifications.addAll(m.getNotifications());
-        }
-        return notifications;
+    protected Map<ModuleIdentifier, String> getIdentifiersToSources(){
+
+        return identifiersToSources;
     }
 
     @Override
-    public Set<RpcDefinition> getOperations() {
-        final Set<RpcDefinition> rpcs = new HashSet<RpcDefinition>();
-        for (Module m : modules) {
-            rpcs.addAll(m.getRpcs());
-        }
-        return rpcs;
+    public Set<Module> getModules(){
+
+        return modules;
     }
 
     @Override
-    public Set<ExtensionDefinition> getExtensions() {
-        final Set<ExtensionDefinition> extensions = new HashSet<ExtensionDefinition>();
-        for (Module m : modules) {
-            extensions.addAll(m.getExtensionSchemaNodes());
-        }
-        return extensions;
+    protected SetMultimap<URI, Module> getNamespaceToModules() {
+
+        return namespaceToModules;
     }
 
     @Override
-    public Module findModuleByName(final String name, final Date revision) {
-        if (name != null) {
-            for (final Module module : modules) {
-                if (revision == null) {
-                    if (module.getName().equals(name)) {
-                        return module;
-                    }
-                } else if (module.getName().equals(name) && module.getRevision().equals(revision)) {
-                    return module;
-                }
-            }
-        }
-        return null;
+    protected SetMultimap<String, Module> getNameToModules() {
+
+        return nameToModules;
     }
 
     @Override
-    public Module findModuleByNamespace(final URI namespace) {
-        if (namespace != null) {
-            for (final Module module : modules) {
-                if (module.getNamespace().equals(namespace)) {
-                    return module;
-                }
-            }
-        }
-        return null;
-    }
+    public String toString() {
 
+        return String.format("SchemaContextImpl{modules=%s}", modules);
+    }
 }