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
15 import org.opendaylight.mdsal.binding.model.api.AnnotationType
16 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty
17 import org.opendaylight.mdsal.binding.model.api.GeneratedType
18 import org.opendaylight.mdsal.binding.model.api.Type
19 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping
20 import org.opendaylight.yangtools.yang.binding.AbstractAugmentable
21 import org.opendaylight.yangtools.yang.binding.DataObject
23 class BuilderImplTemplate extends AbstractBuilderTemplate {
26 new(BuilderTemplate builder, GeneratedType type) {
27 super(builder.javaType.getEnclosedType(type.identifier), type, builder.targetType, builder.properties,
28 builder.augmentType, builder.keyType)
29 this.builderType = builder.type
33 «targetType.annotations.generateDeprecatedAnnotation»
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)»
55 override generateDeprecatedAnnotation(AnnotationType ann) {
56 return generateAnnotation(ann)
60 * Template method which generates the method <code>hashCode()</code>.
62 * @return string with the <code>hashCode()</code> method definition in JAVA format
64 def protected generateHashCode() '''
65 «IF !properties.empty || augmentType !== null»
67 private volatile boolean hashValid = false;
69 @«OVERRIDE.importedName»
70 public int hashCode() {
75 «hashCodeResult(properties)»
76 «IF augmentType !== null»
77 result = prime * result + «JU_OBJECTS.importedName».hashCode(augmentations());
88 * Template method which generates the method <code>equals()</code>.
90 * @return string with the <code>equals()</code> method definition in JAVA format
92 def protected generateEquals() '''
93 «IF !properties.empty || augmentType !== null»
94 @«OVERRIDE.importedName»
95 public boolean equals(«Object.importedName» obj) {
99 if (!(obj instanceof «DataObject.importedName»)) {
102 if (!«targetType.importedName».class.equals(((«DataObject.importedName»)obj).«DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME»())) {
105 «targetType.importedName» other = («targetType.importedName»)obj;
106 «FOR property : properties»
107 «val fieldName = property.fieldName»
108 if (!«property.importedUtilClass».equals(«fieldName», other.«property.getterMethodName»())) {
112 «IF augmentType !== null»
113 if (getClass() == obj.getClass()) {
114 // Simple case: we are comparing against self
115 «type.name» otherImpl = («type.name») obj;
116 if (!«JU_OBJECTS.importedName».equals(augmentations(), otherImpl.augmentations())) {
120 // Hard case: compare our augments with presence there...
121 for («JU_MAP.importedName».Entry<«CLASS.importedName»<? extends «augmentType.importedName»>, «augmentType.importedName»> e : augmentations().entrySet()) {
122 if (!e.getValue().equals(other.«AUGMENTABLE_AUGMENTATION_NAME»(e.getKey()))) {
126 // .. and give the other one the chance to do the same
127 if (!obj.equals(this)) {
137 override protected generateCopyKeys(List<GeneratedProperty> keyProps) '''
138 if (base.«BindingMapping.IDENTIFIABLE_KEY_NAME»() != null) {
139 this.key = base.«BindingMapping.IDENTIFIABLE_KEY_NAME»();
141 this.key = new «keyType.importedName»(«FOR keyProp : keyProps SEPARATOR ", "»base.«keyProp.getterMethodName»()«ENDFOR»);
143 «FOR field : keyProps»
144 this.«field.fieldName» = key.«field.getterMethodName»();
148 override protected generateCopyAugmentation(Type implType) '''
149 super(base.«AUGMENTATION_FIELD»);