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.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.spec.naming.BindingMapping
22 import org.opendaylight.yangtools.yang.binding.AbstractAugmentable
23 import org.opendaylight.yangtools.yang.binding.DataObject
25 class BuilderImplTemplate extends AbstractBuilderTemplate {
28 new(BuilderTemplate builder, GeneratedType type) {
29 super(builder.javaType.getEnclosedType(type.identifier), type, builder.targetType, builder.properties,
30 builder.augmentType, builder.keyType)
31 this.builderType = builder.type
35 «targetType.annotations.generateDeprecatedAnnotation»
36 private static final class «type.name»
37 «val impIface = targetType.importedName»
38 «IF augmentType !== null»
39 extends «AbstractAugmentable.importedName»<«impIface»>
41 implements «impIface» {
43 «generateFields(true)»
45 «generateCopyConstructor(builderType, type)»
47 «generateGetters(true)»
53 «generateToString(properties)»
57 override generateDeprecatedAnnotation(AnnotationType ann) {
58 return generateAnnotation(ann)
62 * Template method which generates the method <code>hashCode()</code>.
64 * @return string with the <code>hashCode()</code> method definition in JAVA format
66 def protected generateHashCode() '''
67 «IF !properties.empty || augmentType !== null»
69 private volatile boolean hashValid = false;
71 @«Override.importedName»
72 public int hashCode() {
77 «hashCodeResult(properties)»
78 «IF augmentType !== null»
79 result = prime * result + «Objects.importedName».hashCode(augmentations());
90 * Template method which generates the method <code>equals()</code>.
92 * @return string with the <code>equals()</code> method definition in JAVA format
94 def protected generateEquals() '''
95 «IF !properties.empty || augmentType !== null»
96 @«Override.importedName»
97 public boolean equals(«Object.importedName» obj) {
101 if (!(obj instanceof «DataObject.importedName»)) {
104 if (!«targetType.importedName».class.equals(((«DataObject.importedName»)obj).«DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME»())) {
107 «targetType.importedName» other = («targetType.importedName»)obj;
108 «FOR property : properties»
109 «val fieldName = property.fieldName»
110 if (!«property.importedUtilClass».equals(«fieldName», other.«property.getterMethodName»())) {
114 «IF augmentType !== null»
115 if (getClass() == obj.getClass()) {
116 // Simple case: we are comparing against self
117 «type.name» otherImpl = («type.name») obj;
118 if (!«Objects.importedName».equals(augmentations(), otherImpl.augmentations())) {
122 // Hard case: compare our augments with presence there...
123 for («Map.importedName».Entry<«Class.importedName»<? extends «augmentType.importedName»>, «augmentType.importedName»> e : augmentations().entrySet()) {
124 if (!e.getValue().equals(other.«AUGMENTABLE_AUGMENTATION_NAME»(e.getKey()))) {
128 // .. and give the other one the chance to do the same
129 if (!obj.equals(this)) {
139 override protected generateCopyKeys(List<GeneratedProperty> keyProps) '''
140 if (base.«BindingMapping.IDENTIFIABLE_KEY_NAME»() != null) {
141 this.key = base.«BindingMapping.IDENTIFIABLE_KEY_NAME»();
143 this.key = new «keyType.importedName»(«FOR keyProp : keyProps SEPARATOR ", "»base.«keyProp.getterMethodName»()«ENDFOR»);
145 «FOR field : keyProps»
146 this.«field.fieldName» = key.«field.getterMethodName»();
150 override protected generateCopyAugmentation(Type implType) '''
151 super(base.«AUGMENTATION_FIELD»);