BUG-4638: add utility check methods for base types 92/29792/3
authorRobert Varga <robert.varga@pantheon.sk>
Tue, 17 Nov 2015 00:08:39 +0000 (01:08 +0100)
committerGerrit Code Review <gerrit@opendaylight.org>
Wed, 18 Nov 2015 13:09:13 +0000 (13:09 +0000)
Some users, like the java binding, need to distriguish the various
integer base types in order to produce the correct type specification.
Add utility methods which check the concrete integer base type
(uint{8,16,32,64} and int{8,16,32,64}.

Change-Id: Idf4c71be6b135d68715fe095e1cc1f5033c4824c
Signed-off-by: Robert Varga <robert.varga@pantheon.sk>
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/BaseTypes.java

index 1da6f3ab9c81fe20d8b1d799d34a3ade7e6befc2..92cd4c1cc913dc2e81c9f1b2c5a8362bfbc279cc 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.yangtools.yang.model.util.type;
 
 import com.google.common.annotations.Beta;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 
 import com.google.common.annotations.Beta;
 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;
 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
@@ -84,18 +85,34 @@ public final class BaseTypes {
         return BaseInt8Type.INSTANCE;
     }
 
         return BaseInt8Type.INSTANCE;
     }
 
+    public static boolean isInt8(final TypeDefinition<?> type) {
+        return BaseInt8Type.INSTANCE.getPath().equals(findBaseTypePath(type));
+    }
+
     public static IntegerTypeDefinition int16Type() {
         return BaseInt16Type.INSTANCE;
     }
 
     public static IntegerTypeDefinition int16Type() {
         return BaseInt16Type.INSTANCE;
     }
 
+    public static boolean isInt16(final TypeDefinition<?> type) {
+        return BaseInt16Type.INSTANCE.getPath().equals(findBaseTypePath(type));
+    }
+
     public static IntegerTypeDefinition int32Type() {
         return BaseInt32Type.INSTANCE;
     }
 
     public static IntegerTypeDefinition int32Type() {
         return BaseInt32Type.INSTANCE;
     }
 
+    public static boolean isInt32(final TypeDefinition<?> type) {
+        return BaseInt32Type.INSTANCE.getPath().equals(findBaseTypePath(type));
+    }
+
     public static IntegerTypeDefinition int64Type() {
         return BaseInt64Type.INSTANCE;
     }
 
     public static IntegerTypeDefinition int64Type() {
         return BaseInt64Type.INSTANCE;
     }
 
+    public static boolean isInt64(final TypeDefinition<?> type) {
+        return BaseInt64Type.INSTANCE.getPath().equals(findBaseTypePath(type));
+    }
+
     public static LeafrefTypeBuilder leafrefTypeBuilder(final SchemaPath path) {
         return new LeafrefTypeBuilder(path);
     }
     public static LeafrefTypeBuilder leafrefTypeBuilder(final SchemaPath path) {
         return new LeafrefTypeBuilder(path);
     }
@@ -112,15 +129,39 @@ public final class BaseTypes {
         return BaseUint8Type.INSTANCE;
     }
 
         return BaseUint8Type.INSTANCE;
     }
 
+    public static boolean isUint8(final TypeDefinition<?> type) {
+        return BaseUint8Type.INSTANCE.getPath().equals(findBaseTypePath(type));
+    }
+
     public static UnsignedIntegerTypeDefinition uint16Type() {
         return BaseUint16Type.INSTANCE;
     }
 
     public static UnsignedIntegerTypeDefinition uint16Type() {
         return BaseUint16Type.INSTANCE;
     }
 
+    public static boolean isUint16(final TypeDefinition<?> type) {
+        return BaseUint16Type.INSTANCE.getPath().equals(findBaseTypePath(type));
+    }
+
     public static UnsignedIntegerTypeDefinition uint32Type() {
         return BaseUint32Type.INSTANCE;
     }
 
     public static UnsignedIntegerTypeDefinition uint32Type() {
         return BaseUint32Type.INSTANCE;
     }
 
+    public static boolean isUint32(final TypeDefinition<?> type) {
+        return BaseUint32Type.INSTANCE.getPath().equals(findBaseTypePath(type));
+    }
+
     public static UnsignedIntegerTypeDefinition uint64Type() {
         return BaseUint64Type.INSTANCE;
     }
     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) {
+        TypeDefinition<?> ret = type;
+        while (ret.getBaseType() != null) {
+            ret = ret.getBaseType();
+        }
+        return ret.getPath();
+    }
 }
 }