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) '''
86 «IF addOverride»@«OVERRIDE.importedName»«ENDIF»
87 public «keyType.importedName» «BindingMapping.IDENTIFIABLE_KEY_NAME»() {
92 «IF !properties.empty»
93 «FOR field : properties SEPARATOR '\n'»
94 «IF addOverride»@«OVERRIDE.importedName»«ENDIF»
100 def protected final CharSequence generateCopyConstructor(Type fromType, Type implType) '''
101 «type.name»(«fromType.importedName» base) {
102 «IF augmentType !== null»
103 «generateCopyAugmentation(implType)»
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)»
112 «generateCopyKeys(keyProps)»
113 «generateCopyNonKeys(allProps)»
115 «generateCopyNonKeys(properties)»
120 def protected final CharSequence generateDeprecatedAnnotation(List<AnnotationType> annotations) {
121 var AnnotationType found = annotations.findDeprecatedAnnotation
122 if (found === null) {
125 return generateDeprecatedAnnotation(found)
128 def protected abstract CharSequence generateCopyKeys(List<GeneratedProperty> keyProps)
130 def protected abstract CharSequence generateCopyNonKeys(Collection<BuilderGeneratedProperty> props)
132 def protected abstract CharSequence generateCopyAugmentation(Type implType)
134 def protected abstract CharSequence generateDeprecatedAnnotation(AnnotationType ann)
136 private def boolean implementsIfc(GeneratedType type, Type impl) {
137 for (Type ifc : type.implements) {
138 if (ifc.equals(impl)) {
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)) {
155 private static def findDeprecatedAnnotation(List<AnnotationType> annotations) {
156 if (annotations !== null) {
157 for (annotation : annotations) {
158 if (DEPRECATED.equals(annotation.identifier)) {
166 package static def hasNonDefaultMethods(GeneratedType type) {
167 !type.methodDefinitions.isEmpty && type.methodDefinitions.exists([def | !def.isDefault])
170 package static def nonDefaultMethods(GeneratedType type) {
171 type.methodDefinitions.filter([def | !def.isDefault])