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.model.util.BindingTypes.DATA_OBJECT
11 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTATION_FIELD
12 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTABLE_AUGMENTATION_NAME
13 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME
16 import org.opendaylight.mdsal.binding.model.api.AnnotationType
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.model.util.Types
21 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping
22 import org.opendaylight.yangtools.yang.binding.AbstractAugmentable
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 «targetType.annotations.generateDeprecatedAnnotation»
35 private static final class «type.name»
36 «val impIface = targetType.importedName»
37 «IF augmentType !== null»
38 extends «AbstractAugmentable.importedName»<«impIface»>
40 implements «impIface» {
42 «generateFields(true)»
44 «generateCopyConstructor(builderType, type)»
46 «generateGetters(true)»
52 «generateToString(properties)»
56 override generateDeprecatedAnnotation(AnnotationType ann) {
57 return generateAnnotation(ann)
61 * Template method which generates the method <code>hashCode()</code>.
63 * @return string with the <code>hashCode()</code> method definition in JAVA format
65 def protected generateHashCode() '''
66 «IF !properties.empty || augmentType !== null»
68 private volatile boolean hashValid = false;
70 @«OVERRIDE.importedName»
71 public int hashCode() {
76 «hashCodeResult(properties)»
77 «IF augmentType !== null»
78 result = prime * result + «JU_OBJECTS.importedName».hashCode(augmentations());
89 * Template method which generates the method <code>equals()</code>.
91 * @return string with the <code>equals()</code> method definition in JAVA format
93 def protected generateEquals() '''
94 «IF !properties.empty || augmentType !== null»
95 @«OVERRIDE.importedName»
96 public boolean equals(«Types.objectType().importedName» obj) {
100 if (!(obj instanceof «DATA_OBJECT.importedName»)) {
103 if (!«targetType.importedName».class.equals(((«DATA_OBJECT.importedName»)obj).«DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME»())) {
106 «targetType.importedName» other = («targetType.importedName»)obj;
107 «FOR property : properties»
108 «val fieldName = property.fieldName»
109 if (!«property.importedUtilClass».equals(«fieldName», other.«property.getterMethodName»())) {
113 «IF augmentType !== null»
114 if (getClass() == obj.getClass()) {
115 // Simple case: we are comparing against self
116 «type.name» otherImpl = («type.name») obj;
117 if (!«JU_OBJECTS.importedName».equals(augmentations(), otherImpl.augmentations())) {
121 // Hard case: compare our augments with presence there...
122 for («JU_MAP.importedName».Entry<«CLASS.importedName»<? extends «augmentType.importedName»>, «augmentType.importedName»> e : augmentations().entrySet()) {
123 if (!e.getValue().equals(other.«AUGMENTABLE_AUGMENTATION_NAME»(e.getKey()))) {
127 // .. and give the other one the chance to do the same
128 if (!obj.equals(this)) {
138 override protected generateCopyKeys(List<GeneratedProperty> keyProps) '''
139 if (base.«BindingMapping.IDENTIFIABLE_KEY_NAME»() != null) {
140 this.key = base.«BindingMapping.IDENTIFIABLE_KEY_NAME»();
142 this.key = new «keyType.importedName»(«FOR keyProp : keyProps SEPARATOR ", "»base.«keyProp.getterMethodName»()«ENDFOR»);
144 «FOR field : keyProps»
145 this.«field.fieldName» = key.«field.getterMethodName»();
149 override protected generateCopyAugmentation(Type implType) '''
150 super(base.«AUGMENTATION_FIELD»);