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.spec.naming.BindingMapping.AUGMENTATION_FIELD
12 import com.google.common.base.MoreObjects
13 import java.util.ArrayList
14 import java.util.Collection
15 import java.util.Collections
16 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.JavaTypeName
25 import org.opendaylight.mdsal.binding.model.api.Type
26 import org.opendaylight.mdsal.binding.model.util.Types
27 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping
28 import org.opendaylight.yangtools.yang.binding.CodeHelpers
29 import org.opendaylight.yangtools.yang.binding.Identifiable
31 abstract class AbstractBuilderTemplate extends BaseTemplate {
32 static val Comparator<GeneratedProperty> KEY_PROPS_COMPARATOR = [ p1, p2 | return p1.name.compareTo(p2.name) ]
34 protected static val DEPRECATED = JavaTypeName.create(Deprecated);
37 * Generated property is set if among methods is found one with the name GET_AUGMENTATION_METHOD_NAME.
39 protected val Type augmentType
42 * Set of class attributes (fields) which are derived from the getter methods names.
44 protected val Set<GeneratedProperty> properties
47 * GeneratedType for key type, null if this type does not have a key.
49 protected val Type keyType
51 protected val GeneratedType targetType;
53 new(AbstractJavaGeneratedType javaType, GeneratedType type, GeneratedType targetType,
54 Set<GeneratedProperty> properties, Type augmentType, Type keyType) {
56 this.targetType = targetType
57 this.properties = properties
58 this.augmentType = augmentType
59 this.keyType = keyType
62 new(GeneratedType type, GeneratedType targetType, Set<GeneratedProperty> properties, Type augmentType,
65 this.targetType = targetType
66 this.properties = properties
67 this.augmentType = augmentType
68 this.keyType = keyType
72 * Template method which generates class attributes.
74 * @param makeFinal value which specify whether field is|isn't final
75 * @return string with class attributes and their types
77 def protected final generateFields(boolean makeFinal) '''
78 «IF properties !== null»
80 private«IF makeFinal» final«ENDIF» «f.returnType.importedName» «f.fieldName»;
84 private«IF makeFinal» final«ENDIF» «keyType.importedName» key;
88 def protected final generateAugmentField() {
89 val augmentTypeRef = augmentType.importedName
91 «Map.importedName»<«Class.importedName»<? extends «augmentTypeRef»>, «augmentTypeRef»> «AUGMENTATION_FIELD» = «Collections.importedName».emptyMap();
95 override generateToString(Collection<GeneratedProperty> properties) '''
96 «IF properties !== null»
97 @«Override.importedName»
98 public «String.importedName» toString() {
99 final «MoreObjects.importedName».ToStringHelper helper = «MoreObjects.importedName».toStringHelper("«targetType.name»");
100 «FOR property : properties»
101 «CodeHelpers.importedName».appendValue(helper, "«property.fieldName»", «property.fieldName»);
103 «IF augmentType !== null»
104 «CodeHelpers.importedName».appendValue(helper, "«AUGMENTATION_FIELD»", augmentations().values());
106 return helper.toString();
112 * Template method which generate getter methods for IMPL class.
114 * @return string with getter methods
116 def final generateGetters(boolean addOverride) '''
117 «IF keyType !== null»
118 «IF addOverride»@«Override.importedName»«ENDIF»
119 public «keyType.importedName» «BindingMapping.IDENTIFIABLE_KEY_NAME»() {
124 «IF !properties.empty»
125 «FOR field : properties SEPARATOR '\n'»
126 «IF addOverride»@«Override.importedName»«ENDIF»
132 def protected final CharSequence generateCopyConstructor(Type fromType, Type implType) '''
133 «type.name»(«fromType.importedName» base) {
134 «IF augmentType !== null»
135 «generateCopyAugmentation(implType)»
137 «val allProps = new ArrayList(properties)»
138 «val isList = implementsIfc(targetType, Types.parameterizedTypeFor(Types.typeForClass(Identifiable), targetType))»
139 «IF isList && keyType !== null»
140 «val keyProps = new ArrayList((keyType as GeneratedTransferObject).properties)»
141 «keyProps.sort(KEY_PROPS_COMPARATOR)»
142 «FOR field : keyProps»
143 «removeProperty(allProps, field.name)»
145 «generateCopyKeys(keyProps)»
147 «FOR field : allProps»
148 this.«field.fieldName» = base.«field.getterMethodName»();
153 def protected final CharSequence generateDeprecatedAnnotation(List<AnnotationType> annotations) {
154 var AnnotationType found = annotations.findDeprecatedAnnotation
155 if (found === null) {
158 return generateDeprecatedAnnotation(found)
161 def protected abstract CharSequence generateCopyKeys(List<GeneratedProperty> keyProps)
163 def protected abstract CharSequence generateCopyAugmentation(Type implType)
165 def protected abstract CharSequence generateDeprecatedAnnotation(AnnotationType ann)
167 private def boolean implementsIfc(GeneratedType type, Type impl) {
168 for (Type ifc : type.implements) {
169 if (ifc.equals(impl)) {
176 private def void removeProperty(Collection<GeneratedProperty> props, String name) {
177 var GeneratedProperty toRemove = null
179 if (p.name.equals(name)) {
183 if (toRemove !== null) {
184 props.remove(toRemove);
188 private static def findDeprecatedAnnotation(List<AnnotationType> annotations) {
189 if (annotations !== null) {
190 for (annotation : annotations) {
191 if (DEPRECATED.equals(annotation.identifier)) {