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
15 import java.util.Collection
17 import org.opendaylight.mdsal.binding.model.api.AnnotationType
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.model.util.Types
22 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping
23 import org.opendaylight.yangtools.yang.binding.AbstractAugmentable
24 import org.opendaylight.mdsal.binding.model.api.MethodSignature.ValueMechanics
26 class BuilderImplTemplate extends AbstractBuilderTemplate {
29 new(BuilderTemplate builder, GeneratedType type) {
30 super(builder.javaType.getEnclosedType(type.identifier), type, builder.targetType, builder.properties,
31 builder.augmentType, builder.keyType)
32 this.builderType = builder.type
36 «targetType.annotations.generateDeprecatedAnnotation»
37 private static final class «type.name»
38 «val impIface = targetType.importedName»
39 «IF augmentType !== null»
40 extends «AbstractAugmentable.importedName»<«impIface»>
42 implements «impIface» {
44 «generateFields(true)»
46 «generateCopyConstructor(builderType, type)»
48 «generateGetters(true)»
54 «generateToString(properties)»
58 override generateDeprecatedAnnotation(AnnotationType ann) {
59 return generateAnnotation(ann)
63 * Template method which generates the method <code>hashCode()</code>.
65 * @return string with the <code>hashCode()</code> method definition in JAVA format
67 def protected generateHashCode() '''
68 «IF !properties.empty || augmentType !== null»
70 private volatile boolean hashValid = false;
72 @«OVERRIDE.importedName»
73 public int hashCode() {
78 «hashCodeResult(properties)»
79 «IF augmentType !== null»
80 result = prime * result + «JU_OBJECTS.importedName».hashCode(augmentations());
91 * Template method which generates the method <code>equals()</code>.
93 * @return string with the <code>equals()</code> method definition in JAVA format
95 def protected generateEquals() '''
96 «IF !properties.empty || augmentType !== null»
97 @«OVERRIDE.importedName»
98 public boolean equals(«Types.objectType().importedName» obj) {
102 if (!(obj instanceof «DATA_OBJECT.importedName»)) {
105 if (!«targetType.importedName».class.equals(((«DATA_OBJECT.importedName»)obj).«DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME»())) {
108 «targetType.importedName» other = («targetType.importedName»)obj;
109 «FOR property : properties»
110 «val fieldName = property.fieldName»
111 if (!«property.importedUtilClass».equals(«fieldName», other.«property.getterName»())) {
115 «IF augmentType !== null»
116 if (getClass() == obj.getClass()) {
117 // Simple case: we are comparing against self
118 «type.name» otherImpl = («type.name») obj;
119 if (!«JU_OBJECTS.importedName».equals(augmentations(), otherImpl.augmentations())) {
123 // Hard case: compare our augments with presence there...
124 for («JU_MAP.importedName».Entry<«CLASS.importedName»<? extends «augmentType.importedName»>, «augmentType.importedName»> e : augmentations().entrySet()) {
125 if (!e.getValue().equals(other.«AUGMENTABLE_AUGMENTATION_NAME»(e.getKey()))) {
129 // .. and give the other one the chance to do the same
130 if (!obj.equals(this)) {
140 override protected generateCopyKeys(List<GeneratedProperty> keyProps) '''
141 if (base.«BindingMapping.IDENTIFIABLE_KEY_NAME»() != null) {
142 this.key = base.«BindingMapping.IDENTIFIABLE_KEY_NAME»();
144 this.key = new «keyType.importedName»(«FOR keyProp : keyProps SEPARATOR ", "»base.«keyProp.getterMethodName»()«ENDFOR»);
146 «FOR field : keyProps»
147 this.«field.fieldName» = key.«field.getterMethodName»();
151 override protected CharSequence generateCopyNonKeys(Collection<BuilderGeneratedProperty> props) '''
153 «IF field.mechanics === ValueMechanics.NULLIFY_EMPTY»
154 this.«field.fieldName» = «CODEHELPERS.importedName».emptyToNull(base.«field.getterName»());
156 this.«field.fieldName» = base.«field.getterName»();
161 override protected generateCopyAugmentation(Type implType) '''
162 super(base.«AUGMENTATION_FIELD»);