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;
{ // 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<>();
public static AbstractFactoryTemplate abstractFactoryTemplateFromMbe(
final ModuleMXBeanEntry mbe) {
AbstractFactoryAttributesProcessor attrProcessor = new AbstractFactoryAttributesProcessor();
- attrProcessor.processAttributes(mbe.getAttributes(),
- mbe.getPackageName());
+ attrProcessor.processAttributes(mbe.getAttributes());
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());
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);
}
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) == false) {\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() {
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));
}
}
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",