Improve class template equals()
[yangtools.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / ClassTemplate.xtend
index 2638e8a99cdad252eedb9782579f11c81f999f90..8eee22e21131e1401c62fb642149494364ec9922 100644 (file)
@@ -23,7 +23,6 @@ import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.UINT32_TYPE
 import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.UINT64_TYPE
 import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.UINT8_TYPE
 import static org.opendaylight.mdsal.binding.model.ri.BindingTypes.SCALAR_TYPE_OBJECT
-import static org.opendaylight.mdsal.binding.model.ri.Types.BOOLEAN
 import static org.opendaylight.mdsal.binding.model.ri.Types.STRING;
 import static extension org.apache.commons.text.StringEscapeUtils.escapeJava
 import static extension org.opendaylight.mdsal.binding.model.ri.BindingTypes.isBitsType
@@ -40,7 +39,6 @@ import java.util.List
 import java.util.Map
 import java.util.Set
 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
@@ -55,7 +53,6 @@ import org.opendaylight.yangtools.yang.common.Empty
 /**
  * Template for generating JAVA class.
  */
-@SuppressModernizer
 class ClassTemplate extends BaseTemplate {
     static val Comparator<GeneratedProperty> PROP_COMPARATOR = Comparator.comparing([prop | prop.name])
     static val VALUEOF_TYPES = Set.of(
@@ -189,7 +186,7 @@ class ClassTemplate extends BaseTemplate {
 
     '''
 
-    def private propertyMethods() {
+    def protected propertyMethods() {
         if (properties.empty) {
             return ""
         }
@@ -338,7 +335,7 @@ class ClassTemplate extends BaseTemplate {
 
     def private genPatternEnforcer(String ref) '''
         «FOR c : consts»
-            «IF c.name == TypeConstants.PATTERN_CONSTANT_NAME»
+            «IF TypeConstants.PATTERN_CONSTANT_NAME.equals(c.name)»
             «CODEHELPERS.importedName».checkPattern(«ref», «Constants.MEMBER_PATTERN_LIST», «Constants.MEMBER_REGEX_LIST»);
             «ENDIF»
         «ENDFOR»
@@ -431,7 +428,7 @@ class ClassTemplate extends BaseTemplate {
         int i = 0;
         return new «genTO.name»(
         «FOR prop : allProperties SEPARATOR ","»
-            properties.get(i++).equals(defaultValue) ? «BOOLEAN.importedName».TRUE : null
+            properties.get(i++).equals(defaultValue) ? true : false
         «ENDFOR»
         );
     '''
@@ -503,7 +500,7 @@ class ClassTemplate extends BaseTemplate {
     def protected constantsDeclarations() '''
         «IF !consts.empty»
             «FOR c : consts»
-                «IF c.name == TypeConstants.PATTERN_CONSTANT_NAME»
+                «IF TypeConstants.PATTERN_CONSTANT_NAME.equals(c.name)»
                     «val cValue = c.value as Map<String, String>»
                     «val jurPatternRef = JUR_PATTERN.importedName»
                     public static final «JU_LIST.importedName»<String> «TypeConstants.PATTERN_CONSTANT_NAME» = «ImmutableList.importedName».of(«
@@ -575,21 +572,12 @@ class ClassTemplate extends BaseTemplate {
     def private generateEquals() '''
         «IF !genTO.equalsIdentifiers.empty»
             @«OVERRIDE.importedName»
-            public final boolean equals(java.lang.Object obj) {
-                if (this == obj) {
-                    return true;
-                }
-                if (!(obj instanceof «type.name»)) {
-                    return false;
-                }
-                final «type.name» other = («type.name») obj;
+            public final boolean equals(«OBJECT.importedName» 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»
     '''