Split out isBitsType()
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / UnionTemplate.xtend
index 78c3b2eafd88fb6e78ed6cab71bed6b45e7d24dd..4779907d5792efe1578be54ec177c27bb2f85a7b 100644 (file)
@@ -7,10 +7,13 @@
  */
 package org.opendaylight.mdsal.binding.java.api.generator
 
-import static org.opendaylight.mdsal.binding.model.util.Types.BOOLEAN;
-import static org.opendaylight.mdsal.binding.model.util.Types.BYTE_ARRAY;
-import static org.opendaylight.mdsal.binding.model.util.Types.STRING;
-import static org.opendaylight.mdsal.binding.model.util.Types.getOuterClassName;
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.BINARY_TYPE;
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.BOOLEAN_TYPE;
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.EMPTY_TYPE;
+import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.STRING_TYPE;
+import static org.opendaylight.mdsal.binding.model.ri.Types.STRING;
+import static org.opendaylight.mdsal.binding.model.ri.Types.getOuterClassName;
+import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.BUILDER_SUFFIX
 
 import java.util.Base64;
 import org.gaul.modernizer_maven_annotations.SuppressModernizer
@@ -80,10 +83,10 @@ class UnionTemplate extends ClassTemplate {
 
     def typeBuilder() {
         val outerCls = getOuterClassName(type);
-        if(outerCls !== null) {
-            return outerCls + type.name + "Builder"
+        if (outerCls !== null) {
+            return outerCls + type.name + BUILDER_SUFFIX
         }
-        return type.name + "Builder"
+        return type.name + BUILDER_SUFFIX
     }
 
     private def unionConstructorsParentProperties() '''
@@ -106,38 +109,35 @@ class UnionTemplate extends ClassTemplate {
                 «val field = property.fieldName»
             if («field» != null) {
                 «val propRet = property.returnType»
-                «IF STRING.equals(propRet)»
+                «IF STRING_TYPE.equals(propRet)»
                     ««« type string
                 return «field»;
                 «ELSEIF "org.opendaylight.yangtools.yang.binding.InstanceIdentifier".equals(propRet.fullyQualifiedName)»
                     ««« type instance-identifier
                 return «field».toString();
-                «ELSEIF BYTE_ARRAY.equals(propRet)»
+                «ELSEIF BINARY_TYPE.equals(propRet)»
                     ««« type binary
                 return new «STRING.importedName»(«field»);
-                «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang") || propRet instanceof Enumeration
-                        || propRet.fullyQualifiedName.startsWith("java.math")»
-                    ««« type int*, decimal64 or enumeration*
+                «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang") || propRet instanceof Enumeration»
+                    ««« type int* or enumeration*
                 return «field».toString();
                 «ELSEIF "org.opendaylight.yangtools.yang.common".equals(propRet.packageName)
-                        && propRet.name.startsWith("Uint"
-                    ««« type uint*
+                        && (propRet.name.startsWith("Uint") || "Decimal64".equals(propRet.name)
+                    ««« type uint*, decimal64
                 return «field».toCanonicalString();
                 «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
                     ««« union type
                 return «field».stringValue();
-                «ELSEIF BOOLEAN.equals(propRet.typedefReturnType)»
+                «ELSEIF BOOLEAN_TYPE.equals(propRet.typedefReturnType)»
                     ««« generated boolean typedef
                 return «field».isValue().toString();
-                «ELSEIF BYTE_ARRAY.equals(propRet.typedefReturnType)»
+                «ELSEIF BINARY_TYPE.equals(propRet.typedefReturnType)»
                     ««« generated byte[] typedef
                 return «Base64.importedName».getEncoder().encodeToString(«field».getValue());
-                «ELSEIF Constants.EMPTY.equals(propRet) || Constants.EMPTY.equals(propRet.typedefReturnType)»
+                «ELSEIF EMPTY_TYPE.equals(propRet) || EMPTY_TYPE.equals(propRet.typedefReturnType)»
                     ««« generated empty typedef
                 return "";
-                «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
-                        && (propRet as GeneratedTransferObject).typedef  // Is it a typedef
-                        && (propRet as GeneratedTransferObject).baseType instanceof BitsTypeDefinition»
+                «ELSEIF propRet.isBitsType»
                     ««« generated bits typedef
                 return «JU_ARRAYS.importedName».toString(«field».getValue());
                 «ELSE»
@@ -146,11 +146,17 @@ class UnionTemplate extends ClassTemplate {
                 «ENDIF»
             }
             «ENDFOR»
-
-            throw new IllegalStateException("No value assinged");
+            throw new IllegalStateException("No value assigned");
         }
     '''
 
+    private static def isBitsType(Type type) {
+        if (type instanceof GeneratedTransferObject) {
+            return type.typedef && type.baseType instanceof BitsTypeDefinition
+        }
+        return false
+    }
+
     private static def Type typedefReturnType(Type type) {
         if (!(type instanceof GeneratedTransferObject)) {
             return null