2 * Copyright (c) 2013 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.controller.binding.generator.util.generated.type.builder;
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
14 import org.opendaylight.controller.sal.binding.model.api.AccessModifier;
15 import org.opendaylight.controller.sal.binding.model.api.AnnotationType;
16 import org.opendaylight.controller.sal.binding.model.api.Constant;
17 import org.opendaylight.controller.sal.binding.model.api.Enumeration;
18 import org.opendaylight.controller.sal.binding.model.api.GeneratedProperty;
19 import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;
20 import org.opendaylight.controller.sal.binding.model.api.MethodSignature;
21 import org.opendaylight.controller.sal.binding.model.api.Type;
22 import org.opendaylight.controller.sal.binding.model.api.type.builder.AnnotationTypeBuilder;
23 import org.opendaylight.controller.sal.binding.model.api.type.builder.ConstantBuilder;
24 import org.opendaylight.controller.sal.binding.model.api.type.builder.EnumBuilder;
25 import org.opendaylight.controller.sal.binding.model.api.type.builder.GeneratedPropertyBuilder;
26 import org.opendaylight.controller.sal.binding.model.api.type.builder.GeneratedTOBuilder;
27 import org.opendaylight.controller.sal.binding.model.api.type.builder.MethodSignatureBuilder;
29 public final class GeneratedTOBuilderImpl implements GeneratedTOBuilder {
30 private String packageName;
31 private final String name;
32 private String comment = "";
34 private GeneratedTransferObject extendsType;
35 private final List<Type> implementsTypes = new ArrayList<Type>();
36 private final List<EnumBuilder> enumerations = new ArrayList<EnumBuilder>();
37 private final List<GeneratedPropertyBuilder> properties = new ArrayList<GeneratedPropertyBuilder>();
38 private final List<GeneratedPropertyBuilder> equalsProperties = new ArrayList<GeneratedPropertyBuilder>();
39 private final List<GeneratedPropertyBuilder> hashProperties = new ArrayList<GeneratedPropertyBuilder>();
40 private final List<GeneratedPropertyBuilder> toStringProperties = new ArrayList<GeneratedPropertyBuilder>();
42 private final List<ConstantBuilder> constantDefintions = new ArrayList<ConstantBuilder>();
43 private final List<MethodSignatureBuilder> methodDefinitions = new ArrayList<MethodSignatureBuilder>();
44 private final List<AnnotationTypeBuilder> annotationBuilders = new ArrayList<AnnotationTypeBuilder>();
46 public GeneratedTOBuilderImpl(String packageName, String name) {
48 this.packageName = packageName;
53 public String getPackageName() {
58 public String getName() {
63 public Type getParentType() {
64 // TODO Auto-generated method stub
69 public void addComment(final String comment) {
70 this.comment = comment;
74 public AnnotationTypeBuilder addAnnotation(String packageName, String name) {
75 if (packageName != null && name != null) {
76 final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(
78 if (annotationBuilders.add(builder)) {
86 public boolean addImplementsType(final Type genType) {
87 if (genType != null) {
88 return implementsTypes.add(genType);
94 public boolean addExtendsType(final GeneratedTransferObject genTransObj) {
95 if (genTransObj != null) {
96 extendsType = genTransObj;
103 public EnumBuilder addEnumeration(String name) {
104 final String innerPackageName = packageName + "." + this.name;
105 final EnumBuilder builder = new EnumerationBuilderImpl(innerPackageName,
107 enumerations.add(builder);
112 public ConstantBuilder addConstant(Type type, String name, Object value) {
113 final ConstantBuilder builder = new ConstantBuilderImpl(type, name,
115 constantDefintions.add(builder);
120 public MethodSignatureBuilder addMethod(String name) {
121 final MethodSignatureBuilder builder = new MethodSignatureBuilderImpl(
123 methodDefinitions.add(builder);
128 public GeneratedPropertyBuilder addProperty(String name) {
129 final GeneratedPropertyBuilder builder = new GeneratedPropertyBuilderImpl(
131 properties.add(builder);
136 public boolean addEqualsIdentity(GeneratedPropertyBuilder property) {
137 return equalsProperties.add(property);
141 public boolean addHashIdentity(GeneratedPropertyBuilder property) {
142 return hashProperties.add(property);
146 public boolean addToStringProperty(GeneratedPropertyBuilder property) {
147 return toStringProperties.add(property);
151 public GeneratedTransferObject toInstance() {
152 return new GeneratedTransferObjectImpl(packageName, name, comment,
153 annotationBuilders, extendsType, implementsTypes, constantDefintions, enumerations,
154 methodDefinitions, properties, equalsProperties,
155 hashProperties, toStringProperties);
158 private static final class GeneratedPropertyBuilderImpl implements
159 GeneratedPropertyBuilder {
161 private final String name;
162 private final List<AnnotationTypeBuilder> annotationBuilders = new ArrayList<AnnotationTypeBuilder>();
163 private Type returnType;
164 private final List<MethodSignature.Parameter> parameters;
165 private String comment = "";
166 private AccessModifier accessModifier;
167 private boolean isFinal;
168 private boolean isReadOnly;
170 public GeneratedPropertyBuilderImpl(final String name) {
173 parameters = new ArrayList<MethodSignature.Parameter>();
175 this.isReadOnly = true;
176 accessModifier = AccessModifier.PUBLIC;
179 public String getName() {
184 public AnnotationTypeBuilder addAnnotation(String packageName,
186 if (packageName != null && name != null) {
187 final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(
189 if (annotationBuilders.add(builder)) {
197 public boolean addReturnType(Type returnType) {
198 if (returnType != null) {
199 this.returnType = returnType;
200 this.parameters.add(new MethodParameterImpl(name, returnType));
207 public void accessorModifier(final AccessModifier modifier) {
208 accessModifier = modifier;
212 public void addComment(String comment) {
213 if (comment != null) {
214 this.comment = comment;
219 public void setFinal(boolean isFinal) {
220 this.isFinal = isFinal;
224 public void setReadOnly(boolean isReadOnly) {
225 this.isReadOnly = isReadOnly;
229 public GeneratedProperty toInstance(final Type definingType) {
230 return new GeneratedPropertyImpl(name, comment, annotationBuilders, definingType,
231 returnType, isFinal, isReadOnly, parameters, accessModifier);
235 private static final class GeneratedPropertyImpl implements
238 private final String name;
239 private List<AnnotationType> annotations;
240 private final String comment;
241 private final Type parent;
242 private final Type returnType;
243 private final boolean isFinal;
244 private final boolean isReadOnly;
245 private final List<MethodSignature.Parameter> parameters;
246 private final AccessModifier modifier;
248 public GeneratedPropertyImpl(final String name, final String comment,
249 final List<AnnotationTypeBuilder> annotationBuilders, final Type parent, final Type returnType,
250 final boolean isFinal, final boolean isReadOnly,
251 final List<Parameter> parameters, final AccessModifier modifier) {
254 this.annotations = new ArrayList<AnnotationType>();
255 for (final AnnotationTypeBuilder builder : annotationBuilders) {
256 this.annotations.add(builder.toInstance());
258 this.annotations = Collections.unmodifiableList(this.annotations);
259 this.comment = comment;
260 this.parent = parent;
261 this.returnType = returnType;
262 this.isFinal = isFinal;
263 this.isReadOnly = isReadOnly;
264 this.parameters = Collections.unmodifiableList(parameters);
265 this.modifier = modifier;
269 public String getName() {
274 public String getComment() {
279 public Type getDefiningType() {
284 public List<AnnotationType> getAnnotations() {
289 public Type getReturnType() {
294 public List<Parameter> getParameters() {
299 public AccessModifier getAccessModifier() {
304 public boolean isReadOnly() {
309 public boolean isFinal() {
316 public int hashCode() {
317 final int prime = 31;
319 result = prime * result + ((name == null) ? 0 : name.hashCode());
320 result = prime * result
321 + ((parameters == null) ? 0 : parameters.hashCode());
322 result = prime * result
323 + ((returnType == null) ? 0 : returnType.hashCode());
328 public boolean equals(Object obj) {
335 if (getClass() != obj.getClass()) {
338 GeneratedPropertyImpl other = (GeneratedPropertyImpl) obj;
340 if (other.name != null) {
343 } else if (!name.equals(other.name)) {
346 if (parameters == null) {
347 if (other.parameters != null) {
350 } else if (!parameters.equals(other.parameters)) {
353 if (returnType == null) {
354 if (other.returnType != null) {
357 } else if (!returnType.getPackageName().equals(other.returnType.getPackageName())) {
359 } else if (!returnType.getName().equals(other.returnType.getName())) {
366 public String toString() {
367 StringBuilder builder = new StringBuilder();
368 builder.append("GeneratedPropertyImpl [name=");
369 builder.append(name);
370 builder.append(", annotations=");
371 builder.append(annotations);
372 builder.append(", comment=");
373 builder.append(comment);
374 if (parent != null) {
375 builder.append(", parent=");
376 builder.append(parent.getPackageName());
378 builder.append(parent.getName());
380 builder.append(", parent=null");
382 builder.append(", returnType=");
383 builder.append(returnType);
384 builder.append(", isFinal=");
385 builder.append(isFinal);
386 builder.append(", isReadOnly=");
387 builder.append(isReadOnly);
388 builder.append(", parameters=");
389 builder.append(parameters);
390 builder.append(", modifier=");
391 builder.append(modifier);
393 return builder.toString();
397 private static final class GeneratedTransferObjectImpl implements
398 GeneratedTransferObject {
400 private final String packageName;
401 private final String name;
402 private final String comment;
403 private final List<Constant> constants;
404 private final List<Enumeration> enumerations;
405 private final List<GeneratedProperty> properties;
406 private final List<GeneratedProperty> equalsProperties;
407 private final List<GeneratedProperty> hashCodeProperties;
408 private final List<GeneratedProperty> stringProperties;
409 private final List<AnnotationType> annotations;
410 private final List<MethodSignature> methods;
411 private final GeneratedTransferObject extendsType;
412 private final List<Type> implementsTypes;
414 public GeneratedTransferObjectImpl(final String packageName,
416 final String comment,
417 final List<AnnotationTypeBuilder> annotationBuilders,
418 final GeneratedTransferObject extendsType,
419 final List<Type> implementsTypes,
420 final List<ConstantBuilder> constantBuilders,
421 final List<EnumBuilder> enumBuilders,
422 final List<MethodSignatureBuilder> methodBuilders,
423 final List<GeneratedPropertyBuilder> propBuilers,
424 final List<GeneratedPropertyBuilder> equalsBuilers,
425 final List<GeneratedPropertyBuilder> hashCodeBuilers,
426 final List<GeneratedPropertyBuilder> stringBuilers) {
428 this.packageName = packageName;
430 this.comment = comment;
431 this.annotations = toUnmodifiableAnnotations(annotationBuilders);
432 this.extendsType = extendsType;
433 this.implementsTypes = Collections.unmodifiableList(implementsTypes);
434 this.constants = toUnmodifiableConstant(constantBuilders);
435 this.enumerations = toUnmodifiableEnumerations(enumBuilders);
436 this.properties = toUnmodifiableProperties(propBuilers);
437 this.methods = toUnmodifiableMethods(methodBuilders);
438 this.equalsProperties = toUnmodifiableProperties(equalsBuilers);
439 this.hashCodeProperties = toUnmodifiableProperties(hashCodeBuilers);
440 this.stringProperties = toUnmodifiableProperties(stringBuilers);
443 private List<AnnotationType> toUnmodifiableAnnotations(
444 final List<AnnotationTypeBuilder> annotationBuilders) {
445 final List<AnnotationType> annotations = new ArrayList<AnnotationType>();
446 for (final AnnotationTypeBuilder builder : annotationBuilders) {
447 annotations.add(builder.toInstance());
449 return Collections.unmodifiableList(annotations);
452 private List<Enumeration> toUnmodifiableEnumerations(
453 final List<EnumBuilder> enumBuilders) {
454 final List<Enumeration> enumerations = new ArrayList<Enumeration>();
455 for (final EnumBuilder builder : enumBuilders) {
456 enumerations.add(builder.toInstance(this));
458 return Collections.unmodifiableList(enumerations);
461 private List<Constant> toUnmodifiableConstant(
462 final List<ConstantBuilder> constBuilders) {
463 final List<Constant> constants = new ArrayList<Constant>();
464 for (final ConstantBuilder builder : constBuilders) {
465 constants.add(builder.toInstance(this));
467 return Collections.unmodifiableList(constants);
470 private List<MethodSignature> toUnmodifiableMethods(
471 final List<MethodSignatureBuilder> methodBuilders) {
472 final List<MethodSignature> methods = new ArrayList<MethodSignature>();
473 for (final MethodSignatureBuilder builder : methodBuilders) {
474 methods.add(builder.toInstance(this));
476 return Collections.unmodifiableList(methods);
479 private List<GeneratedProperty> toUnmodifiableProperties(
480 final List<GeneratedPropertyBuilder> propBuilders) {
481 final List<GeneratedProperty> constants = new ArrayList<GeneratedProperty>();
482 for (final GeneratedPropertyBuilder builder : propBuilders) {
483 constants.add(builder.toInstance(this));
485 return Collections.unmodifiableList(constants);
489 public String getPackageName() {
494 public String getName() {
499 public Type getParentType() {
504 public String getComment() {
509 public List<AnnotationType> getAnnotations() {
514 public List<Type> getImplements() {
515 return implementsTypes;
519 public GeneratedTransferObject getExtends() {
524 public List<Enumeration> getEnumDefintions() {
529 public List<Constant> getConstantDefinitions() {
534 public List<MethodSignature> getMethodDefinitions() {
539 public List<GeneratedProperty> getProperties() {
544 public List<GeneratedProperty> getEqualsIdentifiers() {
545 return equalsProperties;
549 public List<GeneratedProperty> getHashCodeIdentifiers() {
550 return hashCodeProperties;
554 public List<GeneratedProperty> getToStringIdentifiers() {
555 return stringProperties;
559 public int hashCode() {
560 final int prime = 31;
562 result = prime * result + ((name == null) ? 0 : name.hashCode());
563 result = prime * result
564 + ((packageName == null) ? 0 : packageName.hashCode());
569 public boolean equals(Object obj) {
576 if (getClass() != obj.getClass()) {
579 GeneratedTransferObjectImpl other = (GeneratedTransferObjectImpl) obj;
581 if (other.name != null) {
584 } else if (!name.equals(other.name)) {
587 if (packageName == null) {
588 if (other.packageName != null) {
591 } else if (!packageName.equals(other.packageName)) {
598 public String toString() {
599 StringBuilder builder = new StringBuilder();
600 builder.append("GeneratedTransferObject [packageName=");
601 builder.append(packageName);
602 builder.append(", name=");
603 builder.append(name);
604 builder.append(", comment=");
605 builder.append(comment);
606 builder.append(", constants=");
607 builder.append(constants);
608 builder.append(", enumerations=");
609 builder.append(enumerations);
610 builder.append(", properties=");
611 builder.append(properties);
612 builder.append(", equalsProperties=");
613 builder.append(equalsProperties);
614 builder.append(", hashCodeProperties=");
615 builder.append(hashCodeProperties);
616 builder.append(", stringProperties=");
617 builder.append(stringProperties);
618 builder.append(", annotations=");
619 builder.append(annotations);
620 builder.append(", methods=");
621 builder.append(methods);
623 return builder.toString();