Promote BaseStatementSupport to parser.spi.meta
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / type / UnionSpecificationSupport.java
index 00d3c52c79f29d423f6135065721994c5fef3e7c..3ddcb878864a993d57ee49dafa6b446aa33dc8b4 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
 
 import com.google.common.collect.ImmutableList;
+import org.eclipse.jdt.annotation.NonNull;
 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;
@@ -15,20 +16,22 @@ import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.UnionSpecification;
 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
 import org.opendaylight.yangtools.yang.model.util.type.UnionTypeBuilder;
-import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
+import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
+import org.opendaylight.yangtools.yang.parser.spi.meta.CommonStmtCtx;
+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.meta.SubstatementValidator;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 
 final class UnionSpecificationSupport
-        extends BaseStatementSupport<String, UnionSpecification, EffectiveStatement<String, UnionSpecification>> {
+        extends AbstractStatementSupport<String, UnionSpecification, EffectiveStatement<String, UnionSpecification>> {
     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
         .TYPE)
         .addMultiple(YangStmtMapping.TYPE)
         .build();
 
     UnionSpecificationSupport() {
-        super(YangStmtMapping.TYPE);
+        super(YangStmtMapping.TYPE, CopyPolicy.DECLARED_COPY);
     }
 
     @Override
@@ -44,7 +47,7 @@ final class UnionSpecificationSupport
     @Override
     protected UnionSpecification createDeclared(final StmtContext<String, UnionSpecification, ?> ctx,
             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
-        return new UnionSpecificationImpl(ctx.coerceRawStatementArgument(), substatements);
+        return new UnionSpecificationImpl(ctx.getRawArgument(), substatements);
     }
 
     @Override
@@ -54,33 +57,30 @@ final class UnionSpecificationSupport
 
     @Override
     protected EffectiveStatement<String, UnionSpecification> createEffective(
-            final StmtContext<String, UnionSpecification, EffectiveStatement<String, UnionSpecification>> ctx,
-            final UnionSpecification declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
-        final UnionTypeBuilder builder = BaseTypes.unionTypeBuilder(ctx.getSchemaPath().get());
+            final Current<String, UnionSpecification> stmt,
+            final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        if (substatements.isEmpty()) {
+            throw noType(stmt);
+        }
+
+        final UnionTypeBuilder builder = BaseTypes.unionTypeBuilder(stmt.wrapSchemaPath());
 
-        for (final EffectiveStatement<?, ?> stmt : substatements) {
-            if (stmt instanceof TypeEffectiveStatement) {
-                builder.addType(((TypeEffectiveStatement<?>)stmt).getTypeDefinition());
+        for (final EffectiveStatement<?, ?> subStmt : substatements) {
+            if (subStmt instanceof TypeEffectiveStatement) {
+                builder.addType(((TypeEffectiveStatement<?>)subStmt).getTypeDefinition());
             }
         }
 
-        return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
-    }
-
-    @Override
-    protected EffectiveStatement<String, UnionSpecification> createEmptyEffective(
-            final StmtContext<String, UnionSpecification, EffectiveStatement<String, UnionSpecification>> ctx,
-            final UnionSpecification declared) {
-        throw noType(ctx);
+        return new TypeEffectiveStatementImpl<>(stmt.declared(), substatements, builder);
     }
 
-    private static SourceException noType(final StmtContext<?, ?, ?> ctx) {
+    private static SourceException noType(final @NonNull CommonStmtCtx stmt) {
         /*
          *  https://tools.ietf.org/html/rfc7950#section-9.12
          *
          *     When the type is "union", the "type" statement (Section 7.4) MUST be
          *     present.
          */
-        return new SourceException("At least one type statement has to be present", ctx.getStatementSourceReference());
+        return new SourceException("At least one type statement has to be present", stmt);
     }
 }