Do not lose newlines/tabs from javadoc
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / BuilderImplTemplate.xtend
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.java.api.generator
9
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
13
14 import java.util.List
15 import java.util.Map
16 import java.util.Objects
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.spec.naming.BindingMapping
21 import org.opendaylight.yangtools.yang.binding.AbstractAugmentable
22 import org.opendaylight.yangtools.yang.binding.DataObject
23
24 class BuilderImplTemplate extends AbstractBuilderTemplate {
25     val Type builderType;
26
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
31     }
32
33     override body() '''
34         private static final class «type.name»
35             «val impIface = targetType.importedName»
36             «IF augmentType !== null»
37                 extends «AbstractAugmentable.importedName»<«impIface»>
38             «ENDIF»
39             implements «impIface» {
40
41             «generateFields(true)»
42
43             «generateCopyConstructor(builderType, type)»
44
45             «generateGetters(true)»
46
47             «generateHashCode()»
48
49             «generateEquals()»
50
51             «generateToString(properties)»
52         }
53     '''
54
55     /**
56      * Template method which generates the method <code>hashCode()</code>.
57      *
58      * @return string with the <code>hashCode()</code> method definition in JAVA format
59      */
60     def protected generateHashCode() '''
61         «IF !properties.empty || augmentType !== null»
62             private int hash = 0;
63             private volatile boolean hashValid = false;
64
65             @«Override.importedName»
66             public int hashCode() {
67                 if (hashValid) {
68                     return hash;
69                 }
70
71                 «hashCodeResult(properties)»
72                 «IF augmentType !== null»
73                     result = prime * result + «Objects.importedName».hashCode(augmentations());
74                 «ENDIF»
75
76                 hash = result;
77                 hashValid = true;
78                 return result;
79             }
80         «ENDIF»
81     '''
82
83     /**
84      * Template method which generates the method <code>equals()</code>.
85      *
86      * @return string with the <code>equals()</code> method definition in JAVA format
87      */
88     def protected generateEquals() '''
89         «IF !properties.empty || augmentType !== null»
90             @«Override.importedName»
91             public boolean equals(«Object.importedName» obj) {
92                 if (this == obj) {
93                     return true;
94                 }
95                 if (!(obj instanceof «DataObject.importedName»)) {
96                     return false;
97                 }
98                 if (!«targetType.importedName».class.equals(((«DataObject.importedName»)obj).«DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME»())) {
99                     return false;
100                 }
101                 «targetType.importedName» other = («targetType.importedName»)obj;
102                 «FOR property : properties»
103                     «val fieldName = property.fieldName»
104                     if (!«property.importedUtilClass».equals(«fieldName», other.«property.getterMethodName»())) {
105                         return false;
106                     }
107                 «ENDFOR»
108                 «IF augmentType !== null»
109                     if (getClass() == obj.getClass()) {
110                         // Simple case: we are comparing against self
111                         «type.name» otherImpl = («type.name») obj;
112                         if (!«Objects.importedName».equals(augmentations(), otherImpl.augmentations())) {
113                             return false;
114                         }
115                     } else {
116                         // Hard case: compare our augments with presence there...
117                         for («Map.importedName».Entry<«Class.importedName»<? extends «augmentType.importedName»>, «augmentType.importedName»> e : augmentations().entrySet()) {
118                             if (!e.getValue().equals(other.«AUGMENTABLE_AUGMENTATION_NAME»(e.getKey()))) {
119                                 return false;
120                             }
121                         }
122                         // .. and give the other one the chance to do the same
123                         if (!obj.equals(this)) {
124                             return false;
125                         }
126                     }
127                 «ENDIF»
128                 return true;
129             }
130         «ENDIF»
131     '''
132
133     override protected generateCopyKeys(List<GeneratedProperty> keyProps) '''
134         if (base.«BindingMapping.IDENTIFIABLE_KEY_NAME»() != null) {
135             this.key = base.«BindingMapping.IDENTIFIABLE_KEY_NAME»();
136         } else {
137             this.key = new «keyType.importedName»(«FOR keyProp : keyProps SEPARATOR ", "»base.«keyProp.getterMethodName»()«ENDFOR»);
138         }
139         «FOR field : keyProps»
140             this.«field.fieldName» = key.«field.getterMethodName»();
141         «ENDFOR»
142     '''
143
144     override protected generateCopyAugmentation(Type implType) '''
145         super(base.«AUGMENTATION_FIELD»);
146     '''
147 }