Fix ClassTemplate.genConstructor() declaration
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / ClassTemplate.xtend
index d8568bfc8257c8953727b6f6331a990d8585c21f..ddce529380605e7ce6301c536f0a2def321b3b01 100644 (file)
@@ -8,18 +8,23 @@
 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.Collections
+import java.util.Base64;
+import java.util.Comparator
 import java.util.List
 import java.util.Map
-import java.util.Objects
 import java.util.regex.Pattern
+import javax.management.ConstructorParameters
+import org.gaul.modernizer_maven_annotations.SuppressModernizer
 import org.opendaylight.mdsal.binding.model.api.ConcreteType
 import org.opendaylight.mdsal.binding.model.api.Constant
 import org.opendaylight.mdsal.binding.model.api.Enumeration
@@ -29,17 +34,24 @@ 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.common.Uint16
+import org.opendaylight.yangtools.yang.common.Uint32
+import org.opendaylight.yangtools.yang.common.Uint64
+import org.opendaylight.yangtools.yang.common.Uint8
 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
 
 /**
  * Template for generating JAVA class.
  */
+@SuppressModernizer
 class ClassTemplate extends BaseTemplate {
+    static val Comparator<GeneratedProperty> PROP_COMPARATOR = Comparator.comparing([prop | prop.name])
 
     protected val List<GeneratedProperty> properties
     protected val List<GeneratedProperty> finalProperties
     protected val List<GeneratedProperty> parentProperties
-    protected val Iterable<GeneratedProperty> allProperties
+    protected val List<GeneratedProperty> allProperties
     protected val Restrictions restrictions
 
     /**
@@ -78,12 +90,10 @@ class ClassTemplate extends BaseTemplate {
         this.parentProperties = GeneratorUtil.getPropertiesOfAllParents(genTO)
         this.restrictions = genType.restrictions
 
-        var List<GeneratedProperty> sorted = new ArrayList<GeneratedProperty>();
+        val sorted = new ArrayList();
         sorted.addAll(properties);
         sorted.addAll(parentProperties);
-        Collections.sort(sorted, [p1, p2|
-            p1.name.compareTo(p2.name)
-        ]);
+        sorted.sort(PROP_COMPARATOR);
 
         this.allProperties = sorted
         this.enums = genType.enumerations
@@ -203,7 +213,8 @@ class ClassTemplate extends BaseTemplate {
     '''
 
     def protected allValuesConstructor() '''
-    «IF genTO.typedef && !allProperties.empty && allProperties.size == 1 && allProperties.get(0).name.equals("value")»
+    «IF genTO.typedef && allProperties.size == 1 && allProperties.get(0).name.equals("value")»
+        @«ConstructorParameters.importedName»("value")
         @«ConstructorProperties.importedName»("value")
     «ENDIF»
     public «type.name»(«allProperties.asArgumentsDeclaration») {
@@ -218,14 +229,14 @@ class ClassTemplate extends BaseTemplate {
          * If we have patterns, we need to apply them to the value field. This is a sad
          * 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");
+        IF genTO.typedef && allProperties.size == 1 && allProperties.get(0).name.equals("value")»
+            «CodeHelpers.importedName».requireValue(_value);
             «genPatternEnforcer("_value")»
         «ENDIF»
 
         «FOR p : properties»
             «IF p.returnType.importedName.contains("[]")»
-                «IF genTO.typedef && !allProperties.empty && allProperties.size == 1 && allProperties.get(0).name
+                «IF genTO.typedef && allProperties.size == 1 && allProperties.get(0).name
                 .equals("value")»
                 this.«p.fieldName» = «p.fieldName».clone();
                 «ELSE»
@@ -249,7 +260,7 @@ class ClassTemplate extends BaseTemplate {
 
     '''
 
-    def protected genConstructor(GeneratedProperty property, GeneratedProperty... other) '''
+    def protected genConstructor(GeneratedProperty property, Iterable<GeneratedProperty> other) '''
     public «type.name»(«property.returnType.importedName + " " + property.name») {
         «IF false == parentProperties.empty»
             super(«parentProperties.asArguments»);
@@ -329,14 +340,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));
@@ -346,6 +359,14 @@ class ClassTemplate extends BaseTemplate {
                     return new «genTO.name»(«Integer.importedName».valueOf(defaultValue));
                 «ELSEIF "java.lang.Long".equals(prop.returnType.fullyQualifiedName)»
                     return new «genTO.name»(«Long.importedName».valueOf(defaultValue));
+                «ELSEIF "org.opendaylight.yangtools.yang.common.Uint8".equals(prop.returnType.fullyQualifiedName)»
+                    return new «genTO.name»(«Uint8.importedName».valueOf(defaultValue));
+                «ELSEIF "org.opendaylight.yangtools.yang.common.Uint16".equals(prop.returnType.fullyQualifiedName)»
+                    return new «genTO.name»(«Uint16.importedName».valueOf(defaultValue));
+                «ELSEIF "org.opendaylight.yangtools.yang.common.Uint32".equals(prop.returnType.fullyQualifiedName)»
+                    return new «genTO.name»(«Uint32.importedName».valueOf(defaultValue));
+                «ELSEIF "org.opendaylight.yangtools.yang.common.Uint64".equals(prop.returnType.fullyQualifiedName)»
+                    return new «genTO.name»(«Uint64.importedName».valueOf(defaultValue));
                 «ELSE»
                     return new «genTO.name»(new «prop.returnType.importedName»(defaultValue));
                 «ENDIF»
@@ -440,7 +461,7 @@ class ClassTemplate extends BaseTemplate {
                     FOR v : cValue.keySet SEPARATOR ", "»"«v.escapeJava»"«ENDFOR»);
                     «IF cValue.size == 1»
                         private static final «Pattern.importedName» «Constants.MEMBER_PATTERN_LIST» = «Pattern.importedName».compile(«TypeConstants.PATTERN_CONSTANT_NAME».get(0));
-                        private static final String «Constants.MEMBER_REGEX_LIST» = "«cValue.values.get(0).escapeJava»";
+                        private static final String «Constants.MEMBER_REGEX_LIST» = "«cValue.values.iterator.next.escapeJava»";
                     «ELSE»
                         private static final «Pattern.importedName»[] «Constants.MEMBER_PATTERN_LIST» = «CodeHelpers.importedName».compilePatterns(«TypeConstants.PATTERN_CONSTANT_NAME»);
                         private static final String[] «Constants.MEMBER_REGEX_LIST» = { «
@@ -483,12 +504,12 @@ class ClassTemplate extends BaseTemplate {
         return '''
             @«Override.importedName»
             public int hashCode() {
-            «IF size != 1»
-                «hashCodeResult(genTO.hashCodeIdentifiers)»
-                return result;
-            «ELSE»
-                return «CodeHelpers.importedName».wrapperHashCode(«genTO.hashCodeIdentifiers.get(0).fieldName»);
-            «ENDIF»
+                «IF size != 1»
+                    «hashCodeResult(genTO.hashCodeIdentifiers)»
+                    return result;
+                «ELSE»
+                    return «CodeHelpers.importedName».wrapperHashCode(«genTO.hashCodeIdentifiers.get(0).fieldName»);
+                «ENDIF»
             }
         '''
     }
@@ -498,20 +519,17 @@ class ClassTemplate extends BaseTemplate {
      *
      * @return string with the <code>equals()</code> method definition in JAVA format
      */
-    def protected generateEquals() '''
+    def private generateEquals() '''
         «IF !genTO.equalsIdentifiers.empty»
             @«Override.importedName»
-            public boolean equals(java.lang.Object obj) {
+            public final boolean equals(java.lang.Object obj) {
                 if (this == obj) {
                     return true;
                 }
-                if (obj == null) {
-                    return false;
-                }
-                if (getClass() != obj.getClass()) {
+                if (!(obj instanceof «type.name»)) {
                     return false;
                 }
-                «type.name» other = («type.name») obj;
+                final «type.name» other = («type.name») obj;
                 «FOR property : genTO.equalsIdentifiers»
                     «val fieldName = property.fieldName»
                     if (!«property.importedUtilClass».equals(«fieldName», other.«fieldName»)) {