Bug 4412: New yang parser effective statements cleanup
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / EffectiveSchemaContext.java
index 3de0b1eafaff9c621d82d4d122da1388f8dba6fe..c76b86f82809415f99e632ddbc5798d118c2c9a0 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
 
+import com.google.common.base.Optional;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
@@ -15,9 +16,7 @@ import com.google.common.collect.Multimaps;
 import com.google.common.collect.SetMultimap;
 import java.net.URI;
 import java.util.Collection;
-import java.util.HashMap;
 import java.util.HashSet;
-import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -27,17 +26,18 @@ import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
+import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleIdentifierImpl;
 import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort;
 
-public class EffectiveSchemaContext extends AbstractEffectiveSchemaContext {
+public final class EffectiveSchemaContext extends AbstractEffectiveSchemaContext {
 
-    private final Map<ModuleIdentifier, String> identifiersToSources;
     private final SetMultimap<URI, Module> namespaceToModules;
     private final SetMultimap<String, Module> nameToModules;
     private final Set<Module> modules;
 
-    private final ImmutableList<DeclaredStatement<?>> rootDeclaredStatements;
-    private final ImmutableList<EffectiveStatement<?, ?>> rootEffectiveStatements;
+    private final List<DeclaredStatement<?>> rootDeclaredStatements;
+    private final List<EffectiveStatement<?, ?>> rootEffectiveStatements;
+    private final Set<ModuleIdentifier> moduleIdentifiers;
 
     public EffectiveSchemaContext(final List<DeclaredStatement<?>> rootDeclaredStatements,
             final List<EffectiveStatement<?, ?>> rootEffectiveStatements) {
@@ -60,22 +60,19 @@ public class EffectiveSchemaContext extends AbstractEffectiveSchemaContext {
                 new TreeMap<URI, Collection<Module>>(), MODULE_SET_SUPPLIER);
         final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(
                 new TreeMap<String, Collection<Module>>(), MODULE_SET_SUPPLIER);
-        final Map<ModuleIdentifier, String> isMap = new LinkedHashMap<>();
-
+        Set<ModuleIdentifier> modIdBuilder = new HashSet<>();
         for (Module m : modulesInit) {
             nameMap.put(m.getName(), m);
             nsMap.put(m.getNamespace(), m);
-            isMap.put(m, m.getSource());
+            modIdBuilder.add(new ModuleIdentifierImpl(m.getName(), Optional.of(m.getNamespace()), Optional.of(m.getRevision())));
         }
 
         namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
         nameToModules = ImmutableSetMultimap.copyOf(nameMap);
-        identifiersToSources = ImmutableMap.copyOf(isMap);
-
+        moduleIdentifiers = ImmutableSet.copyOf(modIdBuilder);
     }
 
-    public EffectiveSchemaContext(final Set<Module> modules, final Map<ModuleIdentifier, String> identifiersToSources) {
-        this.identifiersToSources = ImmutableMap.copyOf(identifiersToSources);
+    public EffectiveSchemaContext(final Set<Module> modules) {
 
          /*
          * Instead of doing this on each invocation of getModules(), pre-compute
@@ -96,37 +93,36 @@ public class EffectiveSchemaContext extends AbstractEffectiveSchemaContext {
         final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(
                 new TreeMap<String, Collection<Module>>(), MODULE_SET_SUPPLIER);
 
+        Set<ModuleIdentifier> modIdBuilder = new HashSet<>();
         for (Module m : modules) {
             nameMap.put(m.getName(), m);
             nsMap.put(m.getNamespace(), m);
+            modIdBuilder.add(new ModuleIdentifierImpl(m.getName(), Optional.of(m.getNamespace()), Optional.of(m.getRevision())));
         }
 
         namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
         nameToModules = ImmutableSetMultimap.copyOf(nameMap);
+        moduleIdentifiers = ImmutableSet.copyOf(modIdBuilder);
 
-        rootDeclaredStatements = null;
-        rootEffectiveStatements = null;
+        rootDeclaredStatements = ImmutableList.of();
+        rootEffectiveStatements = ImmutableList.of();
     }
 
     public static SchemaContext resolveSchemaContext(final Set<Module> modules) {
-        Map<ModuleIdentifier, String> identifiersToSources = new HashMap<>();
-        for (Module module : modules) {
-            identifiersToSources.put(module, module.getSource());
-        }
-        return new EffectiveSchemaContext(modules, identifiersToSources);
+       return new EffectiveSchemaContext(modules);
     }
 
-    public ImmutableList<DeclaredStatement<?>> getRootDeclaredStatements() {
+    public List<DeclaredStatement<?>> getRootDeclaredStatements() {
         return rootDeclaredStatements;
     }
 
-    public ImmutableList<EffectiveStatement<?, ?>> getRootEffectiveStatements() {
+    public List<EffectiveStatement<?, ?>> getRootEffectiveStatements() {
         return rootEffectiveStatements;
     }
 
     @Override
     protected Map<ModuleIdentifier, String> getIdentifiersToSources() {
-        return identifiersToSources;
+        return ImmutableMap.of();
     }
 
     @Override
@@ -144,6 +140,11 @@ public class EffectiveSchemaContext extends AbstractEffectiveSchemaContext {
         return nameToModules;
     }
 
+    @Override
+    public Set<ModuleIdentifier> getAllModuleIdentifiers() {
+        return moduleIdentifiers;
+    }
+
     @Override
     public String toString() {
         return String.format("SchemaContextImpl{modules=%s}", modules);