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 static org.opendaylight.mdsal.binding.model.util.Types.STRING;
11 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTATION_FIELD
13 import com.google.common.base.MoreObjects
14 import java.util.ArrayList
15 import java.util.Collection
16 import java.util.Collections
17 import java.util.Comparator
20 import org.opendaylight.mdsal.binding.model.api.AnnotationType
21 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty
22 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
23 import org.opendaylight.mdsal.binding.model.api.GeneratedType
24 import org.opendaylight.mdsal.binding.model.api.Type
25 import org.opendaylight.mdsal.binding.model.util.BindingTypes
26 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping
28 abstract class AbstractBuilderTemplate extends BaseTemplate {
29 static val Comparator<GeneratedProperty> KEY_PROPS_COMPARATOR = [ p1, p2 | return p1.name.compareTo(p2.name) ]
32 * Generated property is set if among methods is found one with the name GET_AUGMENTATION_METHOD_NAME.
34 protected val Type augmentType
37 * Set of class attributes (fields) which are derived from the getter methods names.
39 protected val Set<GeneratedProperty> properties
42 * GeneratedType for key type, null if this type does not have a key.
44 protected val Type keyType
46 protected val GeneratedType targetType;
48 new(AbstractJavaGeneratedType javaType, GeneratedType type, GeneratedType targetType,
49 Set<GeneratedProperty> properties, Type augmentType, Type keyType) {
51 this.targetType = targetType
52 this.properties = properties
53 this.augmentType = augmentType
54 this.keyType = keyType
57 new(GeneratedType type, GeneratedType targetType, Set<GeneratedProperty> properties, Type augmentType,
60 this.targetType = targetType
61 this.properties = properties
62 this.augmentType = augmentType
63 this.keyType = keyType
67 * Template method which generates class attributes.
69 * @param makeFinal value which specify whether field is|isn't final
70 * @return string with class attributes and their types
72 def protected final generateFields(boolean makeFinal) '''
73 «IF properties !== null»
75 private«IF makeFinal» final«ENDIF» «f.returnType.importedName» «f.fieldName»;
79 private«IF makeFinal» final«ENDIF» «keyType.importedName» key;
83 def protected final generateAugmentField() {
84 val augmentTypeRef = augmentType.importedName
86 «JU_MAP.importedName»<«CLASS.importedName»<? extends «augmentTypeRef»>, «augmentTypeRef»> «AUGMENTATION_FIELD» = «Collections.importedName».emptyMap();
90 override generateToString(Collection<GeneratedProperty> properties) '''
91 «IF properties !== null»
92 @«OVERRIDE.importedName»
93 public «STRING.importedName» toString() {
94 final «MoreObjects.importedName».ToStringHelper helper = «MoreObjects.importedName».toStringHelper("«targetType.name»");
95 «FOR property : properties»
96 «CODEHELPERS.importedName».appendValue(helper, "«property.fieldName»", «property.fieldName»);
98 «IF augmentType !== null»
99 «CODEHELPERS.importedName».appendValue(helper, "«AUGMENTATION_FIELD»", augmentations().values());
101 return helper.toString();
107 * Template method which generate getter methods for IMPL class.
109 * @return string with getter methods
111 def final generateGetters(boolean addOverride) '''
112 «IF keyType !== null»
113 «IF addOverride»@«OVERRIDE.importedName»«ENDIF»
114 public «keyType.importedName» «BindingMapping.IDENTIFIABLE_KEY_NAME»() {
119 «IF !properties.empty»
120 «FOR field : properties SEPARATOR '\n'»
121 «IF addOverride»@«OVERRIDE.importedName»«ENDIF»
127 def protected final CharSequence generateCopyConstructor(Type fromType, Type implType) '''
128 «type.name»(«fromType.importedName» base) {
129 «IF augmentType !== null»
130 «generateCopyAugmentation(implType)»
132 «val allProps = new ArrayList(properties)»
133 «val isList = implementsIfc(targetType, BindingTypes.identifiable(targetType))»
134 «IF isList && keyType !== null»
135 «val keyProps = new ArrayList((keyType as GeneratedTransferObject).properties)»
136 «keyProps.sort(KEY_PROPS_COMPARATOR)»
137 «FOR field : keyProps»
138 «removeProperty(allProps, field.name)»
140 «generateCopyKeys(keyProps)»
142 «FOR field : allProps»
143 this.«field.fieldName» = base.«field.getterMethodName»();
148 def protected final CharSequence generateDeprecatedAnnotation(List<AnnotationType> annotations) {
149 var AnnotationType found = annotations.findDeprecatedAnnotation
150 if (found === null) {
153 return generateDeprecatedAnnotation(found)
156 def protected abstract CharSequence generateCopyKeys(List<GeneratedProperty> keyProps)
158 def protected abstract CharSequence generateCopyAugmentation(Type implType)
160 def protected abstract CharSequence generateDeprecatedAnnotation(AnnotationType ann)
162 private def boolean implementsIfc(GeneratedType type, Type impl) {
163 for (Type ifc : type.implements) {
164 if (ifc.equals(impl)) {
171 private def void removeProperty(Collection<GeneratedProperty> props, String name) {
172 var GeneratedProperty toRemove = null
174 if (p.name.equals(name)) {
178 if (toRemove !== null) {
179 props.remove(toRemove);
183 private static def findDeprecatedAnnotation(List<AnnotationType> annotations) {
184 if (annotations !== null) {
185 for (annotation : annotations) {
186 if (DEPRECATED.equals(annotation.identifier)) {