Cleanup equals() template
[controller.git] / opendaylight / config / yang-jmx-generator-plugin / src / main / java / org / opendaylight / controller / config / yangjmxgenerator / plugin / ftl / TemplateFactory.java
index 00454d8acf14507a518e63b5d71b79020ce89bfd..294520b36389992edb75af3e0121c7232728496d 100644 (file)
@@ -11,7 +11,6 @@ import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
@@ -64,7 +63,7 @@ public class TemplateFactory {
         { // create GeneralInterfaceFtlFile for runtime MXBean. Attributes will
           // be transformed to getter methods
             String mxBeanTypeName = entry.getJavaNameOfRuntimeMXBean();
-            List<String> extendedInterfaces = Arrays.asList(RuntimeBean.class
+            List<String> extendedInterfaces = Collections.singletonList(RuntimeBean.class
                     .getCanonicalName());
             List<MethodDeclaration> methods = new ArrayList<>();
 
@@ -84,7 +83,7 @@ public class TemplateFactory {
                 // convert JavaAttribute parameters into fields
                 List<Field> fields = new ArrayList<>();
                 for (JavaAttribute ja : rpc.getParameters()) {
-                    Field field = new Field(Collections.<String> emptyList(),
+                    Field field = new Field(Collections.emptyList(),
                             ja.getType().getFullyQualifiedName(),
                             ja.getLowerCaseCammelCase(), ja.getNullableDefaultWrappedForCode());
                     fields.add(field);
@@ -179,8 +178,7 @@ public class TemplateFactory {
     public static AbstractFactoryTemplate abstractFactoryTemplateFromMbe(
             final ModuleMXBeanEntry mbe) {
         AbstractFactoryAttributesProcessor attrProcessor = new AbstractFactoryAttributesProcessor();
-        attrProcessor.processAttributes(mbe.getAttributes(),
-                mbe.getPackageName());
+        attrProcessor.processAttributes(mbe.getAttributes());
 
 
 
@@ -205,7 +203,7 @@ public class TemplateFactory {
         boolean generateRuntime = false;
         String registratorFullyQualifiedName = null;
         if (mbe.getRuntimeBeans() != null
-                && mbe.getRuntimeBeans().isEmpty() == false) {
+                && !mbe.getRuntimeBeans().isEmpty()) {
             generateRuntime = true;
             RuntimeBeanEntry rootEntry = RuntimeRegistratorFtlTemplate
                     .findRoot(mbe.getRuntimeBeans());
@@ -294,7 +292,7 @@ public class TemplateFactory {
                 continue;
             }
 
-            Preconditions.checkState(yangPropertiesToTypesMap.containsKey(returnType.getAttributeYangName()) == false,
+            Preconditions.checkState(!yangPropertiesToTypesMap.containsKey(returnType.getAttributeYangName()),
                     "Duplicate TO %s for %s", returnType.getAttributeYangName(), rbe);
             yangPropertiesToTypesMap.put(returnType.getAttributeYangName(), returnType);
         }
@@ -424,6 +422,43 @@ public class TemplateFactory {
                     methods.add(setter);
                 }
 
+                // Add hashCode
+                final MethodDefinition hashCode = getHash(attrs);
+                methods.add(hashCode);
+
+                // Add equals
+                final MethodDefinition equals = getEquals(attrs);
+                methods.add(equals);
+            }
+
+            private MethodDefinition getEquals(final Map<String, AttributeIfc> attrs) {
+                final StringBuilder equalsBodyBuilder = new StringBuilder(
+                        "        if (this == o) { return true; }\n" +
+                        "        if (o == null || getClass() != o.getClass()) { return false; }\n");
+                equalsBodyBuilder.append(String.format(
+                        "        final %s that = (%s) o;\n", name, name));
+                for (AttributeIfc s : attrs.values()) {
+                    equalsBodyBuilder.append(String.format(
+                            "        if (!java.util.Objects.equals(%1$s, that.%1$s)) {\n" +
+                            "            return false;\n" +
+                            "        }\n\n", s.getLowerCaseCammelCase()));
+                }
+                equalsBodyBuilder.append(
+                        "       return true;\n");
+                return new MethodDefinition("boolean", "equals", Collections.singletonList(new Field("Object", "o")),
+                        Collections.singletonList(new Annotation("Override", Collections.<Parameter>emptyList())), equalsBodyBuilder.toString());
+            }
+
+            private static MethodDefinition getHash(final Map<String, AttributeIfc> attrs) {
+                final StringBuilder hashBodyBuilder = new StringBuilder(
+                        "        return java.util.Objects.hash(");
+                for (AttributeIfc s : attrs.values()) {
+                    hashBodyBuilder.append(s.getLowerCaseCammelCase());
+                    hashBodyBuilder.append(", ");
+                }
+                hashBodyBuilder.replace(hashBodyBuilder.length() - 2, hashBodyBuilder.length(), ");\n");
+                return new MethodDefinition("int", "hashCode", Collections.<Field>emptyList(),
+                        Collections.singletonList(new Annotation("Override", Collections.<Parameter>emptyList())), hashBodyBuilder.toString());
             }
 
             String getType() {
@@ -517,24 +552,19 @@ public class TemplateFactory {
 
         private final List<Field> fields = Lists.newArrayList();
 
-        void processAttributes(final Map<String, AttributeIfc> attributes,
-                final String packageName) {
-            for (Entry<String, AttributeIfc> attrEntry : attributes.entrySet()) {
-                String type;
-                String nullableDefaultWrapped = null;
-                AttributeIfc attributeIfc = attrEntry.getValue();
-
+        void processAttributes(final Map<String, AttributeIfc> attributes) {
+            for (AttributeIfc attributeIfc : attributes.values()) {
                 if (attributeIfc instanceof TypedAttribute) {
                     TypedAttribute typedAttribute = (TypedAttribute) attributeIfc;
-                    type = serializeType(typedAttribute.getType());
+                    String type = serializeType(typedAttribute.getType());
+
+                    fields.add(new Field(type, attributeIfc
+                            .getUpperCaseCammelCase(), null));
                 } else {
                     throw new UnsupportedOperationException(
                             "Attribute not supported: "
                                     + attributeIfc.getClass());
                 }
-
-                fields.add(new Field(type, attributeIfc
-                        .getUpperCaseCammelCase(), nullableDefaultWrapped));
             }
         }
 
@@ -660,8 +690,8 @@ public class TemplateFactory {
 
                 String setterBody = "this." + varName + " = " + varName + ";";
                 if (isListOfDependencies) {
-                    String nullCheck = String.format("if (%s == null) throw new IllegalArgumentException(\"Null not supported\");%n",
-                            varName);
+                    String nullCheck = String.format("if (%s == null) {\n%s = new java.util.ArrayList<>(); \n}%n",
+                            varName, varName);
                     setterBody = nullCheck + setterBody;
                 }
                 MethodDefinition setter = new MethodDefinition("void",