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.GeneratedProperty
21 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
22 import org.opendaylight.mdsal.binding.model.api.GeneratedType
23 import org.opendaylight.mdsal.binding.model.api.Type
24 import org.opendaylight.mdsal.binding.model.util.Types
25 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping
26 import org.opendaylight.yangtools.yang.binding.CodeHelpers
27 import org.opendaylight.yangtools.yang.binding.Identifiable
29 abstract class AbstractBuilderTemplate extends BaseTemplate {
30 static val Comparator<GeneratedProperty> KEY_PROPS_COMPARATOR = [ p1, p2 | return p1.name.compareTo(p2.name) ]
33 * Generated property is set if among methods is found one with the name GET_AUGMENTATION_METHOD_NAME.
35 protected val Type augmentType
38 * Set of class attributes (fields) which are derived from the getter methods names.
40 protected val Set<GeneratedProperty> properties
43 * GeneratedType for key type, null if this type does not have a key.
45 protected val Type keyType
47 protected val GeneratedType targetType;
49 new(AbstractJavaGeneratedType javaType, GeneratedType type, GeneratedType targetType,
50 Set<GeneratedProperty> properties, Type augmentType, Type keyType) {
52 this.targetType = targetType
53 this.properties = properties
54 this.augmentType = augmentType
55 this.keyType = keyType
58 new(GeneratedType type, GeneratedType targetType, Set<GeneratedProperty> properties, Type augmentType,
61 this.targetType = targetType
62 this.properties = properties
63 this.augmentType = augmentType
64 this.keyType = keyType
68 * Template method which generates class attributes.
70 * @param makeFinal value which specify whether field is|isn't final
71 * @return string with class attributes and their types
73 def protected final generateFields(boolean makeFinal) '''
74 «IF properties !== null»
76 private«IF makeFinal» final«ENDIF» «f.returnType.importedName» «f.fieldName»;
80 private«IF makeFinal» final«ENDIF» «keyType.importedName» key;
84 def protected final generateAugmentField() {
85 val augmentTypeRef = augmentType.importedName
87 «Map.importedName»<«Class.importedName»<? extends «augmentTypeRef»>, «augmentTypeRef»> «AUGMENTATION_FIELD» = «Collections.importedName».emptyMap();
91 override generateToString(Collection<GeneratedProperty> properties) '''
92 «IF properties !== null»
93 @«Override.importedName»
94 public «String.importedName» toString() {
95 final «MoreObjects.importedName».ToStringHelper helper = «MoreObjects.importedName».toStringHelper("«targetType.name»");
96 «FOR property : properties»
97 «CodeHelpers.importedName».appendValue(helper, "«property.fieldName»", «property.fieldName»);
99 «IF augmentType !== null»
100 «CodeHelpers.importedName».appendValue(helper, "«AUGMENTATION_FIELD»", augmentations().values());
102 return helper.toString();
108 * Template method which generate getter methods for IMPL class.
110 * @return string with getter methods
112 def final generateGetters(boolean addOverride) '''
113 «IF keyType !== null»
114 «IF addOverride»@«Override.importedName»«ENDIF»
115 public «keyType.importedName» «BindingMapping.IDENTIFIABLE_KEY_NAME»() {
120 «IF !properties.empty»
121 «FOR field : properties SEPARATOR '\n'»
122 «IF addOverride»@«Override.importedName»«ENDIF»
128 def protected final CharSequence generateCopyConstructor(Type fromType, Type implType) '''
129 «type.name»(«fromType.importedName» base) {
130 «IF augmentType !== null»
131 «generateCopyAugmentation(implType)»
133 «val allProps = new ArrayList(properties)»
134 «val isList = implementsIfc(targetType, Types.parameterizedTypeFor(Types.typeForClass(Identifiable), targetType))»
135 «IF isList && keyType !== null»
136 «val keyProps = new ArrayList((keyType as GeneratedTransferObject).properties)»
137 «keyProps.sort(KEY_PROPS_COMPARATOR)»
138 «FOR field : keyProps»
139 «removeProperty(allProps, field.name)»
141 «generateCopyKeys(keyProps)»
143 «FOR field : allProps»
144 this.«field.fieldName» = base.«field.getterMethodName»();
149 def protected abstract CharSequence generateCopyKeys(List<GeneratedProperty> keyProps)
151 def protected abstract CharSequence generateCopyAugmentation(Type implType);
153 private def boolean implementsIfc(GeneratedType type, Type impl) {
154 for (Type ifc : type.implements) {
155 if (ifc.equals(impl)) {
162 private def void removeProperty(Collection<GeneratedProperty> props, String name) {
163 var GeneratedProperty toRemove = null
165 if (p.name.equals(name)) {
169 if (toRemove !== null) {
170 props.remove(toRemove);