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.model.util.Types.STRING;
12 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTATION_FIELD
13 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTABLE_AUGMENTATION_NAME
14 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.BINDING_TO_STRING_NAME
15 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME
17 import java.util.Collection
19 import org.opendaylight.mdsal.binding.model.api.AnnotationType
20 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty
21 import org.opendaylight.mdsal.binding.model.api.GeneratedType
22 import org.opendaylight.mdsal.binding.model.api.Type
23 import org.opendaylight.mdsal.binding.model.util.Types
24 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping
25 import org.opendaylight.yangtools.yang.binding.AbstractAugmentable
26 import org.opendaylight.mdsal.binding.model.api.MethodSignature.ValueMechanics
28 class BuilderImplTemplate extends AbstractBuilderTemplate {
31 new(BuilderTemplate builder, GeneratedType type) {
32 super(builder.javaType.getEnclosedType(type.identifier), type, builder.targetType, builder.properties,
33 builder.augmentType, builder.keyType)
34 this.builderType = builder.type
38 «targetType.annotations.generateDeprecatedAnnotation»
39 private static final class «type.name»
40 «val impIface = targetType.importedName»
41 «IF augmentType !== null»
42 extends «AbstractAugmentable.importedName»<«impIface»>
44 implements «impIface» {
46 «generateFields(true)»
48 «generateCopyConstructor(builderType, type)»
50 «generateGetters(true)»
60 override generateDeprecatedAnnotation(AnnotationType ann) {
61 return generateAnnotation(ann)
65 * Template method which generates the method <code>hashCode()</code>.
67 * @return string with the <code>hashCode()</code> method definition in JAVA format
69 def protected generateHashCode() '''
70 «IF !properties.empty || augmentType !== null»
72 private volatile boolean hashValid = false;
74 @«OVERRIDE.importedName»
75 public int hashCode() {
80 «hashCodeResult(properties)»
81 «IF augmentType !== null»
82 result = prime * result + «JU_OBJECTS.importedName».hashCode(augmentations());
93 * Template method which generates the method <code>equals()</code>.
95 * @return string with the <code>equals()</code> method definition in JAVA format
97 def protected generateEquals() '''
98 «IF !properties.empty || augmentType !== null»
99 @«OVERRIDE.importedName»
100 public boolean equals(«Types.objectType().importedName» obj) {
104 if (!(obj instanceof «DATA_OBJECT.importedName»)) {
107 if (!«targetType.importedName».class.equals(((«DATA_OBJECT.importedName»)obj).«DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME»())) {
110 «targetType.importedName» other = («targetType.importedName»)obj;
111 «FOR property : properties»
112 «val fieldName = property.fieldName»
113 if (!«property.importedUtilClass».equals(«fieldName», other.«property.getterName»())) {
117 «IF augmentType !== null»
118 if (getClass() == obj.getClass()) {
119 // Simple case: we are comparing against self
120 «type.name» otherImpl = («type.name») obj;
121 if (!«JU_OBJECTS.importedName».equals(augmentations(), otherImpl.augmentations())) {
125 // Hard case: compare our augments with presence there...
126 for («JU_MAP.importedName».Entry<«CLASS.importedName»<? extends «augmentType.importedName»>, «augmentType.importedName»> e : augmentations().entrySet()) {
127 if (!e.getValue().equals(other.«AUGMENTABLE_AUGMENTATION_NAME»(e.getKey()))) {
131 // .. and give the other one the chance to do the same
132 if (!obj.equals(this)) {
143 * Template method which generates the method <code>toString()</code>.
145 * @return string with the <code>toString()</code> method definition in JAVA format
147 def protected generateToString() '''
148 @«OVERRIDE.importedName»
149 public «STRING.importedName» toString() {
150 return «targetType.importedName».«BINDING_TO_STRING_NAME»(this);
154 override protected generateCopyKeys(List<GeneratedProperty> keyProps) '''
155 if (base.«BindingMapping.IDENTIFIABLE_KEY_NAME»() != null) {
156 this.key = base.«BindingMapping.IDENTIFIABLE_KEY_NAME»();
158 this.key = new «keyType.importedName»(«FOR keyProp : keyProps SEPARATOR ", "»base.«keyProp.getterMethodName»()«ENDFOR»);
160 «FOR field : keyProps»
161 this.«field.fieldName» = key.«field.getterMethodName»();
165 override protected CharSequence generateCopyNonKeys(Collection<BuilderGeneratedProperty> props) '''
167 «IF field.mechanics === ValueMechanics.NULLIFY_EMPTY»
168 this.«field.fieldName» = «CODEHELPERS.importedName».emptyToNull(base.«field.getterName»());
170 this.«field.fieldName» = base.«field.getterName»();
175 override protected generateCopyAugmentation(Type implType) '''
176 super(base.«AUGMENTATION_FIELD»);