Reformulate StatementContextFactory.createEffective()
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / module / ModuleEffectiveStatementImpl.java
index 9e4df502fb8f94efb002813a715525f3ec0505d3..eaf2fb31401f72f2db909d5941f0d2f304331a84 100644 (file)
@@ -9,17 +9,21 @@ package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.module;
 
 import static com.google.common.base.Verify.verifyNotNull;
 
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableMap.Builder;
-import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Maps;
+import java.util.Collection;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.common.UnqualifiedQName;
 import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.model.api.Submodule;
+import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
 import org.opendaylight.yangtools.yang.model.api.stmt.ExtensionEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.ExtensionEffectiveStatementNamespace;
@@ -30,7 +34,6 @@ import org.opendaylight.yangtools.yang.model.api.stmt.FeatureStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityEffectiveStatementNamespace;
 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.ImportEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixEffectiveStatement;
@@ -39,13 +42,13 @@ import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveModu
 import org.opendaylight.yangtools.yang.parser.spi.ExtensionNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.FeatureNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.IdentityNamespace;
+import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
-import org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToModuleCtx;
 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToModuleCtx;
 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
 
-final class ModuleEffectiveStatementImpl extends AbstractEffectiveModule<ModuleStatement>
-        implements ModuleEffectiveStatement {
+final class ModuleEffectiveStatementImpl extends AbstractEffectiveModule<ModuleStatement, ModuleEffectiveStatement>
+        implements Module, ModuleEffectiveStatement {
     private final ImmutableMap<String, SubmoduleEffectiveStatement> nameToSubmodule;
     private final ImmutableMap<QName, ExtensionEffectiveStatement> qnameToExtension;
     private final ImmutableMap<QName, FeatureEffectiveStatement> qnameToFeature;
@@ -53,26 +56,21 @@ final class ModuleEffectiveStatementImpl extends AbstractEffectiveModule<ModuleS
     private final ImmutableMap<String, ModuleEffectiveStatement> prefixToModule;
     private final ImmutableMap<QNameModule, String> namespaceToPrefix;
     private final @NonNull QNameModule qnameModule;
-    private final ImmutableSet<Module> submodules;
+    private final ImmutableList<Submodule> submodules;
 
-    private ModuleEffectiveStatementImpl(final @NonNull ModuleStmtContext ctx) {
-        super(ctx, findPrefix(ctx.delegate(), "module", ctx.getStatementArgument()));
-        submodules = ctx.getSubmodules();
+    ModuleEffectiveStatementImpl(final Current<UnqualifiedQName, ModuleStatement> stmt,
+            final ImmutableList<? extends EffectiveStatement<?, ?>> substatements,
+            final Collection<? extends Submodule> submodules) {
+        super(stmt, substatements, findPrefix(stmt.caerbannog(), "module",
+            stmt.caerbannog().coerceRawStatementArgument()));
 
-        qnameModule = verifyNotNull(ctx.getFromNamespace(ModuleCtxToModuleQName.class, ctx.delegate()));
+        qnameModule = verifyNotNull(stmt.getFromNamespace(ModuleCtxToModuleQName.class, stmt.caerbannog()));
+        this.submodules = ImmutableList.copyOf(submodules);
 
         final String localPrefix = findFirstEffectiveSubstatementArgument(PrefixEffectiveStatement.class).get();
         final Builder<String, ModuleEffectiveStatement> prefixToModuleBuilder = ImmutableMap.builder();
         prefixToModuleBuilder.put(localPrefix, this);
-
-        streamEffectiveSubstatements(ImportEffectiveStatement.class)
-                .map(imp -> imp.findFirstEffectiveSubstatementArgument(PrefixEffectiveStatement.class).get())
-                .forEach(prefix -> {
-                    final StmtContext<?, ?, ?> importedCtx =
-                            verifyNotNull(ctx.getFromNamespace(ImportPrefixToModuleCtx.class, prefix),
-                                "Failed to resolve prefix %s", prefix);
-                    prefixToModuleBuilder.put(prefix, (ModuleEffectiveStatement) importedCtx.buildEffective());
-                });
+        appendPrefixes(stmt, prefixToModuleBuilder);
         prefixToModule = prefixToModuleBuilder.build();
 
         final Map<QNameModule, String> tmp = Maps.newLinkedHashMapWithExpectedSize(prefixToModule.size() + 1);
@@ -83,29 +81,25 @@ final class ModuleEffectiveStatementImpl extends AbstractEffectiveModule<ModuleS
         namespaceToPrefix = ImmutableMap.copyOf(tmp);
 
         final Map<String, StmtContext<?, ?, ?>> includedSubmodules =
-                ctx.getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToModuleCtx.class);
+                stmt.getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToModuleCtx.class);
         nameToSubmodule = includedSubmodules == null ? ImmutableMap.of()
                 : ImmutableMap.copyOf(Maps.transformValues(includedSubmodules,
                     submodule -> (SubmoduleEffectiveStatement) submodule.buildEffective()));
 
         final Map<QName, StmtContext<?, ExtensionStatement, ExtensionEffectiveStatement>> extensions =
-                ctx.getAllFromCurrentStmtCtxNamespace(ExtensionNamespace.class);
+                stmt.getAllFromCurrentStmtCtxNamespace(ExtensionNamespace.class);
         qnameToExtension = extensions == null ? ImmutableMap.of()
                 : ImmutableMap.copyOf(Maps.transformValues(extensions, StmtContext::buildEffective));
         final Map<QName, StmtContext<?, FeatureStatement, FeatureEffectiveStatement>> features =
-                ctx.getAllFromCurrentStmtCtxNamespace(FeatureNamespace.class);
+                stmt.getAllFromCurrentStmtCtxNamespace(FeatureNamespace.class);
         qnameToFeature = features == null ? ImmutableMap.of()
                 : ImmutableMap.copyOf(Maps.transformValues(features, StmtContext::buildEffective));
         final Map<QName, StmtContext<?, IdentityStatement, IdentityEffectiveStatement>> identities =
-                ctx.getAllFromCurrentStmtCtxNamespace(IdentityNamespace.class);
+                stmt.getAllFromCurrentStmtCtxNamespace(IdentityNamespace.class);
         qnameToIdentity = identities == null ? ImmutableMap.of()
                 : ImmutableMap.copyOf(Maps.transformValues(identities, StmtContext::buildEffective));
     }
 
-    ModuleEffectiveStatementImpl(final StmtContext<String, ModuleStatement, ModuleEffectiveStatement> ctx) {
-        this(ModuleStmtContext.create(ctx));
-    }
-
     @Override
     public @NonNull QNameModule localQNameModule() {
         return qnameModule;
@@ -117,10 +111,15 @@ final class ModuleEffectiveStatementImpl extends AbstractEffectiveModule<ModuleS
     }
 
     @Override
-    public ImmutableSet<Module> getSubmodules() {
+    public Collection<? extends @NonNull Submodule> getSubmodules() {
         return submodules;
     }
 
+    @Override
+    public ModuleEffectiveStatement asEffectiveStatement() {
+        return this;
+    }
+
     @Override
     @SuppressWarnings("unchecked")
     public <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(