BUG-4638: add more type checking methods 73/29873/2
authorRobert Varga <rovarga@cisco.com>
Wed, 18 Nov 2015 17:08:49 +0000 (18:08 +0100)
committerTony Tkacik <ttkacik@cisco.com>
Thu, 19 Nov 2015 18:41:41 +0000 (18:41 +0000)
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 <rovarga@cisco.com>
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/BaseTypes.java
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/DerivedTypes.java

index 92cd4c1cc913dc2e81c9f1b2c5a8362bfbc279cc..a59e637334839ba8a0b78a0ae7346c36914af7e7 100644 (file)
@@ -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;
     }
 }
index 883353605197ed1e812e4595849077f8b43fef91..0b743b9ddd0e6f8ec27c2ba69f519a68eac647ca 100644 (file)
@@ -83,7 +83,103 @@ public final class DerivedTypes {
         }
     }
 
-    public static DerivedTypeBuilder<BinaryTypeDefinition> 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<BinaryTypeDefinition> derivedBinaryBuilder(@Nonnull final BinaryTypeDefinition baseType, @Nonnull final SchemaPath path) {
         return new DerivedTypeBuilder<BinaryTypeDefinition>(baseType, path) {
             @Override
             public BinaryTypeDefinition build() {
@@ -93,7 +189,7 @@ public final class DerivedTypes {
         };
     }
 
-    public static DerivedTypeBuilder<BitsTypeDefinition> derivedBitsBuilder(final BitsTypeDefinition baseType, final SchemaPath path) {
+    private static DerivedTypeBuilder<BitsTypeDefinition> derivedBitsBuilder(final BitsTypeDefinition baseType, final SchemaPath path) {
         return new DerivedTypeBuilder<BitsTypeDefinition>(baseType, path) {
             @Override
             public BitsTypeDefinition build() {
@@ -103,7 +199,7 @@ public final class DerivedTypes {
         };
     }
 
-    public static DerivedTypeBuilder<BooleanTypeDefinition> derivedBooleanBuilder(@Nonnull final BooleanTypeDefinition baseType, @Nonnull final SchemaPath path) {
+    private static DerivedTypeBuilder<BooleanTypeDefinition> derivedBooleanBuilder(@Nonnull final BooleanTypeDefinition baseType, @Nonnull final SchemaPath path) {
         return new DerivedTypeBuilder<BooleanTypeDefinition>(baseType, path) {
             @Override
             public BooleanTypeDefinition build() {
@@ -113,7 +209,7 @@ public final class DerivedTypes {
         };
     }
 
-    public static DerivedTypeBuilder<DecimalTypeDefinition> derivedDecimalBuilder(final DecimalTypeDefinition baseType, final SchemaPath path) {
+    private static DerivedTypeBuilder<DecimalTypeDefinition> derivedDecimalBuilder(final DecimalTypeDefinition baseType, final SchemaPath path) {
         return new DerivedTypeBuilder<DecimalTypeDefinition>(baseType, path) {
             @Override
             public DecimalTypeDefinition build() {
@@ -123,7 +219,7 @@ public final class DerivedTypes {
         };
     }
 
-    public static DerivedTypeBuilder<EmptyTypeDefinition> derivedEmptyBuilder(final EmptyTypeDefinition baseType, final SchemaPath path) {
+    private static DerivedTypeBuilder<EmptyTypeDefinition> derivedEmptyBuilder(final EmptyTypeDefinition baseType, final SchemaPath path) {
         return new DerivedTypeBuilder<EmptyTypeDefinition>(baseType, path) {
             @Override
             public EmptyTypeDefinition build() {
@@ -133,7 +229,7 @@ public final class DerivedTypes {
         };
     }
 
-    public static DerivedTypeBuilder<EnumTypeDefinition> derivedEnumerationBuilder(final EnumTypeDefinition baseType, final SchemaPath path) {
+    private static DerivedTypeBuilder<EnumTypeDefinition> derivedEnumerationBuilder(final EnumTypeDefinition baseType, final SchemaPath path) {
         return new DerivedTypeBuilder<EnumTypeDefinition>(baseType, path) {
             @Override
             public EnumTypeDefinition build() {
@@ -143,7 +239,7 @@ public final class DerivedTypes {
         };
     }
 
-    public static DerivedTypeBuilder<IdentityrefTypeDefinition> derivedIdentityrefBuilder(final IdentityrefTypeDefinition baseType, final SchemaPath path) {
+    private static DerivedTypeBuilder<IdentityrefTypeDefinition> derivedIdentityrefBuilder(final IdentityrefTypeDefinition baseType, final SchemaPath path) {
         return new DerivedTypeBuilder<IdentityrefTypeDefinition>(baseType, path) {
             @Override
             public IdentityrefTypeDefinition build() {
@@ -153,7 +249,7 @@ public final class DerivedTypes {
         };
     }
 
-    public static DerivedTypeBuilder<InstanceIdentifierTypeDefinition> derivedInstanceIdentifierBuilder(final InstanceIdentifierTypeDefinition baseType, final SchemaPath path) {
+    private static DerivedTypeBuilder<InstanceIdentifierTypeDefinition> derivedInstanceIdentifierBuilder(final InstanceIdentifierTypeDefinition baseType, final SchemaPath path) {
         return new DerivedTypeBuilder<InstanceIdentifierTypeDefinition>(baseType, path) {
             @Override
             public InstanceIdentifierTypeDefinition build() {
@@ -163,7 +259,7 @@ public final class DerivedTypes {
         };
     }
 
-    public static DerivedTypeBuilder<IntegerTypeDefinition> derivedIntegerBuilder(final IntegerTypeDefinition baseType, final SchemaPath path) {
+    private static DerivedTypeBuilder<IntegerTypeDefinition> derivedIntegerBuilder(final IntegerTypeDefinition baseType, final SchemaPath path) {
         return new DerivedTypeBuilder<IntegerTypeDefinition>(baseType, path) {
             @Override
             public IntegerTypeDefinition build() {
@@ -173,7 +269,7 @@ public final class DerivedTypes {
         };
     }
 
-    public static DerivedTypeBuilder<LeafrefTypeDefinition> derivedLeafrefBuilder(final LeafrefTypeDefinition baseType, final SchemaPath path) {
+    private static DerivedTypeBuilder<LeafrefTypeDefinition> derivedLeafrefBuilder(final LeafrefTypeDefinition baseType, final SchemaPath path) {
         return new DerivedTypeBuilder<LeafrefTypeDefinition>(baseType, path) {
             @Override
             public LeafrefTypeDefinition build() {
@@ -183,7 +279,7 @@ public final class DerivedTypes {
         };
     }
 
-    public static DerivedTypeBuilder<StringTypeDefinition> derivedStringBuilder(final StringTypeDefinition baseType, final SchemaPath path) {
+    private static DerivedTypeBuilder<StringTypeDefinition> derivedStringBuilder(final StringTypeDefinition baseType, final SchemaPath path) {
         return new DerivedTypeBuilder<StringTypeDefinition>(baseType, path) {
             @Override
             public StringTypeDefinition build() {
@@ -193,7 +289,7 @@ public final class DerivedTypes {
         };
     }
 
-    public static DerivedTypeBuilder<UnionTypeDefinition> derivedUnionBuilder(final UnionTypeDefinition baseType, final SchemaPath path) {
+    private static DerivedTypeBuilder<UnionTypeDefinition> derivedUnionBuilder(final UnionTypeDefinition baseType, final SchemaPath path) {
         return new DerivedTypeBuilder<UnionTypeDefinition>(baseType, path) {
             @Override
             public DerivedUnionType build() {
@@ -203,7 +299,7 @@ public final class DerivedTypes {
         };
     }
 
-    public static DerivedTypeBuilder<UnsignedIntegerTypeDefinition> derivedUnsignedBuilder(final UnsignedIntegerTypeDefinition baseType, final SchemaPath path) {
+    private static DerivedTypeBuilder<UnsignedIntegerTypeDefinition> derivedUnsignedBuilder(final UnsignedIntegerTypeDefinition baseType, final SchemaPath path) {
         return new DerivedTypeBuilder<UnsignedIntegerTypeDefinition>(baseType, path) {
             @Override
             public UnsignedIntegerTypeDefinition build() {