X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fconfig%2Fyang-jmx-generator-plugin%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fyangjmxgenerator%2Fplugin%2Fftl%2FTemplateFactory.java;h=294520b36389992edb75af3e0121c7232728496d;hp=00454d8acf14507a518e63b5d71b79020ce89bfd;hb=44bbe7e2af257469fab2235b5116c81064746f8b;hpb=8e8516351a527f6bafb1598d679547cfbbe029f9 diff --git a/opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/TemplateFactory.java b/opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/TemplateFactory.java index 00454d8acf..294520b363 100644 --- a/opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/TemplateFactory.java +++ b/opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/TemplateFactory.java @@ -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 extendedInterfaces = Arrays.asList(RuntimeBean.class + List extendedInterfaces = Collections.singletonList(RuntimeBean.class .getCanonicalName()); List methods = new ArrayList<>(); @@ -84,7 +83,7 @@ public class TemplateFactory { // convert JavaAttribute parameters into fields List fields = new ArrayList<>(); for (JavaAttribute ja : rpc.getParameters()) { - Field field = new Field(Collections. 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 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.emptyList())), equalsBodyBuilder.toString()); + } + + private static MethodDefinition getHash(final Map 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.emptyList(), + Collections.singletonList(new Annotation("Override", Collections.emptyList())), hashBodyBuilder.toString()); } String getType() { @@ -517,24 +552,19 @@ public class TemplateFactory { private final List fields = Lists.newArrayList(); - void processAttributes(final Map attributes, - final String packageName) { - for (Entry attrEntry : attributes.entrySet()) { - String type; - String nullableDefaultWrapped = null; - AttributeIfc attributeIfc = attrEntry.getValue(); - + void processAttributes(final Map 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",