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 java.util.ArrayList
11 import java.util.Collection
12 import java.util.Comparator
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
23 abstract class AbstractBuilderTemplate extends BaseTemplate {
24 static val Comparator<GeneratedProperty> KEY_PROPS_COMPARATOR = [ p1, p2 | return p1.name.compareTo(p2.name) ]
27 * Generated property is set if among methods is found one with the name GET_AUGMENTATION_METHOD_NAME.
29 protected val Type augmentType
32 * Set of class attributes (fields) which are derived from the getter methods names.
34 protected val Set<BuilderGeneratedProperty> properties
37 * GeneratedType for key type, null if this type does not have a key.
39 protected val Type keyType
41 protected val GeneratedType targetType;
43 new(AbstractJavaGeneratedType javaType, GeneratedType type, GeneratedType targetType,
44 Set<BuilderGeneratedProperty> properties, Type augmentType, Type keyType) {
46 this.targetType = targetType
47 this.properties = properties
48 this.augmentType = augmentType
49 this.keyType = keyType
52 new(GeneratedType type, GeneratedType targetType, Type keyType) {
54 this.targetType = targetType
55 this.keyType = keyType
57 val analysis = analyzeTypeHierarchy(targetType)
58 augmentType = analysis.key
59 properties = analysis.value
63 * Template method which generates class attributes.
65 * @param makeFinal value which specify whether field is|isn't final
66 * @return string with class attributes and their types
68 def protected final generateFields(boolean makeFinal) '''
69 «IF properties !== null»
71 private«IF makeFinal» final«ENDIF» «f.returnType.importedName» «f.fieldName»;
75 private«IF makeFinal» final«ENDIF» «keyType.importedName» key;
80 * Template method which generate getter methods for IMPL class.
82 * @return string with getter methods
84 def final generateGetters(boolean addOverride) '''
87 @«OVERRIDE.importedName»
90 * Return current value associated with the property corresponding to {@link «targetType.importedName»#«BindingMapping.IDENTIFIABLE_KEY_NAME»()}.
92 * @return current value
95 public «keyType.importedName» «BindingMapping.IDENTIFIABLE_KEY_NAME»() {
100 «IF !properties.empty»
101 «FOR field : properties SEPARATOR '\n'»
103 @«OVERRIDE.importedName»
106 * Return current value associated with the property corresponding to {@link «targetType.importedName»#«field.getterName»()}.
108 * @return current value
116 def protected final CharSequence generateCopyConstructor(Type fromType, Type implType) '''
117 «type.name»(«fromType.importedName» base) {
118 «IF augmentType !== null»
119 «generateCopyAugmentation(implType)»
121 «IF keyType !== null && implementsIfc(targetType, BindingTypes.identifiable(targetType))»
122 «val keyProps = new ArrayList((keyType as GeneratedTransferObject).properties)»
123 «keyProps.sort(KEY_PROPS_COMPARATOR)»
124 «val allProps = new ArrayList(properties)»
125 «FOR field : keyProps»
126 «removeProperty(allProps, field.name)»
128 «generateCopyKeys(keyProps)»
129 «generateCopyNonKeys(allProps)»
131 «generateCopyNonKeys(properties)»
136 def protected final CharSequence generateDeprecatedAnnotation(List<AnnotationType> annotations) {
137 var AnnotationType found = annotations.findDeprecatedAnnotation
138 if (found === null) {
141 return generateDeprecatedAnnotation(found)
144 def protected abstract CharSequence generateCopyKeys(List<GeneratedProperty> keyProps)
146 def protected abstract CharSequence generateCopyNonKeys(Collection<BuilderGeneratedProperty> props)
148 def protected abstract CharSequence generateCopyAugmentation(Type implType)
150 def protected abstract CharSequence generateDeprecatedAnnotation(AnnotationType ann)
152 private def boolean implementsIfc(GeneratedType type, Type impl) {
153 for (Type ifc : type.implements) {
154 if (ifc.equals(impl)) {
161 private def void removeProperty(Collection<BuilderGeneratedProperty> props, String name) {
162 val iter = props.iterator
163 while (iter.hasNext) {
164 if (name.equals(iter.next.name)) {
171 private static def findDeprecatedAnnotation(List<AnnotationType> annotations) {
172 if (annotations !== null) {
173 for (annotation : annotations) {
174 if (DEPRECATED.equals(annotation.identifier)) {
182 package static def hasNonDefaultMethods(GeneratedType type) {
183 !type.methodDefinitions.isEmpty && type.methodDefinitions.exists([def | !def.isDefault])
186 package static def nonDefaultMethods(GeneratedType type) {
187 type.methodDefinitions.filter([def | !def.isDefault])