Optimize allProperties sorting 92/84392/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 4 Sep 2019 11:55:25 +0000 (13:55 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 13 Sep 2019 14:24:07 +0000 (16:24 +0200)
Java 8 has list.sort(), which we can easily short-circuit to
ArrayList's implementation by using 'val' declaration. We can
also make the comparator a constant, so that it is widely reused.

Change-Id: Ib8ce132a806d4e1ab58746ce85e732d451d82845
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit ff8b2d05432e6e35f005b9ab7974985b5b4e01d7)

binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/ClassTemplate.xtend

index 7ce8074a90b441c01fedccb5493a621aaf854893..07795a5f03a4691e2283c92a31df6d56c7da3354 100644 (file)
@@ -18,7 +18,7 @@ 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
@@ -38,6 +38,7 @@ 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
@@ -81,12 +82,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