From: Robert Varga Date: Wed, 18 Nov 2015 17:08:49 +0000 (+0100) Subject: BUG-4638: add more type checking methods X-Git-Tag: release/beryllium~125 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=yangtools.git;a=commitdiff_plain;h=f305ed0b98fc14c78cda17178811411bbdd86ff0 BUG-4638: add more type checking methods Update the both BaseTypes and DerivedTypes should have the uint checkers, with the difference being that BaseTypes do not perform a recursive lookup. That means that BaseTypes#isInt8() will return true only for 'type int8;', not derived types. Change-Id: I9c391e1f63c661e65c2e5d3a241465f1c326878d Signed-off-by: Robert Varga --- diff --git a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/BaseTypes.java b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/BaseTypes.java index 92cd4c1cc9..a59e637334 100644 --- a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/BaseTypes.java +++ b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/BaseTypes.java @@ -8,6 +8,7 @@ package org.opendaylight.yangtools.yang.model.util.type; import com.google.common.annotations.Beta; +import javax.annotation.Nonnull; import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition; @@ -86,7 +87,7 @@ public final class BaseTypes { } public static boolean isInt8(final TypeDefinition type) { - return BaseInt8Type.INSTANCE.getPath().equals(findBaseTypePath(type)); + return BaseInt8Type.INSTANCE.getPath().equals(type.getPath()); } public static IntegerTypeDefinition int16Type() { @@ -94,7 +95,7 @@ public final class BaseTypes { } public static boolean isInt16(final TypeDefinition type) { - return BaseInt16Type.INSTANCE.getPath().equals(findBaseTypePath(type)); + return BaseInt16Type.INSTANCE.getPath().equals(type.getPath()); } public static IntegerTypeDefinition int32Type() { @@ -102,7 +103,7 @@ public final class BaseTypes { } public static boolean isInt32(final TypeDefinition type) { - return BaseInt32Type.INSTANCE.getPath().equals(findBaseTypePath(type)); + return BaseInt32Type.INSTANCE.getPath().equals(type.getPath()); } public static IntegerTypeDefinition int64Type() { @@ -110,7 +111,7 @@ public final class BaseTypes { } public static boolean isInt64(final TypeDefinition type) { - return BaseInt64Type.INSTANCE.getPath().equals(findBaseTypePath(type)); + return BaseInt64Type.INSTANCE.getPath().equals(type.getPath()); } public static LeafrefTypeBuilder leafrefTypeBuilder(final SchemaPath path) { @@ -129,39 +130,79 @@ public final class BaseTypes { return BaseUint8Type.INSTANCE; } - public static boolean isUint8(final TypeDefinition type) { - return BaseUint8Type.INSTANCE.getPath().equals(findBaseTypePath(type)); + /** + * Check if a particular type is the base type for uint8. Unlike {@link DerivedTypes#isUint8(TypeDefinition)}, + * this method does not perform recursive base type lookup. + * + * @param type The type to check + * @return If the type corresponds to the base uint8 type. + * @throws NullPointerException if type is null + */ + public static boolean isUint8(@Nonnull final TypeDefinition type) { + return BaseUint8Type.INSTANCE.getPath().equals(type.getPath()); } public static UnsignedIntegerTypeDefinition uint16Type() { return BaseUint16Type.INSTANCE; } - public static boolean isUint16(final TypeDefinition type) { - return BaseUint16Type.INSTANCE.getPath().equals(findBaseTypePath(type)); + /** + * Check if a particular type is the base type for uint16. Unlike {@link DerivedTypes#isUint16(TypeDefinition)}, + * this method does not perform recursive base type lookup. + * + * @param type The type to check + * @return If the type corresponds to the base uint16 type. + * @throws NullPointerException if type is null + */ + public static boolean isUint16(@Nonnull final TypeDefinition type) { + return BaseUint16Type.INSTANCE.getPath().equals(type.getPath()); } public static UnsignedIntegerTypeDefinition uint32Type() { return BaseUint32Type.INSTANCE; } - public static boolean isUint32(final TypeDefinition type) { - return BaseUint32Type.INSTANCE.getPath().equals(findBaseTypePath(type)); + /** + * Check if a particular type is the base type for uint32. Unlike {@link DerivedTypes#isUint32(TypeDefinition)}, + * this method does not perform recursive base type lookup. + * + * @param type The type to check + * @return If the type corresponds to the base uint32 type. + * @throws NullPointerException if type is null + */ + public static boolean isUint32(@Nonnull final TypeDefinition type) { + return BaseUint32Type.INSTANCE.getPath().equals(type.getPath()); } public static UnsignedIntegerTypeDefinition uint64Type() { return BaseUint64Type.INSTANCE; } - public static boolean isUint64(final TypeDefinition type) { - return BaseUint64Type.INSTANCE.getPath().equals(findBaseTypePath(type)); - } - - private static SchemaPath findBaseTypePath(final TypeDefinition type) { + /** + * Check if a particular type is the base type for uint64. Unlike {@link DerivedTypes#isUint64(TypeDefinition)}, + * this method does not perform recursive base type lookup. + * + * @param type The type to check + * @return If the type corresponds to the base uint64 type. + * @throws NullPointerException if type is null + */ + public static boolean isUint64(@Nonnull final TypeDefinition type) { + return BaseUint64Type.INSTANCE.getPath().equals(type.getPath()); + } + + /** + * Return the base type of a particular type. This method performs recursive lookup through the type's base type + * until it finds the last element and returns it. If the argument is already the base type, it is returned as is. + * + * @param type Type for which to find the base type + * @return Base type of specified type + * @throws NullPointerException if type is null + */ + public static TypeDefinition baseTypeOf(@Nonnull final TypeDefinition type) { TypeDefinition ret = type; while (ret.getBaseType() != null) { ret = ret.getBaseType(); } - return ret.getPath(); + return ret; } } diff --git a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/DerivedTypes.java b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/DerivedTypes.java index 8833536051..0b743b9ddd 100644 --- a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/DerivedTypes.java +++ b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/DerivedTypes.java @@ -83,7 +83,103 @@ public final class DerivedTypes { } } - public static DerivedTypeBuilder derivedBinaryBuilder(@Nonnull final BinaryTypeDefinition baseType, @Nonnull final SchemaPath path) { + /** + * Check if a particular type is corresponds to int8. Unlike {@link BaseTypes#isInt8(TypeDefinition)}, this + * method performs recursive lookup to find the base type. + * + * @param type The type to check + * @return If the type belongs to the int8 type family. + * @throws NullPointerException if type is null + */ + public static boolean isInt8(@Nonnull final TypeDefinition type) { + return BaseTypes.isInt8(BaseTypes.baseTypeOf(type)); + } + + /** + * Check if a particular type is corresponds to int16. Unlike {@link BaseTypes#isInt16(TypeDefinition)}, this + * method performs recursive lookup to find the base type. + * + * @param type The type to check + * @return If the type belongs to the int16 type family. + * @throws NullPointerException if type is null + */ + public static boolean isInt16(@Nonnull final TypeDefinition type) { + return BaseTypes.isInt16(BaseTypes.baseTypeOf(type)); + } + + /** + * Check if a particular type is corresponds to int32. Unlike {@link BaseTypes#isInt32(TypeDefinition)}, this + * method performs recursive lookup to find the base type. + * + * @param type The type to check + * @return If the type belongs to the int32 type family. + * @throws NullPointerException if type is null + */ + public static boolean isInt32(@Nonnull final TypeDefinition type) { + return BaseTypes.isInt32(BaseTypes.baseTypeOf(type)); + } + + /** + * Check if a particular type is corresponds to int64. Unlike {@link BaseTypes#isInt64(TypeDefinition)}, this + * method performs recursive lookup to find the base type. + * + * @param type The type to check + * @return If the type belongs to the int64 type family. + * @throws NullPointerException if type is null + */ + public static boolean isInt64(@Nonnull final TypeDefinition type) { + return BaseTypes.isInt64(BaseTypes.baseTypeOf(type)); + } + + /** + * Check if a particular type is corresponds to uint8. Unlike {@link BaseTypes#isUint8(TypeDefinition)}, this + * method performs recursive lookup to find the base type. + * + * @param type The type to check + * @return If the type belongs to the uint8 type family. + * @throws NullPointerException if type is null + */ + public static boolean isUint8(@Nonnull final TypeDefinition type) { + return BaseTypes.isUint8(BaseTypes.baseTypeOf(type)); + } + + /** + * Check if a particular type is corresponds to uint16. Unlike {@link BaseTypes#isUint16(TypeDefinition)}, this + * method performs recursive lookup to find the base type. + * + * @param type The type to check + * @return If the type belongs to the uint16 type family. + * @throws NullPointerException if type is null + */ + public static boolean isUint16(@Nonnull final TypeDefinition type) { + return BaseTypes.isUint16(BaseTypes.baseTypeOf(type)); + } + + /** + * Check if a particular type is corresponds to uint32. Unlike {@link BaseTypes#isUint32(TypeDefinition)}, this + * method performs recursive lookup to find the base type. + * + * @param type The type to check + * @return If the type belongs to the uint32 type family. + * @throws NullPointerException if type is null + */ + public static boolean isUint32(@Nonnull final TypeDefinition type) { + return BaseTypes.isUint32(BaseTypes.baseTypeOf(type)); + } + + /** + * Check if a particular type is corresponds to uint64. Unlike {@link BaseTypes#isUint64(TypeDefinition)}, this + * method performs recursive lookup to find the base type. + * + * @param type The type to check + * @return If the type belongs to the uint64 type family. + * @throws NullPointerException if type is null + */ + public static boolean isUint64(@Nonnull final TypeDefinition type) { + return BaseTypes.isUint64(BaseTypes.baseTypeOf(type)); + } + + private static DerivedTypeBuilder derivedBinaryBuilder(@Nonnull final BinaryTypeDefinition baseType, @Nonnull final SchemaPath path) { return new DerivedTypeBuilder(baseType, path) { @Override public BinaryTypeDefinition build() { @@ -93,7 +189,7 @@ public final class DerivedTypes { }; } - public static DerivedTypeBuilder derivedBitsBuilder(final BitsTypeDefinition baseType, final SchemaPath path) { + private static DerivedTypeBuilder derivedBitsBuilder(final BitsTypeDefinition baseType, final SchemaPath path) { return new DerivedTypeBuilder(baseType, path) { @Override public BitsTypeDefinition build() { @@ -103,7 +199,7 @@ public final class DerivedTypes { }; } - public static DerivedTypeBuilder derivedBooleanBuilder(@Nonnull final BooleanTypeDefinition baseType, @Nonnull final SchemaPath path) { + private static DerivedTypeBuilder derivedBooleanBuilder(@Nonnull final BooleanTypeDefinition baseType, @Nonnull final SchemaPath path) { return new DerivedTypeBuilder(baseType, path) { @Override public BooleanTypeDefinition build() { @@ -113,7 +209,7 @@ public final class DerivedTypes { }; } - public static DerivedTypeBuilder derivedDecimalBuilder(final DecimalTypeDefinition baseType, final SchemaPath path) { + private static DerivedTypeBuilder derivedDecimalBuilder(final DecimalTypeDefinition baseType, final SchemaPath path) { return new DerivedTypeBuilder(baseType, path) { @Override public DecimalTypeDefinition build() { @@ -123,7 +219,7 @@ public final class DerivedTypes { }; } - public static DerivedTypeBuilder derivedEmptyBuilder(final EmptyTypeDefinition baseType, final SchemaPath path) { + private static DerivedTypeBuilder derivedEmptyBuilder(final EmptyTypeDefinition baseType, final SchemaPath path) { return new DerivedTypeBuilder(baseType, path) { @Override public EmptyTypeDefinition build() { @@ -133,7 +229,7 @@ public final class DerivedTypes { }; } - public static DerivedTypeBuilder derivedEnumerationBuilder(final EnumTypeDefinition baseType, final SchemaPath path) { + private static DerivedTypeBuilder derivedEnumerationBuilder(final EnumTypeDefinition baseType, final SchemaPath path) { return new DerivedTypeBuilder(baseType, path) { @Override public EnumTypeDefinition build() { @@ -143,7 +239,7 @@ public final class DerivedTypes { }; } - public static DerivedTypeBuilder derivedIdentityrefBuilder(final IdentityrefTypeDefinition baseType, final SchemaPath path) { + private static DerivedTypeBuilder derivedIdentityrefBuilder(final IdentityrefTypeDefinition baseType, final SchemaPath path) { return new DerivedTypeBuilder(baseType, path) { @Override public IdentityrefTypeDefinition build() { @@ -153,7 +249,7 @@ public final class DerivedTypes { }; } - public static DerivedTypeBuilder derivedInstanceIdentifierBuilder(final InstanceIdentifierTypeDefinition baseType, final SchemaPath path) { + private static DerivedTypeBuilder derivedInstanceIdentifierBuilder(final InstanceIdentifierTypeDefinition baseType, final SchemaPath path) { return new DerivedTypeBuilder(baseType, path) { @Override public InstanceIdentifierTypeDefinition build() { @@ -163,7 +259,7 @@ public final class DerivedTypes { }; } - public static DerivedTypeBuilder derivedIntegerBuilder(final IntegerTypeDefinition baseType, final SchemaPath path) { + private static DerivedTypeBuilder derivedIntegerBuilder(final IntegerTypeDefinition baseType, final SchemaPath path) { return new DerivedTypeBuilder(baseType, path) { @Override public IntegerTypeDefinition build() { @@ -173,7 +269,7 @@ public final class DerivedTypes { }; } - public static DerivedTypeBuilder derivedLeafrefBuilder(final LeafrefTypeDefinition baseType, final SchemaPath path) { + private static DerivedTypeBuilder derivedLeafrefBuilder(final LeafrefTypeDefinition baseType, final SchemaPath path) { return new DerivedTypeBuilder(baseType, path) { @Override public LeafrefTypeDefinition build() { @@ -183,7 +279,7 @@ public final class DerivedTypes { }; } - public static DerivedTypeBuilder derivedStringBuilder(final StringTypeDefinition baseType, final SchemaPath path) { + private static DerivedTypeBuilder derivedStringBuilder(final StringTypeDefinition baseType, final SchemaPath path) { return new DerivedTypeBuilder(baseType, path) { @Override public StringTypeDefinition build() { @@ -193,7 +289,7 @@ public final class DerivedTypes { }; } - public static DerivedTypeBuilder derivedUnionBuilder(final UnionTypeDefinition baseType, final SchemaPath path) { + private static DerivedTypeBuilder derivedUnionBuilder(final UnionTypeDefinition baseType, final SchemaPath path) { return new DerivedTypeBuilder(baseType, path) { @Override public DerivedUnionType build() { @@ -203,7 +299,7 @@ public final class DerivedTypes { }; } - public static DerivedTypeBuilder derivedUnsignedBuilder(final UnsignedIntegerTypeDefinition baseType, final SchemaPath path) { + private static DerivedTypeBuilder derivedUnsignedBuilder(final UnsignedIntegerTypeDefinition baseType, final SchemaPath path) { return new DerivedTypeBuilder(baseType, path) { @Override public UnsignedIntegerTypeDefinition build() {