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])
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()
62 @generateAugmentField(true)
64 @generateCopyConstructor(true)
66 @generateGetters(true)
78 * Template method which generates class attributes.
80 * @param isFinal value which specify whether field is|isn't final
81 * @param genType is genType
82 * @return string with class attributes and their types
84 @generateFields(isFinal: Boolean) = {
85 @if(ImportedNamesWithProperties != null) {
86 @for((key, value) <- ImportedNamesWithProperties) {
87 private @if(isFinal) { final}
88 @{value} @{fieldName(key)};
94 * Template method which generates class attributes.
96 * @param boolean value which specify whether field is|isn't final
97 * @return string with class attributes and their types
99 @generateAugmentField(isPrivate: Boolean) = {
100 @if(augmentField != null) {
101 @if(isPrivate) {private }
102 @{importedNames.get("map")}<@{importedNames.get("class")}<? extends @{importedNames.get("augmentFieldReturnType")}>,
103 @{importedNames.get("augmentFieldReturnType")}> @{augmentField.getName} = @{importedNames.get("collections")}.emptyMap();
107 @implementedInterfaceGetter() = {
108 public @{importedNames.get("class")}<@{importedNames.get("genType")}> getImplementedInterface() {
109 return @{importedNames.get("genType")}.class;
114 * Generate default constructor and constructor for every implemented interface from uses statements.
116 @generateConstructorsFromIfcs() = {
117 public @{genType.getName}Builder() {
119 @if(genType.isInstanceOf[GeneratedType] && !genType.isInstanceOf[GeneratedTransferObject]) {
120 @for(impl <- genType.asInstanceOf[GeneratedType].getImplements) {
121 @generateConstructorFromIfc(impl)
126 @generateMethodFieldsFrom() = {
127 @if(genType.isInstanceOf[GeneratedType] && !genType.isInstanceOf[GeneratedTransferObject]) {
128 @if(hasImplementsFromUses(genType.asInstanceOf[GeneratedType])) {
130 *Set fields from given grouping argument. Valid argument is instance of one of following types:
132 @for(impl <- getAllIfcs(genType.asInstanceOf[GeneratedType])) {
133 * <li>@{impl.getFullyQualifiedName}</li>
137 * @@param arg grouping object
138 * @@throws IllegalArgumentException if given argument is none of valid types
141 public void fieldsFrom(@{importedNames.get("treeNode")} arg) {
142 boolean isValidArg = false;
143 @for(impl <- getAllIfcs(genType.asInstanceOf[GeneratedType])) {
144 @if(impl.isInstanceOf[GeneratedType] && !impl.asInstanceOf[GeneratedType].getMethodDefinitions.isEmpty) {
145 if (arg instanceof @{impl.asInstanceOf[GeneratedType].getFullyQualifiedName}) {
146 @if(!impl.isInstanceOf[GeneratedTransferObject]) {
147 @for(getter <- genType.asInstanceOf[GeneratedType].getMethodDefinitions) {
148 this._@{propertyNameFromGetter(getter)} = ((@{impl.asInstanceOf[GeneratedType].getFullyQualifiedName})arg).@{getter.getName}();
156 throw new IllegalArgumentException(
157 "expected one of: @{toListOfNames(getAllIfcs(genType.asInstanceOf[GeneratedType]))} \n" +
167 * Generate constructor with argument of given type.
169 @generateConstructorFromIfc(impl: Type) = {
170 @if(impl.isInstanceOf[GeneratedType]) {
171 @if(!impl.asInstanceOf[GeneratedType].getMethodDefinitions.isEmpty) {
172 public @{genType.getName}Builder(
173 @{impl.getFullyQualifiedName} arg) {
174 @{printConstructorPropertySetter(impl)}
177 @for(implTypeImplement <- impl.asInstanceOf[GeneratedType].getImplements) {
178 @generateConstructorFromIfc(implTypeImplement)
183 @printConstructorPropertySetter(implementedIfc: Type) = {
184 @if(implementedIfc.isInstanceOf[GeneratedType] && !implementedIfc.isInstanceOf[GeneratedTransferObject]) {
185 @for(getter <- implementedIfc.asInstanceOf[GeneratedType].getMethodDefinitions) {
186 this._@{propertyNameFromGetter(getter)} = arg.@{getter.getName}();
188 @for(impl <- implementedIfc.asInstanceOf[GeneratedType].getImplements) {
189 @{printConstructorPropertySetter(impl)}
194 @generateCopyConstructor(impl: Boolean) = {
195 @if(impl) {private} else {public}
197 @if(impl) {Impl} else {Builder}
199 @if(impl) {Builder} base) {
200 @{copyConstructorHelper}
201 @if(augmentField != null) {
203 switch (base.@{augmentField.getName}.size()) {
205 this.@{augmentField.getName} = @{importedNames.get("collections")}.emptyMap();
208 final @{importedNames.get("map")}.Entry<@{importedNames.get("class")}<? extends @{importedNames.get("augmentFieldReturnType")}>, @{importedNames.get("augmentFieldReturnType")}> e = base.@{augmentField.getName}.entrySet().iterator().next();
209 this.@{augmentField.getName} = @{importedNames.get("collections")}.<@{importedNames.get("class")}<? extends @{importedNames.get("augmentFieldReturnType")}>, @{importedNames.get("augmentFieldReturnType")}> singletonMap(e.getKey(), e.getValue());
212 this.@{augmentField.getName} = new @{importedNames.get("hashMap")}<>(base.@{augmentField.getName});
215 if (base instanceof @{genType.getName}Impl) {
216 @{genType.getName}Impl impl = (@{genType.getName}Impl) base;
217 if (!impl.@{augmentField.getName}.isEmpty()) {
218 this.@{augmentField.getName} = new @{importedNames.get("hashMap")}<>(impl.@{augmentField.getName});
220 } @{"else"} if (base instanceof @{importedNames.get("augmentationHolder")}) {
221 @@SuppressWarnings("unchecked")
222 @{importedNames.get("augmentationHolder")}<@{importedNames.get("genType")}> casted =(@{importedNames.get("augmentationHolder")}<@{importedNames.get("genType")}>) base;
223 if (!casted.augmentations().isEmpty()) {
224 this.@{augmentField.getName} = new @{importedNames.get("hashMap")}<>(casted.augmentations());
233 @generateSetters() = {
234 @for(field <- properties) {
235 @if(!field.getReturnType.isInstanceOf[GeneratedType] && getRestrictions(field.getReturnType) != null) {
236 @if(getRestrictions(field.getReturnType).getRangeConstraints != null && !getRestrictions(field.getReturnType).getRangeConstraints.isEmpty) {
237 @{AbstractRangeGenerator.forType(field.getReturnType).generateRangeChecker(toFirstUpper(field.getName),
238 getRestrictions(field.getReturnType).getRangeConstraints)}
240 @if(getRestrictions(field.getReturnType).getLengthConstraints != null && !getRestrictions(field.getReturnType).getLengthConstraints.isEmpty) {
241 @{LengthGenerator.generateLengthChecker(fieldName(field), field.getReturnType,
242 getRestrictions(field.getReturnType).getLengthConstraints)}
245 public @{genType.getName}Builder set@{toFirstUpper(field.getName)}(final @{importedNames.get("augmentFieldReturnType")} value) {
246 @if(!field.getReturnType.isInstanceOf[GeneratedType] && getRestrictions(field.getReturnType) != null) {
248 @if(getRestrictions(field.getReturnType).getRangeConstraints != null && !getRestrictions(field.getReturnType).getRangeConstraints.isEmpty) {
249 @if(field.getReturnType.isInstanceOf[ConcreteType]) {
250 @{AbstractRangeGenerator.forType(field.getReturnType).generateRangeCheckerCall(toFirstUpper(field.getName), "value")}
252 @{AbstractRangeGenerator.forType(field.getReturnType).generateRangeCheckerCall(toFirstUpper(field.getName), "value.getValue()")}
255 @if(getRestrictions(field.getReturnType).getLengthConstraints != null && !getRestrictions(field.getReturnType).getLengthConstraints.isEmpty) {
256 @if(field.getReturnType.isInstanceOf[ConcreteType]) {
257 @{LengthGenerator.generateLengthCheckerCall(fieldName(field), "value")}
259 @{LengthGenerator.generateLengthCheckerCall(fieldName(field), "value.getValue()")}
264 this.@{fieldName(field)} = value;
268 @if(augmentField != null) {
269 public @{genType.getName}Builder add@{toFirstUpper(augmentField.getName)}(@{importedNames.get("class")}<? extends @{importedNames.get("augmentFieldReturnType")}> augmentationType, @{importedNames.get("augmentFieldReturnType")} augmentation) {
270 if (augmentation == null) {
271 return remove@{toFirstUpper(augmentField.getName)}(augmentationType);
274 if (!(this.@{augmentField.getName} instanceof @{importedNames.get("hashMap")})) {
275 this.@{augmentField.getName} = new @{importedNames.get("hashMap")}<>();
278 this.@{augmentField.getName}.put(augmentationType, augmentation);
282 public @{genType.getName}Builder remove@{toFirstUpper(augmentField.getName)}
283 (@{importedNames.get("class")}<? extends @{importedNames.get("augmentFieldReturnType")}> augmentationType) {
284 if (this.@{augmentField.getName} instanceof @{importedNames.get("hashMap")}) {
285 this.@{augmentField.getName}.remove(augmentationType);
292 @generateGetters(addOverride: Boolean) = {
293 @if(!getterMethods.isEmpty) {
294 @for(property <- getterMethods) {
295 @if(addOverride) {@@Override}
299 @if(augmentField != null) {
300 @@SuppressWarnings("unchecked")
301 @if(addOverride) {@@Override}
302 public <E extends @{importedNames.get("augmentFieldReturnType")}> E get@{toFirstUpper(augmentField.getName)}
303 (@{importedNames.get("class")}<E> augmentationType) {
304 if (augmentationType == null) {
305 throw new IllegalArgumentException("Augmentation Type reference cannot be NULL!");
307 return (E) @{augmentField.getName}.get(augmentationType);
312 @generateHashCode() = {
313 @if(!properties.isEmpty || augmentField != null) {
314 private int hash = 0;
315 private volatile boolean hashValid = false;
318 public int hashCode() {
323 final int prime = 31;
325 @for(property <- properties) {
326 @if(property.getReturnType.getName.contains("[")) {
327 result = prime * result + @{importedNames.get("arrays")}.hashCode(@{fieldName(property)});
329 result = prime * result + @{importedNames.get("objects")}.hashCode(@{fieldName(property)});
332 @if(augmentField != null) {
333 result = prime * result + @{importedNames.get("objects")}.hashCode(@{augmentField.getName});
343 @generateToString() = {
344 @if(properties != null) {
346 public @{importedNames.get("string")} toString() {
347 @{importedNames.get("string")} name = "@{genType.getName} [";
348 @{importedNames.get("stringBuilder")} builder = new @{importedNames.get("stringBuilder")}(name);
349 @for((property, index) <- properties.zipWithIndex) {
350 if (@{fieldName(property)} != null) {
351 builder.append("@{fieldName(property)}=");
352 @if(property.getReturnType.getName.contains("[")) {
353 builder.append(@{importedNames.get("arrays")}.toString(@{fieldName(property)}));
355 builder.append(@{fieldName(property)});
357 @if(properties.size() > 1 && index < properties.size()-1){
358 builder.append(", ");
362 @if(augmentField != null) {
363 @if(!properties.isEmpty()){
364 final int builderLength = builder.length();
365 final int builderAdditionalLength = builder.substring(name.length(), builderLength).length();
366 if (builderAdditionalLength > 2 && !builder.substring(builderLength - 2, builderLength).equals(", ")) {
367 builder.append(", ");
370 builder.append("@{augmentField.getName}=");
371 builder.append(@{augmentField.getName}.values());
372 return builder.append(']').toString();
374 @if(properties.isEmpty()){
375 return builder.append(']').toString();
377 return builder.append(']').toString();
384 @generateEquals() = {
385 @if(!properties.isEmpty || augmentField != null) {
387 public boolean equals(@{importedNames.get("object")} obj) {
391 if (!(obj instanceof @{importedNames.get("treeNode")})) {
394 if (!@{importedNames.get("genType")}.class.equals(((@{importedNames.get("treeNode")})obj).getImplementedInterface)) {
397 @{importedNames.get("genType")} other = (@{importedNames.get("genType")})obj;
398 @for(property <- properties) {
399 @if(property.getReturnType.getName.contains("[")) {
400 if (!@{importedNames.get("arrays")}.equals(@{fieldName(property)}, other.@{getterMethodName(property)}()))
402 if (!@{importedNames.get("objects")}.equals(@{fieldName(property)}, other.@{getterMethodName(property)}()))
408 @if(augmentField != null) {
409 if (getClass() == obj.getClass()) {
410 // Simple case: we are comparing against self
411 @{genType.getName}Impl otherImpl = (@{genType.getName}Impl) obj;
412 if (!@{importedNames.get("objects")}.equals(@{augmentField.getName}, otherImpl.@{augmentField.getName})) {
416 // Hard case: compare our augments with presence there...
417 for (@{importedNames.get("map")}.Entry<@{importedNames.get("class")}<? extends @{importedNames.get("augmentFieldReturnType")}>, @{importedNames.get("augmentFieldReturnType")}> e : @{augmentField.getName}.entrySet()) {
418 if (!e.getValue().equals(other.getAugmentation(e.getKey()))) {
422 // .. and give the other one the chance to do the same
423 if (!obj.equals(this)) {