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)
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} {
57 @if(parentTypeForBuilderName != null) {
58 @implementedInterfaceGetter()
62 @generateAugmentField(true)
64 @generateCopyConstructor(true)
66 @generateGetters(true)
74 @generateImplementedMethods()
80 * Template method which generates class attributes.
82 * @param isFinal value which specify whether field is|isn't final
83 * @param genType is genType
84 * @return string with class attributes and their types
86 @generateFields(isFinal: Boolean) = {
87 @if(ImportedNamesWithProperties != null) {
88 @for((key, value) <- ImportedNamesWithProperties) {
89 private @if(isFinal) { final}
90 @{value} @{fieldName(key)};
96 * Template method which generates class attributes.
98 * @param boolean value which specify whether field is|isn't final
99 * @return string with class attributes and their types
101 @generateAugmentField(isPrivate: Boolean) = {
102 @if(augmentField != null) {
103 @if(isPrivate) {private }
104 @{importedNames.get("map")}<@{importedNames.get("class")}<? extends @{importedNames.get("augmentFieldReturnType")}>,
105 @{importedNames.get("augmentFieldReturnType")}> @{augmentField.getName} = @{importedNames.get("collections")}.emptyMap();
109 @implementedInterfaceGetter() = {
111 public @{importedNames.get("class")}<@{importedNames.get("genType")}> implementedInterface() {
112 return @{importedNames.get("genType")}.class;
117 * Generate default constructor and constructor for every implemented interface from uses statements.
119 @generateConstructorsFromIfcs() = {
120 public @{genType.getName}Builder() {
122 @if(genType.isInstanceOf[GeneratedType] && !genType.isInstanceOf[GeneratedTransferObject]) {
123 @for(impl <- genType.asInstanceOf[GeneratedType].getImplements) {
124 @generateConstructorFromIfc(impl)
129 @generateMethodFieldsFrom() = {
130 @if(genType.isInstanceOf[GeneratedType] && !genType.isInstanceOf[GeneratedTransferObject]) {
131 @if(hasImplementsFromUses(genType.asInstanceOf[GeneratedType])) {
133 *Set fields from given grouping argument. Valid argument is instance of one of following types:
135 @for(impl <- getAllIfcs(genType.asInstanceOf[GeneratedType])) {
136 * <li>@{impl.getFullyQualifiedName}</li>
140 * @@param arg grouping object
141 * @@throws IllegalArgumentException if given argument is none of valid types
144 public void fieldsFrom(@{importedNames.get("treeNode")} arg) {
145 boolean isValidArg = false;
146 @for(impl <- getAllIfcs(genType.asInstanceOf[GeneratedType])) {
147 @if(impl.isInstanceOf[GeneratedType] && !impl.asInstanceOf[GeneratedType].getMethodDefinitions.isEmpty) {
148 if (arg instanceof @{impl.asInstanceOf[GeneratedType].getFullyQualifiedName}) {
149 @if(!impl.isInstanceOf[GeneratedTransferObject]) {
150 @for(getter <- genType.asInstanceOf[GeneratedType].getMethodDefinitions) {
151 this._@{propertyNameFromGetter(getter)} = ((@{impl.asInstanceOf[GeneratedType].getFullyQualifiedName})arg).@{getter.getName}();
159 throw new IllegalArgumentException(
160 "expected one of: @{toListOfNames(getAllIfcs(genType.asInstanceOf[GeneratedType]))} \n" +
170 * Generate constructor with argument of given type.
172 @generateConstructorFromIfc(impl: Type) = {
173 @if(impl.isInstanceOf[GeneratedType]) {
174 @if(!impl.asInstanceOf[GeneratedType].getMethodDefinitions.isEmpty) {
175 public @{genType.getName}Builder(
176 @{impl.getFullyQualifiedName} arg) {
177 @{printConstructorPropertySetter(impl)}
180 @for(implTypeImplement <- impl.asInstanceOf[GeneratedType].getImplements) {
181 @generateConstructorFromIfc(implTypeImplement)
186 @printConstructorPropertySetter(implementedIfc: Type) = {
187 @if(implementedIfc.isInstanceOf[GeneratedType] && !implementedIfc.isInstanceOf[GeneratedTransferObject]) {
188 @for(getter <- implementedIfc.asInstanceOf[GeneratedType].getMethodDefinitions) {
189 this._@{propertyNameFromGetter(getter)} = arg.@{getter.getName}();
191 @for(impl <- implementedIfc.asInstanceOf[GeneratedType].getImplements) {
192 @{printConstructorPropertySetter(impl)}
197 @generateImplType() = {
198 @defining(genType.getName + "Impl") {typeImpl => @typeImpl}
201 @generateBuilderType() = {
202 @defining(genType.getName + "Builder") {typeBuilder => @typeBuilder}
205 @generateInnerBuilderType() = {
206 @defining("(" + genType.getName + "Builder base)") {typeInnerBuilder => @typeInnerBuilder}
209 @generateInnerType() = {
210 @defining("(" + genType.getName + " base)") {innerType => @innerType}
213 @generateCopyConstructor(impl: Boolean) = {
214 @if(impl) {private} else {public}
215 @if(impl) {@generateImplType()} else {@generateBuilderType()}
216 @if(impl) {@generateInnerBuilderType()} else {@generateInnerType()}
218 @{copyConstructorHelper}
219 @if(augmentField != null) {
221 switch (base.@{augmentField.getName}.size()) {
223 this.@{augmentField.getName} = @{importedNames.get("collections")}.emptyMap();
226 final @{importedNames.get("map")}.Entry<@{importedNames.get("class")}<? extends @{importedNames.get("augmentFieldReturnType")}>, @{importedNames.get("augmentFieldReturnType")}> e = base.@{augmentField.getName}.entrySet().iterator().next();
227 this.@{augmentField.getName} = @{importedNames.get("collections")}.<@{importedNames.get("class")}<? extends @{importedNames.get("augmentFieldReturnType")}>, @{importedNames.get("augmentFieldReturnType")}> singletonMap(e.getKey(), e.getValue());
230 this.@{augmentField.getName} = new @{importedNames.get("hashMap")}<>(base.@{augmentField.getName});
233 if (base instanceof @{genType.getName}Impl) {
234 @{genType.getName}Impl impl = (@{genType.getName}Impl) base;
235 if (!impl.@{augmentField.getName}.isEmpty()) {
236 this.@{augmentField.getName} = new @{importedNames.get("hashMap")}<>(impl.@{augmentField.getName});
238 } @{"else"} if (base instanceof @{importedNames.get("augmentationHolder")}) {
239 @@SuppressWarnings("unchecked")
240 @{importedNames.get("augmentationHolder")}<@{importedNames.get("genType")}> casted =(@{importedNames.get("augmentationHolder")}<@{importedNames.get("genType")}>) base;
241 if (!casted.augmentations().isEmpty()) {
242 this.@{augmentField.getName} = new @{importedNames.get("hashMap")}<>(casted.augmentations());
251 @generateSetters() = {
252 @for(field <- properties) {
253 @if(!field.getReturnType.isInstanceOf[GeneratedType] && getRestrictions(field.getReturnType) != null) {
254 @if(getRestrictions(field.getReturnType).getRangeConstraints != null && !getRestrictions(field.getReturnType).getRangeConstraints.isEmpty) {
255 @{AbstractRangeGenerator.forType(field.getReturnType).generateRangeChecker(toFirstUpper(field.getName),
256 getRestrictions(field.getReturnType).getRangeConstraints)}
258 @if(getRestrictions(field.getReturnType).getLengthConstraints != null && !getRestrictions(field.getReturnType).getLengthConstraints.isEmpty) {
259 @{LengthGenerator.generateLengthChecker(fieldName(field), field.getReturnType,
260 getRestrictions(field.getReturnType).getLengthConstraints)}
263 public @{genType.getName}Builder set@{toFirstUpper(field.getName)}(final @{toFirstUpper(field.getReturnType.getName)} value) {
264 @if(!field.getReturnType.isInstanceOf[GeneratedType] && getRestrictions(field.getReturnType) != null) {
266 @if(getRestrictions(field.getReturnType).getRangeConstraints != null && !getRestrictions(field.getReturnType).getRangeConstraints.isEmpty) {
267 @if(field.getReturnType.isInstanceOf[ConcreteType]) {
268 @{AbstractRangeGenerator.forType(field.getReturnType).generateRangeCheckerCall(toFirstUpper(field.getName), "value")}
270 @{AbstractRangeGenerator.forType(field.getReturnType).generateRangeCheckerCall(toFirstUpper(field.getName), "value.getValue()")}
273 @if(getRestrictions(field.getReturnType).getLengthConstraints != null && !getRestrictions(field.getReturnType).getLengthConstraints.isEmpty) {
274 @if(field.getReturnType.isInstanceOf[ConcreteType]) {
275 @{LengthGenerator.generateLengthCheckerCall(fieldName(field), "value")}
277 @{LengthGenerator.generateLengthCheckerCall(fieldName(field), "value.getValue()")}
282 this.@{fieldName(field)} = value;
286 @if(augmentField != null) {
287 public @{genType.getName}Builder add@{toFirstUpper(augmentField.getName)}(@{importedNames.get("class")}<? extends @{importedNames.get("augmentFieldReturnType")}> augmentationType, @{importedNames.get("augmentFieldReturnType")} augmentation) {
288 if (augmentation == null) {
289 return remove@{toFirstUpper(augmentField.getName)}(augmentationType);
292 if (!(this.@{augmentField.getName} instanceof @{importedNames.get("hashMap")})) {
293 this.@{augmentField.getName} = new @{importedNames.get("hashMap")}<>();
296 this.@{augmentField.getName}.put(augmentationType, augmentation);
300 public @{genType.getName}Builder remove@{toFirstUpper(augmentField.getName)}
301 (@{importedNames.get("class")}<? extends @{importedNames.get("augmentFieldReturnType")}> augmentationType) {
302 if (this.@{augmentField.getName} instanceof @{importedNames.get("hashMap")}) {
303 this.@{augmentField.getName}.remove(augmentationType);
310 @generateGetters(addOverride: Boolean) = {
311 @if(!getterMethods.isEmpty) {
312 @for(property <- getterMethods) {
313 @if(addOverride) {@@Override}
317 @if(augmentField != null) {
318 @@SuppressWarnings("unchecked")
319 @if(addOverride) {@@Override}
320 public <E extends @{importedNames.get("augmentation")}<? super @{genType.getName}>> E get@{toFirstUpper(augmentField.getName)}
321 (@{importedNames.get("class")}<E> augmentationType) {
322 if (augmentationType == null) {
323 throw new IllegalArgumentException("Augmentation Type reference cannot be NULL!");
325 return (E) @{augmentField.getName}.get(augmentationType);
330 @generateHashCode() = {
331 @if(!properties.isEmpty || augmentField != null) {
332 private int hash = 0;
333 private volatile boolean hashValid = false;
336 public int hashCode() {
341 final int prime = 31;
343 @for(property <- properties) {
344 @if(property.getReturnType.getName.contains("[")) {
345 result = prime * result + @{importedNames.get("arrays")}.hashCode(@{fieldName(property)});
347 result = prime * result + @{importedNames.get("objects")}.hashCode(@{fieldName(property)});
350 @if(augmentField != null) {
351 result = prime * result + @{importedNames.get("objects")}.hashCode(@{augmentField.getName});
361 @generateToString() = {
362 @if(properties != null) {
364 public @{importedNames.get("string")} toString() {
365 @{importedNames.get("string")} name = "@{genType.getName} [";
366 @{importedNames.get("stringBuilder")} builder = new @{importedNames.get("stringBuilder")}(name);
367 @for((property, index) <- properties.zipWithIndex) {
368 if (@{fieldName(property)} != null) {
369 builder.append("@{fieldName(property)}=");
370 @if(property.getReturnType.getName.contains("[")) {
371 builder.append(@{importedNames.get("arrays")}.toString(@{fieldName(property)}));
373 builder.append(@{fieldName(property)});
375 @if(properties.size() > 1 && index < properties.size()-1){
376 builder.append(", ");
380 @if(augmentField != null) {
381 @if(!properties.isEmpty()){
382 final int builderLength = builder.length();
383 final int builderAdditionalLength = builder.substring(name.length(), builderLength).length();
384 if (builderAdditionalLength > 2 && !builder.substring(builderLength - 2, builderLength).equals(", ")) {
385 builder.append(", ");
388 builder.append("@{augmentField.getName}=");
389 builder.append(@{augmentField.getName}.values());
390 return builder.append(']').toString();
392 @if(properties.isEmpty()){
393 return builder.append(']').toString();
395 return builder.append(']').toString();
402 @generateImplementedMethods() = {
403 @if(parentTypeForBuilderName != null) {
405 public @{importedNames.get("item")}<@{parentTypeForBuilderName}> treeIdentifier() {
411 @if(augmentField != null) {
413 public @{importedNames.get("classInstMap")}<@{importedNames.get("augmentation")}<? super @{genType.getName}>>
421 @generateEquals() = {
422 @if(!properties.isEmpty || augmentField != null) {
424 public boolean equals(@{importedNames.get("object")} obj) {
428 if (!(obj instanceof @{importedNames.get("treeNode")})) {
431 @if(parentTypeForBuilderName != null) {
432 if (!(obj instanceof @{importedNames.get("instantiable")})) {
435 if (!@{importedNames.get("genType")}.class.equals(((@{importedNames.get("instantiable")})obj)
436 .implementedInterface())) {
440 @{importedNames.get("genType")} other = (@{importedNames.get("genType")})obj;
441 @for(property <- properties) {
442 @if(property.getReturnType.getName.contains("[")) {
443 if (!@{importedNames.get("arrays")}.equals(@{fieldName(property)}, other.@{getterMethodName(property)}()))
445 if (!@{importedNames.get("objects")}.equals(@{fieldName(property)}, other.@{getterMethodName(property)}()))
451 @if(augmentField != null) {
452 if (getClass() == obj.getClass()) {
453 // Simple case: we are comparing against self
454 @{genType.getName}Impl otherImpl = (@{genType.getName}Impl) obj;
455 if (!@{importedNames.get("objects")}.equals(@{augmentField.getName}, otherImpl.@{augmentField.getName})) {
459 // Hard case: compare our augments with presence there...
460 for (@{importedNames.get("map")}.Entry<@{importedNames.get("class")}<? extends @{importedNames.get("augmentFieldReturnType")}>, @{importedNames.get("augmentFieldReturnType")}> e : @{augmentField.getName}.entrySet()) {
461 if (!e.getValue().equals(other.getAugmentation(e.getKey()))) {
465 // .. and give the other one the chance to do the same
466 if (!obj.equals(this)) {