Offload null value checking to CodeHelpers
[yangtools.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / ClassTemplate.xtend
index 3e23a387a4fb198a194f2b81a37944b1bccff3f8..d7acf20b7d5bb96e57fd40ee38e58a97554ce3af 100644 (file)
@@ -8,18 +8,20 @@
 package org.opendaylight.mdsal.binding.java.api.generator
 
 import static java.util.Objects.requireNonNull
+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 extension org.apache.commons.text.StringEscapeUtils.escapeJava
 
+import com.google.common.base.Preconditions
 import com.google.common.collect.ImmutableList
 import com.google.common.collect.Lists
-import com.google.common.io.BaseEncoding
 import java.beans.ConstructorProperties
 import java.util.ArrayList
-import java.util.Arrays
+import java.util.Base64;
 import java.util.Collections
 import java.util.List
 import java.util.Map
-import java.util.Objects
 import java.util.regex.Pattern
 import org.opendaylight.mdsal.binding.model.api.ConcreteType
 import org.opendaylight.mdsal.binding.model.api.Constant
@@ -30,6 +32,7 @@ import org.opendaylight.mdsal.binding.model.api.Restrictions
 import org.opendaylight.mdsal.binding.model.api.Type
 import org.opendaylight.mdsal.binding.model.util.TypeConstants
 import org.opendaylight.yangtools.yang.binding.CodeHelpers
+import org.opendaylight.yangtools.yang.common.Empty
 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
 
 /**
@@ -40,7 +43,7 @@ class ClassTemplate extends BaseTemplate {
     protected val List<GeneratedProperty> properties
     protected val List<GeneratedProperty> finalProperties
     protected val List<GeneratedProperty> parentProperties
-    protected val Iterable<GeneratedProperty> allProperties;
+    protected val Iterable<GeneratedProperty> allProperties
     protected val Restrictions restrictions
 
     /**
@@ -53,9 +56,9 @@ class ClassTemplate extends BaseTemplate {
      */
     protected val List<Constant> consts
 
-    protected val GeneratedTransferObject genTO;
+    protected val GeneratedTransferObject genTO
 
-    private val AbstractRangeGenerator<?> rangeGenerator
+    val AbstractRangeGenerator<?> rangeGenerator
 
     /**
      * Creates instance of this class with concrete <code>genType</code>.
@@ -184,9 +187,7 @@ class ClassTemplate extends BaseTemplate {
     def protected innerClassesDeclarations() '''
         «IF !type.enclosedTypes.empty»
             «FOR innerClass : type.enclosedTypes SEPARATOR "\n"»
-                «IF (innerClass instanceof GeneratedTransferObject)»
-                    «new ClassTemplate(javaType.getEnclosedType(innerClass.identifier), innerClass).generateAsInnerClass»
-                «ENDIF»
+                «generateInnerClass(innerClass)»
             «ENDFOR»
         «ENDIF»
     '''
@@ -222,12 +223,8 @@ class ClassTemplate extends BaseTemplate {
          * consequence of how this code is structured.
          */
         IF genTO.typedef && !allProperties.empty && allProperties.size == 1 && allProperties.get(0).name.equals("value")»
-            «Objects.importedName».requireNonNull(_value, "Supplied value may not be null");
-            «FOR c : consts»
-                «IF c.name == TypeConstants.PATTERN_CONSTANT_NAME»
-                «CodeHelpers.importedName».checkPattern(_value, «Constants.MEMBER_PATTERN_LIST», «Constants.MEMBER_REGEX_LIST»);
-                «ENDIF»
-            «ENDFOR»
+            «CodeHelpers.importedName».requireValue(_value);
+            «genPatternEnforcer("_value")»
         «ENDIF»
 
         «FOR p : properties»
@@ -271,6 +268,14 @@ class ClassTemplate extends BaseTemplate {
     }
     '''
 
+    def private genPatternEnforcer(String ref) '''
+        «FOR c : consts»
+            «IF c.name == TypeConstants.PATTERN_CONSTANT_NAME»
+            «CodeHelpers.importedName».checkPattern(«ref», «Constants.MEMBER_PATTERN_LIST», «Constants.MEMBER_REGEX_LIST»);
+            «ENDIF»
+        «ENDFOR»
+    '''
+
     def private static paramValue(Type returnType, String paramName) {
         if (returnType instanceof ConcreteType) {
             return paramName
@@ -318,7 +323,8 @@ class ClassTemplate extends BaseTemplate {
      * @param source Source object
      */
     public «type.name»(«genTO.superType.importedName» source) {
-            super(source);
+        super(source);
+        «genPatternEnforcer("getValue()")»
     }
     '''
 
@@ -327,14 +333,16 @@ class ClassTemplate extends BaseTemplate {
             «val prop = allProperties.get(0)»
             «IF !("org.opendaylight.yangtools.yang.binding.InstanceIdentifier".equals(prop.returnType.fullyQualifiedName))»
             public static «genTO.name» getDefaultInstance(String defaultValue) {
-                «IF "byte[]".equals(prop.returnType.name)»
-                    «BaseEncoding.importedName» baseEncoding = «BaseEncoding.importedName».base64();
-                    return new «genTO.name»(baseEncoding.decode(defaultValue));
-                «ELSEIF "java.lang.String".equals(prop.returnType.fullyQualifiedName)»
+                «IF BYTE_ARRAY.equals(prop.returnType)»
+                    return new «genTO.name»(«Base64.importedName».getDecoder().decode(defaultValue));
+                «ELSEIF STRING.equals(prop.returnType)»
                     return new «genTO.name»(defaultValue);
+                «ELSEIF Constants.EMPTY.equals(prop.returnType)»
+                    «Preconditions.importedName».checkArgument(defaultValue.isEmpty(), "Invalid value %s", defaultValue);
+                    return new «genTO.name»(«Empty.importedName».getInstance());
                 «ELSEIF allProperties.size > 1»
                     «bitsArgs»
-                «ELSEIF "java.lang.Boolean".equals(prop.returnType.fullyQualifiedName)»
+                «ELSEIF BOOLEAN.equals(prop.returnType)»
                     return new «genTO.name»(«Boolean.importedName».valueOf(defaultValue));
                 «ELSEIF "java.lang.Byte".equals(prop.returnType.fullyQualifiedName)»
                     return new «genTO.name»(«Byte.importedName».valueOf(defaultValue));
@@ -473,23 +481,23 @@ class ClassTemplate extends BaseTemplate {
      *
      * @return string with the <code>hashCode()</code> method definition in JAVA format
      */
-    def protected generateHashCode() '''
-        «IF !genTO.hashCodeIdentifiers.empty»
-            @Override
+    def protected generateHashCode() {
+        val size = genTO.hashCodeIdentifiers.size
+        if (size == 0) {
+            return ""
+        }
+        return '''
+            @«Override.importedName»
             public int hashCode() {
-                final int prime = 31;
-                int result = 1;
-                «FOR property : genTO.hashCodeIdentifiers»
-                    «IF property.returnType.name.contains("[")»
-                    result = prime * result + «Arrays.importedName».hashCode(«property.fieldName»);
-                    «ELSE»
-                    result = prime * result + «Objects.importedName».hashCode(«property.fieldName»);
-                    «ENDIF»
-                «ENDFOR»
-                return result;
+                «IF size != 1»
+                    «hashCodeResult(genTO.hashCodeIdentifiers)»
+                    return result;
+                «ELSE»
+                    return «CodeHelpers.importedName».wrapperHashCode(«genTO.hashCodeIdentifiers.get(0).fieldName»);
+                «ENDIF»
             }
-        «ENDIF»
-    '''
+        '''
+    }
 
     /**
      * Template method which generates the method <code>equals()</code>.
@@ -498,7 +506,7 @@ class ClassTemplate extends BaseTemplate {
      */
     def protected generateEquals() '''
         «IF !genTO.equalsIdentifiers.empty»
-            @Override
+            @«Override.importedName»
             public boolean equals(java.lang.Object obj) {
                 if (this == obj) {
                     return true;
@@ -512,11 +520,7 @@ class ClassTemplate extends BaseTemplate {
                 «type.name» other = («type.name») obj;
                 «FOR property : genTO.equalsIdentifiers»
                     «val fieldName = property.fieldName»
-                    «IF property.returnType.name.contains("[")»
-                    if (!«Arrays.importedName».equals(«fieldName», other.«fieldName»)) {
-                    «ELSE»
-                    if (!«Objects.importedName».equals(«fieldName», other.«fieldName»)) {
-                    «ENDIF»
+                    if (!«property.importedUtilClass».equals(«fieldName», other.«fieldName»)) {
                         return false;
                     }
                 «ENDFOR»