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
12 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME
16 import java.util.Objects
17 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty
18 import org.opendaylight.mdsal.binding.model.api.GeneratedType
19 import org.opendaylight.mdsal.binding.model.api.Type
20 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping
21 import org.opendaylight.yangtools.yang.binding.AbstractAugmentable
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»
35 «val impIface = targetType.importedName»
36 «IF augmentType !== null»
37 extends «AbstractAugmentable.importedName»<«impIface»>
39 implements «impIface» {
41 «generateFields(true)»
43 «generateCopyConstructor(builderType, type)»
45 «generateGetters(true)»
51 «generateToString(properties)»
56 * Template method which generates the method <code>hashCode()</code>.
58 * @return string with the <code>hashCode()</code> method definition in JAVA format
60 def protected generateHashCode() '''
61 «IF !properties.empty || augmentType !== null»
63 private volatile boolean hashValid = false;
65 @«Override.importedName»
66 public int hashCode() {
71 «hashCodeResult(properties)»
72 «IF augmentType !== null»
73 result = prime * result + «Objects.importedName».hashCode(augmentations());
84 * Template method which generates the method <code>equals()</code>.
86 * @return string with the <code>equals()</code> method definition in JAVA format
88 def protected generateEquals() '''
89 «IF !properties.empty || augmentType !== null»
90 @«Override.importedName»
91 public boolean equals(«Object.importedName» obj) {
95 if (!(obj instanceof «DataObject.importedName»)) {
98 if (!«targetType.importedName».class.equals(((«DataObject.importedName»)obj).«DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME»())) {
101 «targetType.importedName» other = («targetType.importedName»)obj;
102 «FOR property : properties»
103 «val fieldName = property.fieldName»
104 if (!«property.importedUtilClass».equals(«fieldName», other.«property.getterMethodName»())) {
108 «IF augmentType !== null»
109 if (getClass() == obj.getClass()) {
110 // Simple case: we are comparing against self
111 «type.name» otherImpl = («type.name») obj;
112 if (!«Objects.importedName».equals(augmentations(), otherImpl.augmentations())) {
116 // Hard case: compare our augments with presence there...
117 for («Map.importedName».Entry<«Class.importedName»<? extends «augmentType.importedName»>, «augmentType.importedName»> e : augmentations().entrySet()) {
118 if (!e.getValue().equals(other.«AUGMENTABLE_AUGMENTATION_NAME»(e.getKey()))) {
122 // .. and give the other one the chance to do the same
123 if (!obj.equals(this)) {
133 override protected generateCopyKeys(List<GeneratedProperty> keyProps) '''
134 if (base.«BindingMapping.IDENTIFIABLE_KEY_NAME»() != null) {
135 this.key = base.«BindingMapping.IDENTIFIABLE_KEY_NAME»();
137 this.key = new «keyType.importedName»(«FOR keyProp : keyProps SEPARATOR ", "»base.«keyProp.getterMethodName»()«ENDFOR»);
139 «FOR field : keyProps»
140 this.«field.fieldName» = key.«field.getterMethodName»();
144 override protected generateCopyAugmentation(Type implType) '''
145 super(base.«AUGMENTATION_FIELD»);