Improve class template equals() 83/103183/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 9 Nov 2022 22:53:46 +0000 (23:53 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 9 Nov 2022 22:53:46 +0000 (23:53 +0100)
Rather than generating a slew of if/return statements, use instanceof
pattern and generate a single-expression return statement.

Change-Id: Id8c0fc65736399fed3454fdbcc16a16849f86064
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 ca0961d39bff9339d6c9cd3c085ae31912abed8c..8eee22e21131e1401c62fb642149494364ec9922 100644 (file)
@@ -573,20 +573,11 @@ class ClassTemplate extends BaseTemplate {
         «IF !genTO.equalsIdentifiers.empty»
             @«OVERRIDE.importedName»
             public final boolean equals(«OBJECT.importedName» obj) {
-                if (this == obj) {
-                    return true;
-                }
-                if (!(obj instanceof «type.name»)) {
-                    return false;
-                }
-                final «type.name» other = («type.name») obj;
+                return this == obj || obj instanceof «type.name» other
                 «FOR property : genTO.equalsIdentifiers»
                     «val fieldName = property.fieldName»
-                    if (!«property.importedUtilClass».equals(«fieldName», other.«fieldName»)) {
-                        return false;
-                    }
-                «ENDFOR»
-                return true;
+                        && «property.importedUtilClass».equals(«fieldName», other.«fieldName»)«
+                »«ENDFOR»;
             }
         «ENDIF»
     '''