Use Map.of() instead of Collections.emptyMap() in builders
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / AbstractBuilderTemplate.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 java.util.ArrayList
11 import java.util.Collection
12 import java.util.Comparator
13 import java.util.List
14 import java.util.Set
15 import org.opendaylight.mdsal.binding.model.api.AnnotationType
16 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty
17 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
18 import org.opendaylight.mdsal.binding.model.api.GeneratedType
19 import org.opendaylight.mdsal.binding.model.api.Type
20 import org.opendaylight.mdsal.binding.model.ri.BindingTypes
21 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping
22
23 abstract class AbstractBuilderTemplate extends BaseTemplate {
24     static val Comparator<GeneratedProperty> KEY_PROPS_COMPARATOR = [ p1, p2 | return p1.name.compareTo(p2.name) ]
25
26     /**
27      * Generated property is set if among methods is found one with the name GET_AUGMENTATION_METHOD_NAME.
28      */
29     protected val Type augmentType
30
31     /**
32      * Set of class attributes (fields) which are derived from the getter methods names.
33      */
34     protected val Set<BuilderGeneratedProperty> properties
35
36     /**
37      * GeneratedType for key type, null if this type does not have a key.
38      */
39     protected val Type keyType
40
41     protected val GeneratedType targetType;
42
43     new(AbstractJavaGeneratedType javaType, GeneratedType type, GeneratedType targetType,
44             Set<BuilderGeneratedProperty> properties, Type augmentType, Type keyType) {
45         super(javaType, type)
46         this.targetType = targetType
47         this.properties = properties
48         this.augmentType = augmentType
49         this.keyType = keyType
50     }
51
52     new(GeneratedType type, GeneratedType targetType, Type keyType) {
53         super(type)
54         this.targetType = targetType
55         this.keyType = keyType
56
57         val analysis = analyzeTypeHierarchy(targetType)
58         augmentType = analysis.key
59         properties = analysis.value
60     }
61
62     /**
63      * Template method which generates class attributes.
64      *
65      * @param makeFinal value which specify whether field is|isn't final
66      * @return string with class attributes and their types
67      */
68     def protected final generateFields(boolean makeFinal) '''
69         «IF properties !== null»
70             «FOR f : properties»
71                 private«IF makeFinal» final«ENDIF» «f.returnType.importedName» «f.fieldName»;
72             «ENDFOR»
73         «ENDIF»
74         «IF keyType !== null»
75             private«IF makeFinal» final«ENDIF» «keyType.importedName» key;
76         «ENDIF»
77     '''
78
79     /**
80      * Template method which generate getter methods for IMPL class.
81      *
82      * @return string with getter methods
83      */
84     def final generateGetters(boolean addOverride) '''
85         «IF keyType !== null»
86             «IF addOverride»@«OVERRIDE.importedName»«ENDIF»
87             public «keyType.importedName» «BindingMapping.IDENTIFIABLE_KEY_NAME»() {
88                 return key;
89             }
90
91         «ENDIF»
92         «IF !properties.empty»
93             «FOR field : properties SEPARATOR '\n'»
94                 «IF addOverride»@«OVERRIDE.importedName»«ENDIF»
95                 «field.getterMethod»
96             «ENDFOR»
97         «ENDIF»
98     '''
99
100     def protected final CharSequence generateCopyConstructor(Type fromType, Type implType) '''
101         «type.name»(«fromType.importedName» base) {
102             «IF augmentType !== null»
103                 «generateCopyAugmentation(implType)»
104             «ENDIF»
105             «IF keyType !== null && implementsIfc(targetType, BindingTypes.identifiable(targetType))»
106                 «val keyProps = new ArrayList((keyType as GeneratedTransferObject).properties)»
107                 «keyProps.sort(KEY_PROPS_COMPARATOR)»
108                 «val allProps = new ArrayList(properties)»
109                 «FOR field : keyProps»
110                     «removeProperty(allProps, field.name)»
111                 «ENDFOR»
112                 «generateCopyKeys(keyProps)»
113                 «generateCopyNonKeys(allProps)»
114             «ELSE»
115                 «generateCopyNonKeys(properties)»
116             «ENDIF»
117         }
118     '''
119
120     def protected final CharSequence generateDeprecatedAnnotation(List<AnnotationType> annotations) {
121         var AnnotationType found = annotations.findDeprecatedAnnotation
122         if (found === null) {
123             return ""
124         }
125         return generateDeprecatedAnnotation(found)
126     }
127
128     def protected abstract CharSequence generateCopyKeys(List<GeneratedProperty> keyProps)
129
130     def protected abstract CharSequence generateCopyNonKeys(Collection<BuilderGeneratedProperty> props)
131
132     def protected abstract CharSequence generateCopyAugmentation(Type implType)
133
134     def protected abstract CharSequence generateDeprecatedAnnotation(AnnotationType ann)
135
136     private def boolean implementsIfc(GeneratedType type, Type impl) {
137         for (Type ifc : type.implements) {
138             if (ifc.equals(impl)) {
139                 return true;
140             }
141         }
142         return false;
143     }
144
145     private def void removeProperty(Collection<BuilderGeneratedProperty> props, String name) {
146         val iter = props.iterator
147         while (iter.hasNext) {
148             if (name.equals(iter.next.name)) {
149                 iter.remove
150                 return
151             }
152         }
153     }
154
155     private static def findDeprecatedAnnotation(List<AnnotationType> annotations) {
156         if (annotations !== null) {
157             for (annotation : annotations) {
158                 if (DEPRECATED.equals(annotation.identifier)) {
159                     return annotation
160                 }
161             }
162         }
163         return null
164     }
165
166     package static def hasNonDefaultMethods(GeneratedType type) {
167         !type.methodDefinitions.isEmpty && type.methodDefinitions.exists([def | !def.isDefault])
168     }
169
170     package static def nonDefaultMethods(GeneratedType type) {
171         type.methodDefinitions.filter([def | !def.isDefault])
172     }
173 }