2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.binding.java.api.generator
10 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTATION_FIELD
11 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTABLE_AUGMENTATION_NAME
13 import java.util.Arrays
14 import java.util.Objects
16 import org.opendaylight.mdsal.binding.model.api.GeneratedType
17 import org.opendaylight.mdsal.binding.model.api.Type
18 import org.opendaylight.yangtools.yang.binding.DataObject
20 class BuilderImplTemplate extends AbstractBuilderTemplate {
23 new(BuilderTemplate builder, GeneratedType type) {
24 super(builder.javaType.getEnclosedType(type.identifier), type, builder.targetType, builder.properties,
25 builder.augmentType, builder.keyType)
26 this.builderType = builder.type
30 private static final class «type.name» implements «targetType.importedName» {
32 «generateFields(true)»
34 «generateAugmentField(true)»
36 «generateCopyConstructor(true, builderType, type)»
38 @«Override.importedName»
39 public «Class.importedName»<«targetType.importedName»> getImplementedInterface() {
40 return «targetType.importedName».class;
43 «generateGetters(true)»
49 «generateToString(properties)»
54 * Template method which generates the method <code>hashCode()</code>.
56 * @return string with the <code>hashCode()</code> method definition in JAVA format
58 def protected generateHashCode() '''
59 «IF !properties.empty || augmentType !== null»
61 private volatile boolean hashValid = false;
63 @«Override.importedName»
64 public int hashCode() {
71 «FOR property : properties»
72 «IF property.returnType.name.contains("[")»
73 result = prime * result + «Arrays.importedName».hashCode(«property.fieldName»);
75 result = prime * result + «Objects.importedName».hashCode(«property.fieldName»);
78 «IF augmentType !== null»
79 result = prime * result + «Objects.importedName».hashCode(«AUGMENTATION_FIELD»);
90 * Template method which generates the method <code>equals()</code>.
92 * @return string with the <code>equals()</code> method definition in JAVA format
94 def protected generateEquals() '''
95 «IF !properties.empty || augmentType !== null»
96 @«Override.importedName»
97 public boolean equals(«Object.importedName» obj) {
101 if (!(obj instanceof «DataObject.importedName»)) {
104 if (!«targetType.importedName».class.equals(((«DataObject.importedName»)obj).getImplementedInterface())) {
107 «targetType.importedName» other = («targetType.importedName»)obj;
108 «FOR property : properties»
109 «val fieldName = property.fieldName»
110 «IF property.returnType.name.contains("[")»
111 if (!«Arrays.importedName».equals(«fieldName», other.«property.getterMethodName»())) {
113 if (!«Objects.importedName».equals(«fieldName», other.«property.getterMethodName»())) {
118 «IF augmentType !== null»
119 if (getClass() == obj.getClass()) {
120 // Simple case: we are comparing against self
121 «type.name» otherImpl = («type.name») obj;
122 if (!«Objects.importedName».equals(«AUGMENTATION_FIELD», otherImpl.«AUGMENTATION_FIELD»)) {
126 // Hard case: compare our augments with presence there...
127 for («Map.importedName».Entry<«Class.importedName»<? extends «augmentType.importedName»>, «augmentType.importedName»> e : «AUGMENTATION_FIELD».entrySet()) {
128 if (!e.getValue().equals(other.«AUGMENTABLE_AUGMENTATION_NAME»(e.getKey()))) {
132 // .. and give the other one the chance to do the same
133 if (!obj.equals(this)) {