Migrate BitsSpecificationSupport 95/90895/3
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 2 Jul 2020 20:43:18 +0000 (22:43 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 2 Jul 2020 22:31:55 +0000 (00:31 +0200)
BitsSpecificationSupport can use memory-efficient representation
through BaseStatementSupport. Furthermore we can reuse
TypeEffectiveStatementImpl instead of brewing a separate effective
implementation.

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

diff --git a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/BitsSpecificationEffectiveStatement.java b/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/BitsSpecificationEffectiveStatement.java
deleted file mode 100644 (file)
index 4e1c817..0000000
+++ /dev/null
@@ -1,74 +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.common.Uint32;
-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.BitEffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.BitsSpecification;
-import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
-import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
-import org.opendaylight.yangtools.yang.model.util.type.BitsTypeBuilder;
-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 BitsSpecificationEffectiveStatement extends DeclaredEffectiveStatementBase<String, BitsSpecification>
-        implements TypeEffectiveStatement<BitsSpecification> {
-
-    private final @NonNull BitsTypeDefinition typeDefinition;
-
-    BitsSpecificationEffectiveStatement(
-            final StmtContext<String, BitsSpecification, EffectiveStatement<String, BitsSpecification>> ctx) {
-        super(ctx);
-
-        final BitsTypeBuilder builder = BaseTypes.bitsTypeBuilder(ctx.getSchemaPath().get());
-        Uint32 highestPosition = null;
-        for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
-            if (stmt instanceof BitEffectiveStatement) {
-                final BitEffectiveStatement bitSubStmt = (BitEffectiveStatement) stmt;
-
-                final Optional<Uint32> declaredPosition = bitSubStmt.getDeclaredPosition();
-                final Uint32 effectivePos;
-                if (declaredPosition.isEmpty()) {
-                    if (highestPosition != null) {
-                        SourceException.throwIf(Uint32.MAX_VALUE.equals(highestPosition),
-                            ctx.getStatementSourceReference(), "Bit %s must have a position statement", bitSubStmt);
-                        effectivePos = Uint32.fromIntBits(highestPosition.intValue() + 1);
-                    } else {
-                        effectivePos = Uint32.ZERO;
-                    }
-                } else {
-                    effectivePos = declaredPosition.get();
-                }
-
-                final Bit bit = EffectiveTypeUtil.buildBit(bitSubStmt, effectivePos);
-                if (highestPosition == null || highestPosition.compareTo(bit.getPosition()) < 0) {
-                    highestPosition = bit.getPosition();
-                }
-
-                builder.addBit(bit);
-            }
-            if (stmt instanceof UnknownSchemaNode) {
-                builder.addUnknownSchemaNode((UnknownSchemaNode) stmt);
-            }
-        }
-
-        typeDefinition = builder.build();
-    }
-
-    @Override
-    public BitsTypeDefinition getTypeDefinition() {
-        return typeDefinition;
-    }
-}
index d4068f9254cdc3ade133ce3eeccfa91cd36f49f6..d22ef586298f612f54823b3f6e22f477df9995ef 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.BitsSpecification;
-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 BitsSpecificationImpl extends AbstractDeclaredStatement<String> implements BitsSpecification {
-    BitsSpecificationImpl(final StmtContext<String, BitsSpecification, ?> context) {
-        super(context);
+final class BitsSpecificationImpl extends WithSubstatements implements BitsSpecification {
+    BitsSpecificationImpl(final StmtContext<String, ?, ?> context,
+            final ImmutableList<? extends DeclaredStatement<?>> substatements) {
+        super(context, substatements);
     }
 }
index 6a215826894c872c18e3a03d2df9d16cf5eb9af0..8640170f866a51eb55bc90068172d30b3a7f6bb4 100644 (file)
@@ -7,15 +7,24 @@
  */
 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
 
+import com.google.common.collect.ImmutableList;
+import java.util.Optional;
+import org.opendaylight.yangtools.yang.common.Uint32;
 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.BitEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.BitsSpecification;
-import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
+import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
+import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
+import org.opendaylight.yangtools.yang.model.util.type.BitsTypeBuilder;
+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 BitsSpecificationSupport
-        extends AbstractStatementSupport<String, BitsSpecification, EffectiveStatement<String, BitsSpecification>> {
+        extends BaseStatementSupport<String, BitsSpecification, EffectiveStatement<String, BitsSpecification>> {
     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
         YangStmtMapping.TYPE)
         .addMultiple(YangStmtMapping.BIT)
@@ -31,18 +40,71 @@ final class BitsSpecificationSupport
     }
 
     @Override
-    public BitsSpecification createDeclared(final StmtContext<String, BitsSpecification, ?> ctx) {
-        return new BitsSpecificationImpl(ctx);
+    protected SubstatementValidator getSubstatementValidator() {
+        return SUBSTATEMENT_VALIDATOR;
     }
 
     @Override
-    public EffectiveStatement<String, BitsSpecification> createEffective(
-            final StmtContext<String, BitsSpecification, EffectiveStatement<String, BitsSpecification>> ctx) {
-        return new BitsSpecificationEffectiveStatement(ctx);
+    protected BitsSpecification createDeclared(final StmtContext<String, BitsSpecification, ?> ctx,
+            final ImmutableList<? extends DeclaredStatement<?>> substatements) {
+        return new BitsSpecificationImpl(ctx, substatements);
     }
 
     @Override
-    protected SubstatementValidator getSubstatementValidator() {
-        return SUBSTATEMENT_VALIDATOR;
+    protected BitsSpecification createEmptyDeclared(final StmtContext<String, BitsSpecification, ?> ctx) {
+        throw noBits(ctx);
+    }
+
+    @Override
+    protected EffectiveStatement<String, BitsSpecification> createEffective(
+            final StmtContext<String, BitsSpecification, EffectiveStatement<String, BitsSpecification>> ctx,
+            final BitsSpecification declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        final BitsTypeBuilder builder = BaseTypes.bitsTypeBuilder(ctx.getSchemaPath().get());
+        Uint32 highestPosition = null;
+        for (final EffectiveStatement<?, ?> stmt : substatements) {
+            if (stmt instanceof BitEffectiveStatement) {
+                final BitEffectiveStatement bitSubStmt = (BitEffectiveStatement) stmt;
+
+                final Optional<Uint32> declaredPosition = bitSubStmt.getDeclaredPosition();
+                final Uint32 effectivePos;
+                if (declaredPosition.isEmpty()) {
+                    if (highestPosition != null) {
+                        SourceException.throwIf(Uint32.MAX_VALUE.equals(highestPosition),
+                            ctx.getStatementSourceReference(), "Bit %s must have a position statement", bitSubStmt);
+                        effectivePos = Uint32.fromIntBits(highestPosition.intValue() + 1);
+                    } else {
+                        effectivePos = Uint32.ZERO;
+                    }
+                } else {
+                    effectivePos = declaredPosition.get();
+                }
+
+                final Bit bit = EffectiveTypeUtil.buildBit(bitSubStmt, effectivePos);
+                if (highestPosition == null || highestPosition.compareTo(bit.getPosition()) < 0) {
+                    highestPosition = bit.getPosition();
+                }
+
+                builder.addBit(bit);
+            }
+        }
+
+        return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
+    }
+
+    @Override
+    protected EffectiveStatement<String, BitsSpecification> createEmptyEffective(
+            final StmtContext<String, BitsSpecification, EffectiveStatement<String, BitsSpecification>> ctx,
+            final BitsSpecification declared) {
+        throw noBits(ctx);
+    }
+
+    private static SourceException noBits(final StmtContext<?, ?, ?> ctx) {
+        /*
+         *  https://tools.ietf.org/html/rfc7950#section-9.7.4:
+         *
+         *     The "bit" statement, which is a substatement to the "type" statement,
+         *     MUST be present if the type is "bits".
+         */
+        return new SourceException("At least one bit statement has to be present", ctx.getStatementSourceReference());
     }
-}
\ No newline at end of file
+}
index 64c14a8ee9a9609d52f6be7d2972c5ace8e473f8..a30eb21127421cfbd9b67d8809c4dab1b55ff6e1 100644 (file)
@@ -17,12 +17,12 @@ import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
 import org.opendaylight.yangtools.yang.model.util.type.TypeBuilder;
 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractDeclaredEffectiveStatement.DefaultArgument.WithSubstatements;
 
-final class TypeEffectiveStatementImpl<T extends TypeDefinition<T>> extends WithSubstatements<String, TypeStatement>
-        implements TypeEffectiveStatement<TypeStatement> {
+final class TypeEffectiveStatementImpl<T extends TypeDefinition<T>, D extends TypeStatement>
+        extends WithSubstatements<String, D> implements TypeEffectiveStatement<D> {
     private final @NonNull T typeDefinition;
 
-    TypeEffectiveStatementImpl(final TypeStatement declared,
-            final ImmutableList<? extends EffectiveStatement<?, ?>> substatements, final TypeBuilder<T> builder) {
+    TypeEffectiveStatementImpl(final D declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements,
+            final TypeBuilder<T> builder) {
         super(declared, substatements);
 
         for (EffectiveStatement<?, ?> stmt : substatements) {