2 * Copyright (c) 2016 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
10 @import org.opendaylight.mdsal.binding.javav2.java.api.generator.util.TextTemplateUtil.fieldName
11 @import org.opendaylight.mdsal.binding.javav2.java.api.generator.util.TextTemplateUtil.formatDataForJavaDocBuilder
12 @import org.opendaylight.mdsal.binding.javav2.java.api.generator.util.TextTemplateUtil.getSimpleNameForBuilder
13 @import org.opendaylight.mdsal.binding.javav2.java.api.generator.util.TextTemplateUtil.getterMethodName
14 @import org.opendaylight.mdsal.binding.javav2.java.api.generator.util.TextTemplateUtil.getRestrictions
15 @import org.opendaylight.mdsal.binding.javav2.java.api.generator.util.TextTemplateUtil.propertyNameFromGetter
16 @import org.opendaylight.mdsal.binding.javav2.java.api.generator.util.TextTemplateUtil.toFirstUpper
17 @import org.opendaylight.mdsal.binding.javav2.java.api.generator.util.TextTemplateUtil.wrapToDocumentation
18 @import org.opendaylight.mdsal.binding.javav2.java.api.generator.renderers.BuilderRenderer.getAllIfcs
19 @import org.opendaylight.mdsal.binding.javav2.java.api.generator.renderers.BuilderRenderer.hasImplementsFromUses
20 @import org.opendaylight.mdsal.binding.javav2.java.api.generator.renderers.BuilderRenderer.toListOfNames
21 @import org.opendaylight.mdsal.binding.javav2.java.api.generator.rangeGenerators.AbstractRangeGenerator
22 @import org.opendaylight.mdsal.binding.javav2.java.api.generator.rangeGenerators.LengthGenerator
23 @import org.opendaylight.mdsal.binding.javav2.model.api.ConcreteType
24 @import org.opendaylight.mdsal.binding.javav2.model.api.GeneratedType
25 @import org.opendaylight.mdsal.binding.javav2.model.api.GeneratedTransferObject
26 @import org.opendaylight.mdsal.binding.javav2.model.api.GeneratedProperty
27 @import org.opendaylight.mdsal.binding.javav2.model.api.Type
28 @import org.opendaylight.yangtools.concepts.Builder
30 @(genType: GeneratedType, properties: Set[GeneratedProperty], importedNames: Map[String, String],
31 ImportedNamesWithProperties: Map[GeneratedProperty, String], augmentField: GeneratedProperty, copyConstructorHelper: String,
32 getterMethods: List[String], parentTypeForBuilderName: String, childTreeNode: Boolean, instantiable: Boolean)
33 @if(genType != null) {
34 @{wrapToDocumentation(formatDataForJavaDocBuilder(importedNames.get("genType")))}
35 public class @{genType.getName}Builder implements @{getSimpleNameForBuilder} <@{importedNames.get("genType")}> {
37 @generateFields(false)
39 @generateAugmentField(false)
41 @generateConstructorsFromIfcs()
43 @generateCopyConstructor(false)
45 @generateMethodFieldsFrom()
47 @generateGetters(false)
52 public @{genType.getName} build() {
53 return new @{genType.getName}Impl(this);
56 private static final class @{genType.getName}Impl implements @{genType.getName} {
58 @implementedInterfaceGetter()
63 @generateAugmentField(true)
65 @generateCopyConstructor(true)
67 @generateGetters(true)
75 @generateImplementedMethods()
81 * Template method which generates class attributes.
83 * @param isFinal value which specify whether field is|isn't final
84 * @param genType is genType
85 * @return string with class attributes and their types
87 @generateFields(isFinal: Boolean) = {
88 @if(ImportedNamesWithProperties != null) {
89 @for((key, value) <- ImportedNamesWithProperties) {
90 private @if(isFinal) { final}
91 @{value} @{fieldName(key)};
97 * Template method which generates class attributes.
99 * @param boolean value which specify whether field is|isn't final
100 * @return string with class attributes and their types
102 @generateAugmentField(isPrivate: Boolean) = {
103 @if(augmentField != null) {
104 @if(isPrivate) {private }
105 @{importedNames.get("map")}<@{importedNames.get("class")}<? extends @{importedNames.get("augmentFieldReturnType")}>,
106 @{importedNames.get("augmentFieldReturnType")}> @{augmentField.getName} = @{importedNames.get("collections")}.emptyMap();
110 @implementedInterfaceGetter() = {
112 public @{importedNames.get("class")}<@{importedNames.get("genType")}> implementedInterface() {
113 return @{importedNames.get("genType")}.class;
118 * Generate default constructor and constructor for every implemented interface from uses statements.
120 @generateConstructorsFromIfcs() = {
121 public @{genType.getName}Builder() {
123 @if(genType.isInstanceOf[GeneratedType] && !genType.isInstanceOf[GeneratedTransferObject]) {
124 @for(impl <- genType.asInstanceOf[GeneratedType].getImplements) {
125 @generateConstructorFromIfc(impl)
130 @generateMethodFieldsFrom() = {
131 @if(genType.isInstanceOf[GeneratedType] && !genType.isInstanceOf[GeneratedTransferObject]) {
132 @if(hasImplementsFromUses(genType.asInstanceOf[GeneratedType])) {
134 *Set fields from given grouping argument. Valid argument is instance of one of following types:
136 @for(impl <- getAllIfcs(genType.asInstanceOf[GeneratedType])) {
137 * <li>@{impl.getFullyQualifiedName}</li>
141 * @@param arg grouping object
142 * @@throws IllegalArgumentException if given argument is none of valid types
145 public void fieldsFrom(@{importedNames.get("treeNode")} arg) {
146 boolean isValidArg = false;
147 @for(impl <- getAllIfcs(genType.asInstanceOf[GeneratedType])) {
148 @if(impl.isInstanceOf[GeneratedType] && !impl.asInstanceOf[GeneratedType].getMethodDefinitions.isEmpty) {
149 if (arg instanceof @{impl.asInstanceOf[GeneratedType].getFullyQualifiedName}) {
150 @if(!impl.isInstanceOf[GeneratedTransferObject]) {
151 @for(getter <- genType.asInstanceOf[GeneratedType].getMethodDefinitions) {
152 this._@{propertyNameFromGetter(getter)} = ((@{impl.asInstanceOf[GeneratedType].getFullyQualifiedName})arg).@{getter.getName}();
160 throw new IllegalArgumentException(
161 "expected one of: @{toListOfNames(getAllIfcs(genType.asInstanceOf[GeneratedType]))} \n" +
171 * Generate constructor with argument of given type.
173 @generateConstructorFromIfc(impl: Type) = {
174 @if(impl.isInstanceOf[GeneratedType]) {
175 @if(!impl.asInstanceOf[GeneratedType].getMethodDefinitions.isEmpty) {
176 public @{genType.getName}Builder(
177 @{impl.getFullyQualifiedName} arg) {
178 @{printConstructorPropertySetter(impl)}
181 @for(implTypeImplement <- impl.asInstanceOf[GeneratedType].getImplements) {
182 @generateConstructorFromIfc(implTypeImplement)
187 @printConstructorPropertySetter(implementedIfc: Type) = {
188 @if(implementedIfc.isInstanceOf[GeneratedType] && !implementedIfc.isInstanceOf[GeneratedTransferObject]) {
189 @for(getter <- implementedIfc.asInstanceOf[GeneratedType].getMethodDefinitions) {
190 this._@{propertyNameFromGetter(getter)} = arg.@{getter.getName}();
192 @for(impl <- implementedIfc.asInstanceOf[GeneratedType].getImplements) {
193 @{printConstructorPropertySetter(impl)}
198 @generateImplType() = {
199 @defining(genType.getName + "Impl") {typeImpl => @typeImpl}
202 @generateBuilderType() = {
203 @defining(genType.getName + "Builder") {typeBuilder => @typeBuilder}
206 @generateInnerBuilderType() = {
207 @defining("(" + genType.getName + "Builder base)") {typeInnerBuilder => @typeInnerBuilder}
210 @generateInnerType() = {
211 @defining("(" + genType.getName + " base)") {innerType => @innerType}
214 @generateCopyConstructor(impl: Boolean) = {
215 @if(impl) {private} else {public}
216 @if(impl) {@generateImplType()} else {@generateBuilderType()}
217 @if(impl) {@generateInnerBuilderType()} else {@generateInnerType()}
219 @{copyConstructorHelper}
220 @if(augmentField != null) {
222 switch (base.@{augmentField.getName}.size()) {
224 this.@{augmentField.getName} = @{importedNames.get("collections")}.emptyMap();
227 final @{importedNames.get("map")}.Entry<@{importedNames.get("class")}<? extends @{importedNames.get("augmentFieldReturnType")}>, @{importedNames.get("augmentFieldReturnType")}> e = base.@{augmentField.getName}.entrySet().iterator().next();
228 this.@{augmentField.getName} = @{importedNames.get("collections")}.<@{importedNames.get("class")}<? extends @{importedNames.get("augmentFieldReturnType")}>, @{importedNames.get("augmentFieldReturnType")}> singletonMap(e.getKey(), e.getValue());
231 this.@{augmentField.getName} = new @{importedNames.get("hashMap")}<>(base.@{augmentField.getName});
234 if (base instanceof @{genType.getName}Impl) {
235 @{genType.getName}Impl impl = (@{genType.getName}Impl) base;
236 if (!impl.@{augmentField.getName}.isEmpty()) {
237 this.@{augmentField.getName} = new @{importedNames.get("hashMap")}<>(impl.@{augmentField.getName});
239 } @{"else"} if (base instanceof @{importedNames.get("augmentationHolder")}) {
240 @@SuppressWarnings("unchecked")
241 @{importedNames.get("augmentationHolder")}<@{importedNames.get("genType")}> casted =(@{importedNames.get("augmentationHolder")}<@{importedNames.get("genType")}>) base;
242 if (!casted.augmentations().isEmpty()) {
243 this.@{augmentField.getName} = new @{importedNames.get("hashMap")}<>(casted.augmentations());
252 @generateSetters() = {
253 @for(field <- properties) {
254 @if(!field.getReturnType.isInstanceOf[GeneratedType] && getRestrictions(field.getReturnType) != null) {
255 @if(getRestrictions(field.getReturnType).getRangeConstraints != null && !getRestrictions(field.getReturnType).getRangeConstraints.isEmpty) {
256 @{AbstractRangeGenerator.forType(field.getReturnType).generateRangeChecker(toFirstUpper(field.getName),
257 getRestrictions(field.getReturnType).getRangeConstraints)}
259 @if(getRestrictions(field.getReturnType).getLengthConstraints != null && !getRestrictions(field.getReturnType).getLengthConstraints.isEmpty) {
260 @{LengthGenerator.generateLengthChecker(fieldName(field), field.getReturnType,
261 getRestrictions(field.getReturnType).getLengthConstraints)}
264 public @{genType.getName}Builder set@{toFirstUpper(field.getName)}(final @{toFirstUpper(field.getReturnType.getName)} value) {
265 @if(!field.getReturnType.isInstanceOf[GeneratedType] && getRestrictions(field.getReturnType) != null) {
267 @if(getRestrictions(field.getReturnType).getRangeConstraints != null && !getRestrictions(field.getReturnType).getRangeConstraints.isEmpty) {
268 @if(field.getReturnType.isInstanceOf[ConcreteType]) {
269 @{AbstractRangeGenerator.forType(field.getReturnType).generateRangeCheckerCall(toFirstUpper(field.getName), "value")}
271 @{AbstractRangeGenerator.forType(field.getReturnType).generateRangeCheckerCall(toFirstUpper(field.getName), "value.getValue()")}
274 @if(getRestrictions(field.getReturnType).getLengthConstraints != null && !getRestrictions(field.getReturnType).getLengthConstraints.isEmpty) {
275 @if(field.getReturnType.isInstanceOf[ConcreteType]) {
276 @{LengthGenerator.generateLengthCheckerCall(fieldName(field), "value")}
278 @{LengthGenerator.generateLengthCheckerCall(fieldName(field), "value.getValue()")}
283 this.@{fieldName(field)} = value;
287 @if(augmentField != null) {
288 public @{genType.getName}Builder add@{toFirstUpper(augmentField.getName)}(@{importedNames.get("class")}<? extends @{importedNames.get("augmentFieldReturnType")}> augmentationType, @{importedNames.get("augmentFieldReturnType")} augmentation) {
289 if (augmentation == null) {
290 return remove@{toFirstUpper(augmentField.getName)}(augmentationType);
293 if (!(this.@{augmentField.getName} instanceof @{importedNames.get("hashMap")})) {
294 this.@{augmentField.getName} = new @{importedNames.get("hashMap")}<>();
297 this.@{augmentField.getName}.put(augmentationType, augmentation);
301 public @{genType.getName}Builder remove@{toFirstUpper(augmentField.getName)}
302 (@{importedNames.get("class")}<? extends @{importedNames.get("augmentFieldReturnType")}> augmentationType) {
303 if (this.@{augmentField.getName} instanceof @{importedNames.get("hashMap")}) {
304 this.@{augmentField.getName}.remove(augmentationType);
311 @generateGetters(addOverride: Boolean) = {
312 @if(!getterMethods.isEmpty) {
313 @for(property <- getterMethods) {
314 @if(addOverride) {@@Override}
318 @if(augmentField != null) {
319 @@SuppressWarnings("unchecked")
320 @if(addOverride) {@@Override}
321 public <E extends @{importedNames.get("augmentation")}<? super @{genType.getName}>> E get@{toFirstUpper(augmentField.getName)}
322 (@{importedNames.get("class")}<E> augmentationType) {
323 if (augmentationType == null) {
324 throw new IllegalArgumentException("Augmentation Type reference cannot be NULL!");
326 return (E) @{augmentField.getName}.get(augmentationType);
331 @generateHashCode() = {
332 @if(!properties.isEmpty || augmentField != null) {
333 private int hash = 0;
334 private volatile boolean hashValid = false;
337 public int hashCode() {
342 final int prime = 31;
344 @for(property <- properties) {
345 @if(property.getReturnType.getName.contains("[")) {
346 result = prime * result + @{importedNames.get("arrays")}.hashCode(@{fieldName(property)});
348 result = prime * result + @{importedNames.get("objects")}.hashCode(@{fieldName(property)});
351 @if(augmentField != null) {
352 result = prime * result + @{importedNames.get("objects")}.hashCode(@{augmentField.getName});
362 @generateToString() = {
363 @if(properties != null) {
365 public @{importedNames.get("string")} toString() {
366 @{importedNames.get("string")} name = "@{genType.getName} [";
367 @{importedNames.get("stringBuilder")} builder = new @{importedNames.get("stringBuilder")}(name);
368 @for((property, index) <- properties.zipWithIndex) {
369 if (@{fieldName(property)} != null) {
370 builder.append("@{fieldName(property)}=");
371 @if(property.getReturnType.getName.contains("[")) {
372 builder.append(@{importedNames.get("arrays")}.toString(@{fieldName(property)}));
374 builder.append(@{fieldName(property)});
376 @if(properties.size() > 1 && index < properties.size()-1){
377 builder.append(", ");
381 @if(augmentField != null) {
382 @if(!properties.isEmpty()){
383 final int builderLength = builder.length();
384 final int builderAdditionalLength = builder.substring(name.length(), builderLength).length();
385 if (builderAdditionalLength > 2 && !builder.substring(builderLength - 2, builderLength).equals(", ")) {
386 builder.append(", ");
389 builder.append("@{augmentField.getName}=");
390 builder.append(@{augmentField.getName}.values());
391 return builder.append(']').toString();
393 @if(properties.isEmpty()){
394 return builder.append(']').toString();
396 return builder.append(']').toString();
403 @generateImplementedMethods() = {
406 public @{importedNames.get("item")}<@{parentTypeForBuilderName}> treeIdentifier() {
412 @if(augmentField != null) {
414 public @{importedNames.get("classInstMap")}<@{importedNames.get("augmentation")}<? super @{genType.getName}>>
422 @generateEquals() = {
423 @if(!properties.isEmpty || augmentField != null) {
425 public boolean equals(@{importedNames.get("object")} obj) {
429 if (!(obj instanceof @{importedNames.get("treeNode")})) {
432 @if(parentTypeForBuilderName != null) {
433 if (!(obj instanceof @{importedNames.get("instantiable")})) {
436 if (!@{importedNames.get("genType")}.class.equals(((@{importedNames.get("instantiable")})obj)
437 .implementedInterface())) {
441 @{importedNames.get("genType")} other = (@{importedNames.get("genType")})obj;
442 @for(property <- properties) {
443 @if(property.getReturnType.getName.contains("[")) {
444 if (!@{importedNames.get("arrays")}.equals(@{fieldName(property)}, other.@{getterMethodName(property)}()))
446 if (!@{importedNames.get("objects")}.equals(@{fieldName(property)}, other.@{getterMethodName(property)}()))
452 @if(augmentField != null) {
453 if (getClass() == obj.getClass()) {
454 // Simple case: we are comparing against self
455 @{genType.getName}Impl otherImpl = (@{genType.getName}Impl) obj;
456 if (!@{importedNames.get("objects")}.equals(@{augmentField.getName}, otherImpl.@{augmentField.getName})) {
460 // Hard case: compare our augments with presence there...
461 for (@{importedNames.get("map")}.Entry<@{importedNames.get("class")}<? extends @{importedNames.get("augmentFieldReturnType")}>, @{importedNames.get("augmentFieldReturnType")}> e : @{augmentField.getName}.entrySet()) {
462 if (!e.getValue().equals(other.getAugmentation(e.getKey()))) {
466 // .. and give the other one the chance to do the same
467 if (!obj.equals(this)) {