Use StringBuilder fluently
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / UnionTemplate.xtend
index 4f0ab4b1577cb8bd261eed26e0568e75400407a4..4779907d5792efe1578be54ec177c27bb2f85a7b 100644 (file)
@@ -7,12 +7,13 @@
  */
 package org.opendaylight.mdsal.binding.java.api.generator
 
-import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.BINARY_TYPE;
-import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.BOOLEAN_TYPE;
-import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.EMPTY_TYPE;
-import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.STRING_TYPE;
-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
@@ -82,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() '''
@@ -117,13 +118,12 @@ class UnionTemplate extends ClassTemplate {
                 «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
@@ -137,9 +137,7 @@ class UnionTemplate extends ClassTemplate {
                 «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»
@@ -148,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