From a8db14b89279281a8bce2e7a912913534076def7 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Thu, 2 Jul 2020 20:02:43 +0200 Subject: [PATCH] Consolidate DecimalTypeEffectiveStatementImpl TypeEffectiveStatementImpl can easily support the case of an decimal type, migrate it. JIRA: YANGTOOLS-1065 Change-Id: I352675843882b2fce414b10b58a38315c759278d Signed-off-by: Robert Varga --- .../type/AbstractTypeStatementSupport.java | 27 ++++++++- .../DecimalTypeEffectiveStatementImpl.java | 59 ------------------- 2 files changed, 26 insertions(+), 60 deletions(-) delete mode 100644 yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/DecimalTypeEffectiveStatementImpl.java diff --git a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/AbstractTypeStatementSupport.java b/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/AbstractTypeStatementSupport.java index 6a3a9aeb78..72cb0e99e4 100644 --- a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/AbstractTypeStatementSupport.java +++ b/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/AbstractTypeStatementSupport.java @@ -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 createDecimal(final StmtContext ctx, + final DecimalTypeDefinition baseType, final TypeStatement declared, + final ImmutableList> substatements) { + final RangeRestrictedTypeBuilder 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 createEmpty(final StmtContext ctx, final EmptyTypeDefinition baseType, final TypeStatement declared, final ImmutableList> 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 index ce809bb529..0000000000 --- a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/DecimalTypeEffectiveStatementImpl.java +++ /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 - implements TypeEffectiveStatement { - private final @NonNull DecimalTypeDefinition typeDefinition; - - DecimalTypeEffectiveStatementImpl( - final StmtContext> ctx, - final DecimalTypeDefinition baseType) { - super(ctx); - - final RangeRestrictedTypeBuilder 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; - } -} -- 2.36.6