Consolidate DecimalTypeEffectiveStatementImpl 89/90889/5
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 2 Jul 2020 18:02:43 +0000 (20:02 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 2 Jul 2020 22:31:55 +0000 (00:31 +0200)
TypeEffectiveStatementImpl can easily support the case of an decimal
type, migrate it.

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

index 6a3a9aeb780b905216f7d5e269a3dc6c3865043b..72cb0e99e477c02239cd96e6c2e9ee4241bec7e2 100644 (file)
@@ -11,6 +11,7 @@ import static com.google.common.base.Preconditions.checkArgument;
 
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
+import java.math.BigDecimal;
 import java.util.Collection;
 import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNull;
@@ -23,7 +24,9 @@ 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.FractionDigitsEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.LengthEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.RangeEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
@@ -51,6 +54,7 @@ import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
 import org.opendaylight.yangtools.yang.model.util.type.BitsTypeBuilder;
 import org.opendaylight.yangtools.yang.model.util.type.InvalidLengthConstraintException;
 import org.opendaylight.yangtools.yang.model.util.type.LengthRestrictedTypeBuilder;
+import org.opendaylight.yangtools.yang.model.util.type.RangeRestrictedTypeBuilder;
 import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
 import org.opendaylight.yangtools.yang.parser.spi.TypeNamespace;
@@ -227,7 +231,7 @@ abstract class AbstractTypeStatementSupport
         } else if (baseType instanceof BooleanTypeDefinition) {
             return createBoolean(ctx, (BooleanTypeDefinition) baseType, declared, substatements);
         } else if (baseType instanceof DecimalTypeDefinition) {
-            return new DecimalTypeEffectiveStatementImpl(ctx, (DecimalTypeDefinition) baseType);
+            return createDecimal(ctx, (DecimalTypeDefinition) baseType, declared, substatements);
         } else if (baseType instanceof EmptyTypeDefinition) {
             return createEmpty(ctx, (EmptyTypeDefinition) baseType, declared, substatements);
         } else if (baseType instanceof EnumTypeDefinition) {
@@ -394,6 +398,27 @@ abstract class AbstractTypeStatementSupport
             typeEffectiveSchemaPath(ctx)));
     }
 
+    private static @NonNull TypeEffectiveStatement<TypeStatement> createDecimal(final StmtContext<?, ?, ?> ctx,
+            final DecimalTypeDefinition baseType, final TypeStatement declared,
+            final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        final RangeRestrictedTypeBuilder<DecimalTypeDefinition, BigDecimal> builder =
+                RestrictedTypes.newDecima64Builder(baseType, typeEffectiveSchemaPath(ctx));
+
+        for (EffectiveStatement<?, ?> stmt : substatements) {
+            if (stmt instanceof RangeEffectiveStatement) {
+                final RangeEffectiveStatement range = (RangeEffectiveStatement) stmt;
+                builder.setRangeConstraint(range, range.argument());
+            }
+            if (stmt instanceof FractionDigitsEffectiveStatement) {
+                final Integer digits = ((FractionDigitsEffectiveStatement)stmt).argument();
+                SourceException.throwIf(baseType.getFractionDigits() != digits, ctx.getStatementSourceReference(),
+                    "Cannot override fraction-digits from base type %s to %s", baseType, digits);
+            }
+        }
+
+        return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
+    }
+
     private static @NonNull TypeEffectiveStatement<TypeStatement> createEmpty(final StmtContext<?, ?, ?> ctx,
             final EmptyTypeDefinition baseType, final TypeStatement declared,
             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
diff --git a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/DecimalTypeEffectiveStatementImpl.java b/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/DecimalTypeEffectiveStatementImpl.java
deleted file mode 100644 (file)
index ce809bb..0000000
+++ /dev/null
@@ -1,59 +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.math.BigDecimal;
-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.FractionDigitsEffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.RangeEffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
-import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
-import org.opendaylight.yangtools.yang.model.util.type.RangeRestrictedTypeBuilder;
-import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
-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 DecimalTypeEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, TypeStatement>
-        implements TypeEffectiveStatement<TypeStatement> {
-    private final @NonNull DecimalTypeDefinition typeDefinition;
-
-    DecimalTypeEffectiveStatementImpl(
-            final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
-            final DecimalTypeDefinition baseType) {
-        super(ctx);
-
-        final RangeRestrictedTypeBuilder<DecimalTypeDefinition, BigDecimal> builder =
-                RestrictedTypes.newDecima64Builder(baseType, AbstractTypeStatementSupport.typeEffectiveSchemaPath(ctx));
-
-        for (EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
-            if (stmt instanceof RangeEffectiveStatement) {
-                final RangeEffectiveStatement range = (RangeEffectiveStatement) stmt;
-                builder.setRangeConstraint(range, range.argument());
-            }
-            if (stmt instanceof FractionDigitsEffectiveStatement) {
-                final Integer digits = ((FractionDigitsEffectiveStatement)stmt).argument();
-                SourceException.throwIf(baseType.getFractionDigits() != digits, ctx.getStatementSourceReference(),
-                    "Cannot override fraction-digits from base type %s to %s", baseType, digits);
-            }
-            if (stmt instanceof UnknownSchemaNode) {
-                builder.addUnknownSchemaNode((UnknownSchemaNode)stmt);
-            }
-        }
-
-        typeDefinition = builder.build();
-    }
-
-    @Override
-    public DecimalTypeDefinition getTypeDefinition() {
-        return typeDefinition;
-    }
-}