Speed up AbstractBuilderTemplate.removeProperty()
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / ClassTemplate.xtend
index 24f2972c625635ec1a53b900295f63cbd4821eeb..c34953fd052f2fdab71844a159e2d4d5b3edc701 100644 (file)
@@ -19,10 +19,9 @@ import com.google.common.collect.Lists
 import java.beans.ConstructorProperties
 import java.util.ArrayList
 import java.util.Base64;
-import java.util.Collections
+import java.util.Comparator
 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
@@ -40,11 +39,12 @@ import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
  * Template for generating JAVA class.
  */
 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
 
     /**
@@ -83,12 +83,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
@@ -208,7 +206,7 @@ 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")»
         @«ConstructorProperties.importedName»("value")
     «ENDIF»
     public «type.name»(«allProperties.asArgumentsDeclaration») {
@@ -223,21 +221,21 @@ 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
-                .equals("value")»
-                this.«p.fieldName» = «p.fieldName».clone();
+            «val fieldName = p.fieldName»
+            «IF p.returnType.name.endsWith("[]")»
+                «IF genTO.typedef && allProperties.size == 1 && allProperties.get(0).name.equals("value")»
+                this.«fieldName» = «fieldName».clone();
                 «ELSE»
-                this.«p.fieldName» = «p.fieldName» == null ? null : «p.fieldName».clone();
+                this.«fieldName» = «fieldName» == null ? null : «fieldName».clone();
                 «ENDIF»
             «ELSE»
-            this.«p.fieldName» = «p.fieldName»;
+            this.«fieldName» = «fieldName»;
             «ENDIF»
         «ENDFOR»
     }
@@ -254,15 +252,16 @@ 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»);
         «ENDIF»
 
-        «generateRestrictions(type, property.fieldName.toString, property.returnType)»
+        «val fieldName = property.fieldName»
+        «generateRestrictions(type, fieldName.toString, property.returnType)»
 
-        this.«property.fieldName» = «property.name»;
+        this.«fieldName» = «property.name»;
         «FOR p : other»
             this.«p.fieldName» = null;
         «ENDFOR»
@@ -312,7 +311,8 @@ class ClassTemplate extends BaseTemplate {
             super(source);
         «ENDIF»
         «FOR p : properties»
-            this.«p.fieldName» = source.«p.fieldName»;
+            «val fieldName = p.fieldName»
+            this.«fieldName» = source.«fieldName»;
         «ENDFOR»
     }
     '''
@@ -447,7 +447,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» = { «