From 68e01edf2b36097d5b0968200394c0c6c12713dc Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Thu, 2 Jul 2020 20:25:52 +0200 Subject: [PATCH] Consolidate IntegralTypeEffectiveStatementImpl TypeEffectiveStatementImpl can easily support the case of each integral type, migrate it. JIRA: YANGTOOLS-1065 Change-Id: I21f9cbdd97966f7a1d36ae536ac01fb93f3596ca Signed-off-by: Robert Varga --- .../type/AbstractTypeStatementSupport.java | 39 ++++++++++--- .../IntegralTypeEffectiveStatementImpl.java | 56 ------------------- 2 files changed, 30 insertions(+), 65 deletions(-) delete mode 100644 yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/IntegralTypeEffectiveStatementImpl.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 8cfe5f34da..aab13f0907 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 @@ -51,6 +51,7 @@ import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint; +import org.opendaylight.yangtools.yang.model.api.type.RangeRestrictedTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition; @@ -61,6 +62,7 @@ import org.opendaylight.yangtools.yang.model.util.type.BitsTypeBuilder; import org.opendaylight.yangtools.yang.model.util.type.EnumerationTypeBuilder; import org.opendaylight.yangtools.yang.model.util.type.InstanceIdentifierTypeBuilder; import org.opendaylight.yangtools.yang.model.util.type.InvalidLengthConstraintException; +import org.opendaylight.yangtools.yang.model.util.type.InvalidRangeConstraintException; 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.RequireInstanceRestrictedTypeBuilder; @@ -255,32 +257,32 @@ abstract class AbstractTypeStatementSupport } else if (baseType instanceof InstanceIdentifierTypeDefinition) { return createInstanceIdentifier(ctx, (InstanceIdentifierTypeDefinition) baseType, declared, substatements); } else if (baseType instanceof Int8TypeDefinition) { - return new IntegralTypeEffectiveStatementImpl<>(ctx, - RestrictedTypes.newInt8Builder((Int8TypeDefinition) baseType, typeEffectiveSchemaPath(ctx))); + return createIntegral(ctx, declared, substatements, + RestrictedTypes.newInt8Builder((Int8TypeDefinition) baseType, typeEffectiveSchemaPath(ctx))); } else if (baseType instanceof Int16TypeDefinition) { - return new IntegralTypeEffectiveStatementImpl<>(ctx, + return createIntegral(ctx, declared, substatements, RestrictedTypes.newInt16Builder((Int16TypeDefinition) baseType, typeEffectiveSchemaPath(ctx))); } else if (baseType instanceof Int32TypeDefinition) { - return new IntegralTypeEffectiveStatementImpl<>(ctx, + return createIntegral(ctx, declared, substatements, RestrictedTypes.newInt32Builder((Int32TypeDefinition) baseType, typeEffectiveSchemaPath(ctx))); } else if (baseType instanceof Int64TypeDefinition) { - return new IntegralTypeEffectiveStatementImpl<>(ctx, + return createIntegral(ctx, declared, substatements, RestrictedTypes.newInt64Builder((Int64TypeDefinition) baseType, typeEffectiveSchemaPath(ctx))); } else if (baseType instanceof LeafrefTypeDefinition) { return createLeafref(ctx, (LeafrefTypeDefinition) baseType, declared, substatements); } else if (baseType instanceof StringTypeDefinition) { return createString(ctx, (StringTypeDefinition) baseType, declared, substatements); } else if (baseType instanceof Uint8TypeDefinition) { - return new IntegralTypeEffectiveStatementImpl<>(ctx, + return createIntegral(ctx, declared, substatements, RestrictedTypes.newUint8Builder((Uint8TypeDefinition) baseType, typeEffectiveSchemaPath(ctx))); } else if (baseType instanceof Uint16TypeDefinition) { - return new IntegralTypeEffectiveStatementImpl<>(ctx, + return createIntegral(ctx, declared, substatements, RestrictedTypes.newUint16Builder((Uint16TypeDefinition) baseType, typeEffectiveSchemaPath(ctx))); } else if (baseType instanceof Uint32TypeDefinition) { - return new IntegralTypeEffectiveStatementImpl<>(ctx, + return createIntegral(ctx, declared, substatements, RestrictedTypes.newUint32Builder((Uint32TypeDefinition) baseType, typeEffectiveSchemaPath(ctx))); } else if (baseType instanceof Uint64TypeDefinition) { - return new IntegralTypeEffectiveStatementImpl<>(ctx, + return createIntegral(ctx, declared, substatements, RestrictedTypes.newUint64Builder((Uint64TypeDefinition) baseType, typeEffectiveSchemaPath(ctx))); } else if (baseType instanceof UnionTypeDefinition) { return createUnion(ctx, (UnionTypeDefinition) baseType, declared, substatements); @@ -490,6 +492,25 @@ abstract class AbstractTypeStatementSupport return new TypeEffectiveStatementImpl<>(declared, substatements, builder); } + private static , N extends Number & Comparable> + @NonNull TypeEffectiveStatement createIntegral(final StmtContext ctx, + final TypeStatement declared, final ImmutableList> substatements, + final RangeRestrictedTypeBuilder builder) { + for (EffectiveStatement stmt : substatements) { + if (stmt instanceof RangeEffectiveStatement) { + final RangeEffectiveStatement rangeStmt = (RangeEffectiveStatement)stmt; + builder.setRangeConstraint(rangeStmt, rangeStmt.argument()); + } + } + + try { + return new TypeEffectiveStatementImpl<>(declared, substatements, builder); + } catch (InvalidRangeConstraintException e) { + throw new SourceException(ctx.getStatementSourceReference(), e, "Invalid range constraint: %s", + e.getOffendingRanges()); + } + } + private static @NonNull TypeEffectiveStatement createLeafref(final StmtContext ctx, final LeafrefTypeDefinition 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/IntegralTypeEffectiveStatementImpl.java b/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/IntegralTypeEffectiveStatementImpl.java deleted file mode 100644 index 6f8eca6f88..0000000000 --- a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/type/IntegralTypeEffectiveStatementImpl.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (c) 2017 Pantheon Technologies, s.r.o. 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.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.RangeRestrictedTypeDefinition; -import org.opendaylight.yangtools.yang.model.util.type.InvalidRangeConstraintException; -import org.opendaylight.yangtools.yang.model.util.type.RangeRestrictedTypeBuilder; -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 IntegralTypeEffectiveStatementImpl, - N extends Number & Comparable> extends DeclaredEffectiveStatementBase - implements TypeEffectiveStatement { - - private final @NonNull T typeDefinition; - - IntegralTypeEffectiveStatementImpl( - final StmtContext> ctx, - final RangeRestrictedTypeBuilder builder) { - super(ctx); - - for (EffectiveStatement stmt : effectiveSubstatements()) { - if (stmt instanceof RangeEffectiveStatement) { - final RangeEffectiveStatement rangeStmt = (RangeEffectiveStatement)stmt; - builder.setRangeConstraint(rangeStmt, rangeStmt.argument()); - } - if (stmt instanceof UnknownSchemaNode) { - builder.addUnknownSchemaNode((UnknownSchemaNode)stmt); - } - } - - try { - typeDefinition = builder.build(); - } catch (InvalidRangeConstraintException e) { - throw new SourceException(ctx.getStatementSourceReference(), e, "Invalid range constraint: %s", - e.getOffendingRanges()); - } - } - - @Override - public T getTypeDefinition() { - return typeDefinition; - } -} -- 2.36.6