Move more statements
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / choice / ChoiceStatementSupport.java
index 9ec26068213a787fc8edb7ee390824d46b3896f2..48449760d1138211e72f1c7b9b921a05f1a34462 100644 (file)
@@ -11,6 +11,7 @@ import static java.util.Objects.requireNonNull;
 
 import com.google.common.annotations.Beta;
 import com.google.common.collect.ImmutableList;
+import java.util.Collection;
 import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.common.QName;
@@ -26,11 +27,13 @@ import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.DefaultEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatements;
+import org.opendaylight.yangtools.yang.model.ri.stmt.EffectiveStatements;
+import org.opendaylight.yangtools.yang.model.spi.meta.EffectiveStatementMixins.EffectiveStatementWithFlags.FlagsBuilder;
+import org.opendaylight.yangtools.yang.model.spi.meta.SubstatementIndexingException;
 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.YangValidationBundles;
-import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseSchemaTreeStatementSupport;
-import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStatementMixins.EffectiveStatementWithFlags.FlagsBuilder;
-import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.SubstatementIndexingException;
 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.case_.CaseStatementSupport;
+import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractSchemaTreeStatementSupport;
 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ImplicitParentAwareStatementSupport;
 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
@@ -41,7 +44,7 @@ import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 
 @Beta
 public final class ChoiceStatementSupport
-        extends BaseSchemaTreeStatementSupport<ChoiceStatement, ChoiceEffectiveStatement>
+        extends AbstractSchemaTreeStatementSupport<ChoiceStatement, ChoiceEffectiveStatement>
         implements ImplicitParentAwareStatementSupport {
     private static final @NonNull ChoiceStatementSupport RFC6020_INSTANCE = new ChoiceStatementSupport(
         SubstatementValidator.builder(YangStmtMapping.CHOICE)
@@ -86,7 +89,7 @@ public final class ChoiceStatementSupport
     private final CaseStatementSupport implicitCase;
 
     private ChoiceStatementSupport(final SubstatementValidator validator, final CaseStatementSupport implicitCase) {
-        super(YangStmtMapping.CHOICE, StatementPolicy.legacyDeclaredCopy());
+        super(YangStmtMapping.CHOICE, instantiatedPolicy());
         this.validator = requireNonNull(validator);
         this.implicitCase = requireNonNull(implicitCase);
     }
@@ -113,12 +116,19 @@ public final class ChoiceStatementSupport
     @Override
     protected ChoiceStatement createDeclared(@NonNull final StmtContext<QName, ChoiceStatement, ?> ctx,
             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
-        return new RegularChoiceStatement(ctx.getArgument(), substatements);
+        return DeclaredStatements.createChoice(ctx.getArgument(), substatements);
     }
 
     @Override
     protected ChoiceStatement createEmptyDeclared(@NonNull final StmtContext<QName, ChoiceStatement, ?> ctx) {
-        return new EmptyChoiceStatement(ctx.getArgument());
+        return DeclaredStatements.createChoice(ctx.getArgument());
+    }
+
+    @Override
+    public ChoiceEffectiveStatement copyEffective(final Current<QName, ChoiceStatement> stmt,
+            final ChoiceEffectiveStatement original) {
+        return EffectiveStatements.copyChoice(original, stmt.effectivePath(),
+            computeFlags(stmt, original.effectiveSubstatements()), stmt.original(ChoiceSchemaNode.class));
     }
 
     @Override
@@ -141,20 +151,24 @@ public final class ChoiceStatementSupport
             defaultCase = null;
         }
 
-        final int flags = new FlagsBuilder()
-                .setHistory(stmt.history())
-                .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
-                .setConfiguration(stmt.effectiveConfig().asNullable())
-                .setMandatory(findFirstArgument(substatements, MandatoryEffectiveStatement.class, Boolean.FALSE))
-                .toFlags();
         try {
-            return new ChoiceEffectiveStatementImpl(stmt.declared(), substatements, flags, stmt.wrapSchemaPath(),
-                defaultCase, (ChoiceSchemaNode) stmt.original());
+            return EffectiveStatements.createChoice(stmt.declared(), stmt.effectivePath(),
+                computeFlags(stmt, substatements), substatements, defaultCase, stmt.original(ChoiceSchemaNode.class));
         } catch (SubstatementIndexingException e) {
             throw new SourceException(e.getMessage(), stmt, e);
         }
     }
 
+    private static int computeFlags(final Current<?, ?> stmt,
+            final Collection<? extends EffectiveStatement<?, ?>> substatements) {
+        return new FlagsBuilder()
+            .setHistory(stmt.history())
+            .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
+            .setConfiguration(stmt.effectiveConfig().asNullable())
+            .setMandatory(findFirstArgument(substatements, MandatoryEffectiveStatement.class, Boolean.FALSE))
+            .toFlags();
+    }
+
     private static CaseSchemaNode findCase(final QName qname,
             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
         for (final EffectiveStatement<?, ?> effectiveStatement : substatements) {