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

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

diff --git a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/EnumSpecificationEffectiveStatement.java b/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/EnumSpecificationEffectiveStatement.java
deleted file mode 100644 (file)
index 47e78b9..0000000
+++ /dev/null
@@ -1,75 +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 java.util.Optional;
-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.EnumEffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.EnumSpecification;
-import org.opendaylight.yangtools.yang.model.api.stmt.ValueEffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
-import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
-import org.opendaylight.yangtools.yang.model.util.type.EnumerationTypeBuilder;
-import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.DeclaredEffectiveStatementBase;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
-import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
-
-final class EnumSpecificationEffectiveStatement extends DeclaredEffectiveStatementBase<String, EnumSpecification>
-        implements TypeEffectiveStatement<EnumSpecification> {
-
-    private final @NonNull EnumTypeDefinition typeDefinition;
-
-    EnumSpecificationEffectiveStatement(
-            final StmtContext<String, EnumSpecification, EffectiveStatement<String, EnumSpecification>> ctx) {
-        super(ctx);
-
-        final EnumerationTypeBuilder builder = BaseTypes.enumerationTypeBuilder(ctx.getSchemaPath().get());
-        Integer highestValue = null;
-        for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
-            if (stmt instanceof EnumEffectiveStatement) {
-                final EnumEffectiveStatement enumSubStmt = (EnumEffectiveStatement) stmt;
-
-                final Optional<Integer> declaredValue =
-                        enumSubStmt.findFirstEffectiveSubstatementArgument(ValueEffectiveStatement.class);
-                final int effectiveValue;
-                if (declaredValue.isEmpty()) {
-                    if (highestValue != null) {
-                        SourceException.throwIf(highestValue == 2147483647, ctx.getStatementSourceReference(),
-                                "Enum '%s' must have a value statement", enumSubStmt);
-                        effectiveValue = highestValue + 1;
-                    } else {
-                        effectiveValue = 0;
-                    }
-                } else {
-                    effectiveValue = declaredValue.orElseThrow();
-                }
-
-                final EnumPair pair = EffectiveTypeUtil.buildEnumPair(enumSubStmt, effectiveValue);
-                if (highestValue == null || highestValue < pair.getValue()) {
-                    highestValue = pair.getValue();
-                }
-
-                builder.addEnum(pair);
-            }
-            if (stmt instanceof UnknownSchemaNode) {
-                builder.addUnknownSchemaNode((UnknownSchemaNode) stmt);
-            }
-        }
-
-        typeDefinition = builder.build();
-    }
-
-    @Override
-    public EnumTypeDefinition getTypeDefinition() {
-        return typeDefinition;
-    }
-}
index 2d760f1b7fded00c34f4dc615ec3025566bc9c07..8b65541942130edad996a564cfeb7104d705478a 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.EnumSpecification;
-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 EnumSpecificationImpl extends AbstractDeclaredStatement<String> implements EnumSpecification {
-    EnumSpecificationImpl(final StmtContext<String, EnumSpecification, ?> context) {
-        super(context);
+final class EnumSpecificationImpl extends WithSubstatements implements EnumSpecification {
+    EnumSpecificationImpl(final StmtContext<String, ?, ?> context,
+            final ImmutableList<? extends DeclaredStatement<?>> substatements) {
+        super(context, substatements);
     }
 }
index 01aea391f8859dd810ab93c962705aa4ad433253..33998b07abceb989b2f5596fd568119e0310e4fe 100644 (file)
@@ -7,19 +7,26 @@
  */
 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
 
+import com.google.common.collect.ImmutableList;
+import java.util.Optional;
 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.EnumEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.EnumSpecification;
-import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
+import org.opendaylight.yangtools.yang.model.api.stmt.ValueEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
+import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
+import org.opendaylight.yangtools.yang.model.util.type.EnumerationTypeBuilder;
+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 EnumSpecificationSupport
-        extends AbstractStatementSupport<String, EnumSpecification, EffectiveStatement<String, EnumSpecification>> {
-    private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
-        .TYPE)
-        .addMultiple(YangStmtMapping.ENUM)
-        .build();
+        extends BaseStatementSupport<String, EnumSpecification, EffectiveStatement<String, EnumSpecification>> {
+    private static final SubstatementValidator SUBSTATEMENT_VALIDATOR =
+            SubstatementValidator.builder(YangStmtMapping.TYPE).addMultiple(YangStmtMapping.ENUM).build();
 
     EnumSpecificationSupport() {
         super(YangStmtMapping.TYPE);
@@ -31,18 +38,73 @@ final class EnumSpecificationSupport
     }
 
     @Override
-    public EnumSpecification createDeclared(final StmtContext<String, EnumSpecification, ?> ctx) {
-        return new EnumSpecificationImpl(ctx);
+    protected SubstatementValidator getSubstatementValidator() {
+        return SUBSTATEMENT_VALIDATOR;
     }
 
     @Override
-    public EffectiveStatement<String, EnumSpecification> createEffective(
-            final StmtContext<String, EnumSpecification, EffectiveStatement<String, EnumSpecification>> ctx) {
-        return new EnumSpecificationEffectiveStatement(ctx);
+    protected EnumSpecification createDeclared(final StmtContext<String, EnumSpecification, ?> ctx,
+            final ImmutableList<? extends DeclaredStatement<?>> substatements) {
+        return new EnumSpecificationImpl(ctx, substatements);
     }
 
     @Override
-    protected SubstatementValidator getSubstatementValidator() {
-        return SUBSTATEMENT_VALIDATOR;
+    protected EnumSpecification createEmptyDeclared(final StmtContext<String, EnumSpecification, ?> ctx) {
+        throw noEnum(ctx);
     }
+
+    @Override
+    protected EffectiveStatement<String, EnumSpecification> createEffective(
+            final StmtContext<String, EnumSpecification, EffectiveStatement<String, EnumSpecification>> ctx,
+            final EnumSpecification declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        final EnumerationTypeBuilder builder = BaseTypes.enumerationTypeBuilder(ctx.getSchemaPath().get());
+        Integer highestValue = null;
+        for (final EffectiveStatement<?, ?> stmt : substatements) {
+            if (stmt instanceof EnumEffectiveStatement) {
+                final EnumEffectiveStatement enumSubStmt = (EnumEffectiveStatement) stmt;
+
+                final Optional<Integer> declaredValue =
+                        enumSubStmt.findFirstEffectiveSubstatementArgument(ValueEffectiveStatement.class);
+                final int effectiveValue;
+                if (declaredValue.isEmpty()) {
+                    if (highestValue != null) {
+                        SourceException.throwIf(highestValue == 2147483647, ctx.getStatementSourceReference(),
+                                "Enum '%s' must have a value statement", enumSubStmt);
+                        effectiveValue = highestValue + 1;
+                    } else {
+                        effectiveValue = 0;
+                    }
+                } else {
+                    effectiveValue = declaredValue.orElseThrow();
+                }
+
+                final EnumPair pair = EffectiveTypeUtil.buildEnumPair(enumSubStmt, effectiveValue);
+                if (highestValue == null || highestValue < pair.getValue()) {
+                    highestValue = pair.getValue();
+                }
+
+                builder.addEnum(pair);
+            }
+        }
+
+        return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
+    }
+
+    @Override
+    protected EffectiveStatement<String, EnumSpecification> createEmptyEffective(
+            final StmtContext<String, EnumSpecification, EffectiveStatement<String, EnumSpecification>> ctx,
+            final EnumSpecification declared) {
+        throw noEnum(ctx);
+    }
+
+    private static SourceException noEnum(final StmtContext<?, ?, ?> ctx) {
+        /*
+         *  https://tools.ietf.org/html/rfc7950#section-9.6.4
+         *
+         *     The "enum" statement, which is a substatement to the "type"
+         *     statement, MUST be present if the type is "enumeration".
+         */
+        return new SourceException("At least one enum statement has to be present", ctx.getStatementSourceReference());
+    }
+
 }
\ No newline at end of file