Optimize simple declared statements
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / belongs_to / BelongsToStatementSupport.java
index 93e2d0b46a1b28d5454ad4751dbe46f761119887..7979f3f473819d89d37465567e415e10e3bc62af 100644 (file)
@@ -9,14 +9,17 @@ package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.belongs_to;
 
 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.findFirstDeclaredSubstatement;
 
+import com.google.common.collect.ImmutableList;
 import java.util.Collection;
 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
+import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.BelongsToEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.BelongsToStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
-import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
+import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStringStatementSupport;
 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
@@ -30,47 +33,37 @@ import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToModuleContext;
 import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToPrefixToModuleCtx;
 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNamespaceForBelongsTo;
 
-public class BelongsToStatementSupport
-        extends
-        AbstractStatementSupport<String, BelongsToStatement, EffectiveStatement<String, BelongsToStatement>> {
+public final class BelongsToStatementSupport
+        extends BaseStringStatementSupport<BelongsToStatement, BelongsToEffectiveStatement> {
     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR =
             SubstatementValidator.builder(YangStmtMapping.BELONGS_TO).addMandatory(YangStmtMapping.PREFIX).build();
+    private static final BelongsToStatementSupport INSTANCE = new BelongsToStatementSupport();
 
-    public BelongsToStatementSupport() {
+    private BelongsToStatementSupport() {
         super(YangStmtMapping.BELONGS_TO);
     }
 
-    @Override
-    public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
-        return value;
-    }
-
-    @Override
-    public BelongsToStatement createDeclared(
-            final StmtContext<String, BelongsToStatement, ?> ctx) {
-        return new BelongsToStatementImpl(ctx);
+    public static BelongsToStatementSupport getInstance() {
+        return INSTANCE;
     }
 
     @Override
-    public EffectiveStatement<String, BelongsToStatement> createEffective(
-            final StmtContext<String, BelongsToStatement, EffectiveStatement<String, BelongsToStatement>> ctx) {
-        return new BelongsToEffectiveStatementImpl(ctx);
+    public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
+        return value;
     }
 
     @Override
-    public void onPreLinkageDeclared(final StmtContext.Mutable<String, BelongsToStatement,
-            EffectiveStatement<String, BelongsToStatement>> belongsToCtx) {
-        belongsToCtx.addRequiredSource(getSourceIdentifier(belongsToCtx));
+    public void onPreLinkageDeclared(final Mutable<String, BelongsToStatement, BelongsToEffectiveStatement> ctx) {
+        ctx.addRequiredSource(getSourceIdentifier(ctx));
     }
 
     @Override
-    public void onLinkageDeclared(final Mutable<String, BelongsToStatement,
-            EffectiveStatement<String, BelongsToStatement>> belongsToCtx) {
+    public void onLinkageDeclared(final Mutable<String, BelongsToStatement, BelongsToEffectiveStatement> belongsToCtx) {
         ModelActionBuilder belongsToAction = belongsToCtx.newInferenceAction(ModelProcessingPhase.SOURCE_LINKAGE);
 
         final SourceIdentifier belongsToSourceIdentifier = getSourceIdentifier(belongsToCtx);
         final Prerequisite<StmtContext<?, ?, ?>> belongsToPrereq = belongsToAction.requiresCtx(belongsToCtx,
-            ModuleNamespaceForBelongsTo.class, belongsToCtx.getStatementArgument(),
+            ModuleNamespaceForBelongsTo.class, belongsToCtx.coerceStatementArgument(),
             ModelProcessingPhase.SOURCE_LINKAGE);
 
         belongsToAction.apply(new InferenceAction() {
@@ -80,12 +73,12 @@ public class BelongsToStatementSupport
 
                 belongsToCtx.addToNs(BelongsToModuleContext.class, belongsToSourceIdentifier, belongsToModuleCtx);
                 belongsToCtx.addToNs(BelongsToPrefixToModuleCtx.class,
-                    findFirstDeclaredSubstatement(belongsToCtx, PrefixStatement.class).getStatementArgument(),
+                    findFirstDeclaredSubstatement(belongsToCtx, PrefixStatement.class).coerceStatementArgument(),
                     belongsToModuleCtx);
             }
 
             @Override
-            public void prerequisiteFailed(final Collection<? extends ModelActionBuilder.Prerequisite<?>> failed) {
+            public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
                 if (failed.contains(belongsToPrereq)) {
                     throw new InferenceException(belongsToCtx.getStatementSourceReference(),
                         "Module '%s' from belongs-to was not found", belongsToCtx.getStatementArgument());
@@ -94,9 +87,34 @@ public class BelongsToStatementSupport
         });
     }
 
+    @Override
+    protected BelongsToStatement createDeclared(final StmtContext<String, BelongsToStatement, ?> ctx,
+            final ImmutableList<? extends DeclaredStatement<?>> substatements) {
+        return new RegularBelongsToStatement(ctx, substatements);
+    }
+
+    @Override
+    protected BelongsToStatement createEmptyDeclared(final StmtContext<String, BelongsToStatement, ?> ctx) {
+        return new EmptyBelongsToStatement(ctx);
+    }
+
+    @Override
+    protected BelongsToEffectiveStatement createEffective(
+            final StmtContext<String, BelongsToStatement, BelongsToEffectiveStatement> ctx,
+            final BelongsToStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        return new RegularBelongsToEffectiveStatement(declared, substatements);
+    }
+
+    @Override
+    protected BelongsToEffectiveStatement createEmptyEffective(
+            final StmtContext<String, BelongsToStatement, BelongsToEffectiveStatement> ctx,
+            final BelongsToStatement declared) {
+        return new EmptyBelongsToEffectiveStatement(declared);
+    }
+
     private static SourceIdentifier getSourceIdentifier(final StmtContext<String, BelongsToStatement,
-            EffectiveStatement<String, BelongsToStatement>> belongsToCtx) {
-        return RevisionSourceIdentifier.create(belongsToCtx.getStatementArgument());
+            BelongsToEffectiveStatement> belongsToCtx) {
+        return RevisionSourceIdentifier.create(belongsToCtx.coerceStatementArgument());
     }
 
     @Override