Migrate UnionSpecificationSupport 01/90901/2
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 2 Jul 2020 21:44:23 +0000 (23:44 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 2 Jul 2020 22:31:55 +0000 (00:31 +0200)
UnionSpecificationSupport can use memory-efficient representation
through BaseStatementSupport. Furthermore we can reuse
TypeEffectiveStatementImpl instead of brewing a separate effective
implementation.

JIRA: YANGTOOLS-1065
Change-Id: I93ad3126e000c489776ea92aed58e9183e93da42
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/UnionSpecificationEffectiveStatement.java [deleted file]
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/UnionSpecificationImpl.java
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/UnionSpecificationSupport.java

diff --git a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/UnionSpecificationEffectiveStatement.java b/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/UnionSpecificationEffectiveStatement.java
deleted file mode 100644 (file)
index 6ea6895..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
-
-import org.eclipse.jdt.annotation.NonNull;
-import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
-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.api.type.UnionTypeDefinition;
-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.DeclaredEffectiveStatementBase;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
-
-final class UnionSpecificationEffectiveStatement extends DeclaredEffectiveStatementBase<String, UnionSpecification>
-        implements TypeEffectiveStatement<UnionSpecification> {
-
-    private final @NonNull UnionTypeDefinition typeDefinition;
-
-    UnionSpecificationEffectiveStatement(
-            final StmtContext<String, UnionSpecification, EffectiveStatement<String, UnionSpecification>> ctx) {
-        super(ctx);
-
-        final UnionTypeBuilder builder = BaseTypes.unionTypeBuilder(ctx.getSchemaPath().get());
-
-        for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
-            if (stmt instanceof TypeEffectiveStatement) {
-                builder.addType(((TypeEffectiveStatement<?>)stmt).getTypeDefinition());
-            }
-            if (stmt instanceof UnknownSchemaNode) {
-                builder.addUnknownSchemaNode((UnknownSchemaNode)stmt);
-            }
-        }
-
-        typeDefinition = builder.build();
-    }
-
-    @Override
-    public UnionTypeDefinition getTypeDefinition() {
-        return typeDefinition;
-    }
-}
index 833dd04a64a6372d92933a85ac319e70e831e536..de5cac415b078b296a0a6b8bab0a8e89cfc76c2d 100644 (file)
@@ -7,12 +7,15 @@
  */
 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
 
+import com.google.common.collect.ImmutableList;
+import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.UnionSpecification;
-import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
+import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractDeclaredStatement.WithRawStringArgument.WithSubstatements;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 
-final class UnionSpecificationImpl extends AbstractDeclaredStatement<String> implements UnionSpecification {
-    UnionSpecificationImpl(final StmtContext<String, UnionSpecification, ?> context) {
-        super(context);
+final class UnionSpecificationImpl extends WithSubstatements implements UnionSpecification {
+    UnionSpecificationImpl(final StmtContext<String, ?, ?> context,
+            final ImmutableList<? extends DeclaredStatement<?>> substatements) {
+        super(context, substatements);
     }
 }
index 3fcc5f7e20c08183724e837bdb1df742eed3556a..f2bd4b6bd53f7992e300ca5b92a11370291650e6 100644 (file)
@@ -7,15 +7,21 @@
  */
 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
 
+import com.google.common.collect.ImmutableList;
 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.TypeEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.UnionSpecification;
-import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
+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.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
+import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 
-final class UnionSpecificationSupport extends
-    AbstractStatementSupport<String, UnionSpecification, EffectiveStatement<String, UnionSpecification>> {
+final class UnionSpecificationSupport
+        extends BaseStatementSupport<String, UnionSpecification, EffectiveStatement<String, UnionSpecification>> {
     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
         .TYPE)
         .addMultiple(YangStmtMapping.TYPE)
@@ -31,18 +37,50 @@ final class UnionSpecificationSupport extends
     }
 
     @Override
-    public UnionSpecification createDeclared(final StmtContext<String, UnionSpecification, ?> ctx) {
-        return new UnionSpecificationImpl(ctx);
+    protected SubstatementValidator getSubstatementValidator() {
+        return SUBSTATEMENT_VALIDATOR;
     }
 
     @Override
-    public EffectiveStatement<String, UnionSpecification> createEffective(
-            final StmtContext<String, UnionSpecification, EffectiveStatement<String, UnionSpecification>> ctx) {
-        return new UnionSpecificationEffectiveStatement(ctx);
+    protected UnionSpecification createDeclared(final StmtContext<String, UnionSpecification, ?> ctx,
+            final ImmutableList<? extends DeclaredStatement<?>> substatements) {
+        return new UnionSpecificationImpl(ctx, substatements);
     }
 
     @Override
-    protected SubstatementValidator getSubstatementValidator() {
-        return SUBSTATEMENT_VALIDATOR;
+    protected UnionSpecification createEmptyDeclared(final StmtContext<String, UnionSpecification, ?> ctx) {
+        throw noType(ctx);
+    }
+
+    @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());
+
+        for (final EffectiveStatement<?, ?> stmt : substatements) {
+            if (stmt instanceof TypeEffectiveStatement) {
+                builder.addType(((TypeEffectiveStatement<?>)stmt).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);
+    }
+
+    private static SourceException noType(final StmtContext<?, ?, ?> ctx) {
+        /*
+         *  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());
     }
-}
\ No newline at end of file
+}