Optimize allProperties sorting 92/84192/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 4 Sep 2019 11:55:25 +0000 (13:55 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 4 Sep 2019 12:03:08 +0000 (14:03 +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>
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/ClassTemplate.xtend

index 18baa3440388d00432e73d5f656beddb496de228..f4c9024ae0f7bd4d62336eec74d59645ef7c7880 100644 (file)
@@ -19,7 +19,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.regex.Pattern
@@ -46,6 +46,7 @@ import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
  */
 @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
@@ -89,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