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 com.google.common.collect.ImmutableMap
14 import java.util.Arrays
17 import java.util.Objects
18 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty
19 import org.opendaylight.mdsal.binding.model.api.GeneratedType
20 import org.opendaylight.mdsal.binding.model.api.Type
21 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping
22 import org.opendaylight.yangtools.yang.binding.DataObject
24 class BuilderImplTemplate extends AbstractBuilderTemplate {
27 new(BuilderTemplate builder, GeneratedType type) {
28 super(builder.javaType.getEnclosedType(type.identifier), type, builder.targetType, builder.properties,
29 builder.augmentType, builder.keyType)
30 this.builderType = builder.type
34 private static final class «type.name» implements «targetType.importedName» {
36 «generateFields(true)»
38 «generateAugmentField(true)»
40 «generateCopyConstructor(builderType, type)»
42 @«Override.importedName»
43 public «Class.importedName»<«targetType.importedName»> getImplementedInterface() {
44 return «targetType.importedName».class;
47 «generateGetters(true)»
53 «generateToString(properties)»
58 * Template method which generates the method <code>hashCode()</code>.
60 * @return string with the <code>hashCode()</code> method definition in JAVA format
62 def protected generateHashCode() '''
63 «IF !properties.empty || augmentType !== null»
65 private volatile boolean hashValid = false;
67 @«Override.importedName»
68 public int hashCode() {
75 «FOR property : properties»
76 «IF property.returnType.name.contains("[")»
77 result = prime * result + «Arrays.importedName».hashCode(«property.fieldName»);
79 result = prime * result + «Objects.importedName».hashCode(«property.fieldName»);
82 «IF augmentType !== null»
83 result = prime * result + «Objects.importedName».hashCode(«AUGMENTATION_FIELD»);
94 * Template method which generates the method <code>equals()</code>.
96 * @return string with the <code>equals()</code> method definition in JAVA format
98 def protected generateEquals() '''
99 «IF !properties.empty || augmentType !== null»
100 @«Override.importedName»
101 public boolean equals(«Object.importedName» obj) {
105 if (!(obj instanceof «DataObject.importedName»)) {
108 if (!«targetType.importedName».class.equals(((«DataObject.importedName»)obj).getImplementedInterface())) {
111 «targetType.importedName» other = («targetType.importedName»)obj;
112 «FOR property : properties»
113 «val fieldName = property.fieldName»
114 «IF property.returnType.name.contains("[")»
115 if (!«Arrays.importedName».equals(«fieldName», other.«property.getterMethodName»())) {
117 if (!«Objects.importedName».equals(«fieldName», other.«property.getterMethodName»())) {
122 «IF augmentType !== null»
123 if (getClass() == obj.getClass()) {
124 // Simple case: we are comparing against self
125 «type.name» otherImpl = («type.name») obj;
126 if (!«Objects.importedName».equals(«AUGMENTATION_FIELD», otherImpl.«AUGMENTATION_FIELD»)) {
130 // Hard case: compare our augments with presence there...
131 for («Map.importedName».Entry<«Class.importedName»<? extends «augmentType.importedName»>, «augmentType.importedName»> e : «AUGMENTATION_FIELD».entrySet()) {
132 if (!e.getValue().equals(other.«AUGMENTABLE_AUGMENTATION_NAME»(e.getKey()))) {
136 // .. and give the other one the chance to do the same
137 if (!obj.equals(this)) {
147 override protected generateCopyKeys(List<GeneratedProperty> keyProps) '''
148 if (base.«BindingMapping.IDENTIFIABLE_KEY_NAME»() != null) {
149 this.key = base.«BindingMapping.IDENTIFIABLE_KEY_NAME»();
151 this.key = new «keyType.importedName»(«FOR keyProp : keyProps SEPARATOR ", "»base.«keyProp.getterMethodName»()«ENDFOR»);
153 «FOR field : keyProps»
154 this.«field.fieldName» = key.«field.getterMethodName»();
158 override protected generateCopyAugmentation(Type implType) '''
159 this.«AUGMENTATION_FIELD» = «ImmutableMap.importedName».copyOf(base.«AUGMENTATION_FIELD»);