Split out isBitsType()
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / UnionTemplate.xtend
index 93980a116e97dfb9e36ebedf31b3eea4ada8ace0..4779907d5792efe1578be54ec177c27bb2f85a7b 100644 (file)
@@ -7,12 +7,16 @@
  */
 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.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.Arrays
 import java.util.Base64;
+import org.gaul.modernizer_maven_annotations.SuppressModernizer
 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
 import org.opendaylight.mdsal.binding.model.api.Enumeration
 import org.opendaylight.mdsal.binding.model.api.Type
@@ -21,8 +25,8 @@ import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
 /**
  * Template for generating JAVA class.
  */
+@SuppressModernizer
 class UnionTemplate extends ClassTemplate {
-
     /**
      * Creates instance of this class with concrete <code>genType</code>.
      *
@@ -65,7 +69,7 @@ class UnionTemplate extends ClassTemplate {
             public «type.name»(«propertyAndTopParentProperties.asArgumentsDeclaration») {
                 super(«parentProperties.asArguments»);
                 «IF restrictions !== null»
-                    «checkArgument(property, restrictions, actualType, property.fieldName.toString
+                    «checkArgument(property, restrictions, actualType, property.fieldName)»
                 «ENDIF»
                 this.«property.fieldName» = «property.fieldName»;
                 «FOR other : finalProperties»
@@ -79,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() '''
@@ -100,49 +104,59 @@ class UnionTemplate extends ClassTemplate {
          *
          * @return String representation of this union's value.
          */
-        public «String.importedName» stringValue() {
+        public «STRING.importedName» stringValue() {
             «FOR property : finalProperties»
                 «val field = property.fieldName»
             if («field» != null) {
                 «val propRet = property.returnType»
-                «IF "java.lang.String".equals(propRet.fullyQualifiedName
+                «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[]".equals(propRet.name
+                «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*, uint, decimal64 or enumeration*
+                return new «STRING.importedName»(«field»);
+                «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") || "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 propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
-                        && (propRet as GeneratedTransferObject).typedef  // Is it a typedef
-                        && (propRet as GeneratedTransferObject).baseType instanceof BitsTypeDefinition»
+                «ELSEIF EMPTY_TYPE.equals(propRet) || EMPTY_TYPE.equals(propRet.typedefReturnType)»
+                    ««« generated empty typedef
+                return "";
+                «ELSEIF propRet.isBitsType»
                     ««« generated bits typedef
-                return «Arrays.importedName».toString(«field».getValue());
+                return «JU_ARRAYS.importedName».toString(«field».getValue());
                 «ELSE»
                     ««« generated type
                 return «field».getValue().toString();
                 «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
@@ -169,10 +183,11 @@ class UnionTemplate extends ClassTemplate {
                 super(source);
             «ENDIF»
             «FOR p : properties»
-                «IF p.returnType.importedName.contains("[]")»
-                this.«p.fieldName» = source.«p.fieldName» == null ? null : source.«p.fieldName».clone();
+                «val fieldName = p.fieldName»
+                «IF p.returnType.name.endsWith("[]")»
+                this.«fieldName» = source.«fieldName» == null ? null : source.«fieldName».clone();
                 «ELSE»
-                this.«p.fieldName» = source.«p.fieldName»;
+                this.«fieldName» = source.«fieldName»;
                 «ENDIF»
             «ENDFOR»
         }