2 * Copyright (c) 2014 Cisco Systems, Inc. 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 extension org.apache.commons.text.StringEscapeUtils.escapeJava
11 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTATION_FIELD
13 import com.google.common.collect.ImmutableList
14 import java.util.ArrayList
15 import java.util.Collection
16 import java.util.HashMap
17 import java.util.HashSet
21 import java.util.regex.Pattern
22 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty
23 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
24 import org.opendaylight.mdsal.binding.model.api.GeneratedType
25 import org.opendaylight.mdsal.binding.model.api.JavaTypeName
26 import org.opendaylight.mdsal.binding.model.api.ParameterizedType
27 import org.opendaylight.mdsal.binding.model.api.Type
28 import org.opendaylight.mdsal.binding.model.util.TypeConstants
29 import org.opendaylight.mdsal.binding.model.util.Types
30 import org.opendaylight.yangtools.concepts.Builder
31 import org.opendaylight.yangtools.yang.binding.CodeHelpers
32 import org.opendaylight.yangtools.yang.binding.DataObject
35 * Template for generating JAVA builder classes.
37 class BuilderTemplate extends AbstractBuilderTemplate {
39 * Constant used as suffix for builder name.
41 public static val BUILDER = "Builder";
44 * Constructs new instance of this class.
45 * @throws IllegalArgumentException if <code>genType</code> equals <code>null</code>
47 new(GeneratedType genType, GeneratedType targetType, Set<GeneratedProperty> properties, Type augmentType,
49 super(genType, targetType, properties, augmentType, keyType)
52 override isLocalInnerClass(JavaTypeName name) {
53 // Builders do not have inner types
58 * Template method which generates JAVA class body for builder class and for IMPL class.
60 * @return string with JAVA source code
63 «wrapToDocumentation(formatDataForJavaDoc(type))»
64 public class «type.name» implements «Builder.importedName»<«targetType.importedName»> {
66 «generateFields(false)»
68 «constantsDeclarations()»
70 «generateAugmentField(false)»
72 «generateConstructorsFromIfcs()»
74 «generateCopyConstructor(false, targetType, type.enclosedTypes.get(0))»
76 «generateMethodFieldsFrom()»
78 «generateGetters(false)»
82 @«Override.importedName»
83 public «targetType.name» build() {
84 return new «type.enclosedTypes.get(0).importedName»(this);
87 «new BuilderImplTemplate(this, type.enclosedTypes.get(0)).body»
92 * Generate default constructor and constructor for every implemented interface from uses statements.
94 def private generateConstructorsFromIfcs() '''
95 public «type.name»() {
97 «IF (!(targetType instanceof GeneratedTransferObject))»
98 «FOR impl : targetType.implements»
99 «generateConstructorFromIfc(impl)»
105 * Generate constructor with argument of given type.
107 def private Object generateConstructorFromIfc(Type impl) '''
108 «IF (impl instanceof GeneratedType)»
109 «IF !(impl.methodDefinitions.empty)»
110 public «type.name»(«impl.fullyQualifiedName» arg) {
111 «printConstructorPropertySetter(impl)»
114 «FOR implTypeImplement : impl.implements»
115 «generateConstructorFromIfc(implTypeImplement)»
120 def private Object printConstructorPropertySetter(Type implementedIfc) '''
121 «IF (implementedIfc instanceof GeneratedType && !(implementedIfc instanceof GeneratedTransferObject))»
122 «val ifc = implementedIfc as GeneratedType»
123 «FOR getter : ifc.methodDefinitions»
124 this._«getter.propertyNameFromGetter» = arg.«getter.name»();
126 «FOR impl : ifc.implements»
127 «printConstructorPropertySetter(impl)»
133 * Generate 'fieldsFrom' method to set builder properties based on type of given argument.
135 def private generateMethodFieldsFrom() '''
136 «IF (!(targetType instanceof GeneratedTransferObject))»
137 «IF targetType.hasImplementsFromUses»
138 «val List<Type> done = targetType.getBaseIfcs»
139 «generateMethodFieldsFromComment(targetType)»
140 public void fieldsFrom(«DataObject.importedName» arg) {
141 boolean isValidArg = false;
142 «FOR impl : targetType.getAllIfcs»
143 «generateIfCheck(impl, done)»
145 «CodeHelpers.importedName».validValue(isValidArg, arg, "«targetType.getAllIfcs.toListOfNames»");
151 def private generateMethodFieldsFromComment(GeneratedType type) '''
153 * Set fields from given grouping argument. Valid argument is instance of one of following types:
155 «FOR impl : type.getAllIfcs»
156 * <li>«impl.fullyQualifiedName»</li>
160 * @param arg grouping object
161 * @throws IllegalArgumentException if given argument is none of valid types
166 * Method is used to find out if given type implements any interface from uses.
168 def boolean hasImplementsFromUses(GeneratedType type) {
170 for (impl : type.getAllIfcs) {
171 if ((impl instanceof GeneratedType) && !((impl as GeneratedType).methodDefinitions.empty)) {
178 def private generateIfCheck(Type impl, List<Type> done) '''
179 «IF (impl instanceof GeneratedType) && !((impl as GeneratedType).methodDefinitions.empty)»
180 «val implType = impl as GeneratedType»
181 if (arg instanceof «implType.fullyQualifiedName») {
182 «printPropertySetter(implType)»
188 def private printPropertySetter(Type implementedIfc) '''
189 «IF (implementedIfc instanceof GeneratedType && !(implementedIfc instanceof GeneratedTransferObject))»
190 «val ifc = implementedIfc as GeneratedType»
191 «FOR getter : ifc.methodDefinitions»
192 this._«getter.propertyNameFromGetter» = ((«implementedIfc.fullyQualifiedName»)arg).«getter.name»();
197 private def List<Type> getBaseIfcs(GeneratedType type) {
198 val List<Type> baseIfcs = new ArrayList();
199 for (ifc : type.implements) {
200 if (ifc instanceof GeneratedType && !(ifc as GeneratedType).methodDefinitions.empty) {
207 private def Set<Type> getAllIfcs(Type type) {
208 val Set<Type> baseIfcs = new HashSet()
209 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
210 val ifc = type as GeneratedType
211 for (impl : ifc.implements) {
212 if (impl instanceof GeneratedType && !(impl as GeneratedType).methodDefinitions.empty) {
215 baseIfcs.addAll(impl.getAllIfcs)
221 private def List<String> toListOfNames(Collection<Type> types) {
222 val List<String> names = new ArrayList
224 names.add(type.fullyQualifiedName)
229 def private constantsDeclarations() '''
230 «FOR c : type.getConstantDefinitions»
231 «IF c.getName.startsWith(TypeConstants.PATTERN_CONSTANT_NAME)»
232 «val cValue = c.value as Map<String, String>»
233 «val String fieldSuffix = c.getName.substring(TypeConstants.PATTERN_CONSTANT_NAME.length)»
234 «IF cValue.size == 1»
235 private static final «Pattern.importedName» «Constants.MEMBER_PATTERN_LIST»«fieldSuffix» = «Pattern.importedName».compile("«cValue.keySet.get(0).escapeJava»");
236 private static final String «Constants.MEMBER_REGEX_LIST»«fieldSuffix» = "«cValue.values.get(0).escapeJava»";
238 private static final «Pattern.importedName»[] «Constants.MEMBER_PATTERN_LIST»«fieldSuffix» = «CodeHelpers.importedName».compilePatterns(«ImmutableList.importedName».of(
239 «FOR v : cValue.keySet SEPARATOR ", "»"«v.escapeJava»"«ENDFOR»));
240 private static final String[] «Constants.MEMBER_REGEX_LIST»«fieldSuffix» = { «
241 FOR v : cValue.values SEPARATOR ", "»"«v.escapeJava»"«ENDFOR» };
249 def private generateListSetter(GeneratedProperty field, Type actualType) '''
250 «val restrictions = restrictionsForSetter(actualType)»
251 «IF restrictions !== null»
252 «generateCheckers(field, restrictions, actualType)»
254 public «type.getName» set«field.getName.toFirstUpper»(final «field.returnType.importedName» values) {
255 «IF restrictions !== null»
256 if (values != null) {
257 for («actualType.getFullyQualifiedName» value : values) {
258 «checkArgument(field, restrictions, actualType, "value")»
262 this.«field.fieldName.toString» = values;
268 def private generateSetter(GeneratedProperty field, Type actualType) '''
269 «val restrictions = restrictionsForSetter(actualType)»
270 «IF restrictions !== null»
271 «generateCheckers(field, restrictions, actualType)»
274 public «type.getName» set«field.getName.toFirstUpper»(final «field.returnType.importedName» value) {
275 «IF restrictions !== null»
277 «checkArgument(field, restrictions, actualType, "value")»
280 this.«field.fieldName.toString» = value;
285 private def Type getActualType(ParameterizedType ptype) {
286 return ptype.getActualTypeArguments.get(0)
290 * Template method which generates setter methods
292 * @return string with the setter methods
294 def private generateSetters() '''
295 «IF keyType !== null»
296 public «type.getName» withKey(final «keyType.importedName» key) {
301 «FOR property : properties»
302 «IF property.returnType instanceof ParameterizedType && Types.isListType(property.returnType)»
303 «generateListSetter(property, getActualType(property.returnType as ParameterizedType))»
305 «generateSetter(property, property.returnType)»
309 «IF augmentType !== null»
310 public «type.name» add«AUGMENTATION_FIELD.toFirstUpper»(«Class.importedName»<? extends «augmentType.importedName»> augmentationType, «augmentType.importedName» augmentationValue) {
311 if (augmentationValue == null) {
312 return remove«AUGMENTATION_FIELD.toFirstUpper»(augmentationType);
315 if (!(this.«AUGMENTATION_FIELD» instanceof «HashMap.importedName»)) {
316 this.«AUGMENTATION_FIELD» = new «HashMap.importedName»<>();
319 this.«AUGMENTATION_FIELD».put(augmentationType, augmentationValue);
323 public «type.name» remove«AUGMENTATION_FIELD.toFirstUpper»(«Class.importedName»<? extends «augmentType.importedName»> augmentationType) {
324 if (this.«AUGMENTATION_FIELD» instanceof «HashMap.importedName») {
325 this.«AUGMENTATION_FIELD».remove(augmentationType);
332 private def createDescription(GeneratedType type) {
334 Class that builds {@link «type.importedName»} instances.
336 @see «type.importedName»
340 override protected String formatDataForJavaDoc(GeneratedType type) {
341 val typeDescription = createDescription(type)
344 «IF !typeDescription.nullOrEmpty»