Separate out builder/impl copy generators
[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
13 import com.google.common.collect.ImmutableMap
14 import java.util.Arrays
15 import java.util.List
16 import java.util.Map
17 import java.util.Objects
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.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» implements «targetType.importedName» {
35
36             «generateFields(true)»
37
38             «generateAugmentField(true)»
39
40             «generateCopyConstructor(builderType, type)»
41
42             @«Override.importedName»
43             public «Class.importedName»<«targetType.importedName»> getImplementedInterface() {
44                 return «targetType.importedName».class;
45             }
46
47             «generateGetters(true)»
48
49             «generateHashCode()»
50
51             «generateEquals()»
52
53             «generateToString(properties)»
54         }
55     '''
56
57     /**
58      * Template method which generates the method <code>hashCode()</code>.
59      *
60      * @return string with the <code>hashCode()</code> method definition in JAVA format
61      */
62     def protected generateHashCode() '''
63         «IF !properties.empty || augmentType !== null»
64             private int hash = 0;
65             private volatile boolean hashValid = false;
66
67             @«Override.importedName»
68             public int hashCode() {
69                 if (hashValid) {
70                     return hash;
71                 }
72
73                 final int prime = 31;
74                 int result = 1;
75                 «FOR property : properties»
76                     «IF property.returnType.name.contains("[")»
77                     result = prime * result + «Arrays.importedName».hashCode(«property.fieldName»);
78                     «ELSE»
79                     result = prime * result + «Objects.importedName».hashCode(«property.fieldName»);
80                     «ENDIF»
81                 «ENDFOR»
82                 «IF augmentType !== null»
83                     result = prime * result + «Objects.importedName».hashCode(«AUGMENTATION_FIELD»);
84                 «ENDIF»
85
86                 hash = result;
87                 hashValid = true;
88                 return result;
89             }
90         «ENDIF»
91     '''
92
93     /**
94      * Template method which generates the method <code>equals()</code>.
95      *
96      * @return string with the <code>equals()</code> method definition in JAVA format
97      */
98     def protected generateEquals() '''
99         «IF !properties.empty || augmentType !== null»
100             @«Override.importedName»
101             public boolean equals(«Object.importedName» obj) {
102                 if (this == obj) {
103                     return true;
104                 }
105                 if (!(obj instanceof «DataObject.importedName»)) {
106                     return false;
107                 }
108                 if (!«targetType.importedName».class.equals(((«DataObject.importedName»)obj).getImplementedInterface())) {
109                     return false;
110                 }
111                 «targetType.importedName» other = («targetType.importedName»)obj;
112                 «FOR property : properties»
113                     «val fieldName = property.fieldName»
114                     «IF property.returnType.name.contains("[")»
115                     if (!«Arrays.importedName».equals(«fieldName», other.«property.getterMethodName»())) {
116                     «ELSE»
117                     if (!«Objects.importedName».equals(«fieldName», other.«property.getterMethodName»())) {
118                     «ENDIF»
119                         return false;
120                     }
121                 «ENDFOR»
122                 «IF augmentType !== null»
123                     if (getClass() == obj.getClass()) {
124                         // Simple case: we are comparing against self
125                         «type.name» otherImpl = («type.name») obj;
126                         if (!«Objects.importedName».equals(«AUGMENTATION_FIELD», otherImpl.«AUGMENTATION_FIELD»)) {
127                             return false;
128                         }
129                     } else {
130                         // Hard case: compare our augments with presence there...
131                         for («Map.importedName».Entry<«Class.importedName»<? extends «augmentType.importedName»>, «augmentType.importedName»> e : «AUGMENTATION_FIELD».entrySet()) {
132                             if (!e.getValue().equals(other.«AUGMENTABLE_AUGMENTATION_NAME»(e.getKey()))) {
133                                 return false;
134                             }
135                         }
136                         // .. and give the other one the chance to do the same
137                         if (!obj.equals(this)) {
138                             return false;
139                         }
140                     }
141                 «ENDIF»
142                 return true;
143             }
144         «ENDIF»
145     '''
146
147     override protected generateCopyKeys(List<GeneratedProperty> keyProps) '''
148         if (base.«BindingMapping.IDENTIFIABLE_KEY_NAME»() != null) {
149             this.key = base.«BindingMapping.IDENTIFIABLE_KEY_NAME»();
150         } else {
151             this.key = new «keyType.importedName»(«FOR keyProp : keyProps SEPARATOR ", "»base.«keyProp.getterMethodName»()«ENDFOR»);
152         }
153         «FOR field : keyProps»
154             this.«field.fieldName» = key.«field.getterMethodName»();
155         «ENDFOR»
156     '''
157
158     override protected generateCopyAugmentation(Type implType) '''
159         this.«AUGMENTATION_FIELD» = «ImmutableMap.importedName».copyOf(base.«AUGMENTATION_FIELD»);
160     '''
161 }