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 EnumBuilder builder = new EnumerationBuilderImpl(packageName,
106 enumerations.add(builder);
111 public ConstantBuilder addConstant(Type type, String name, Object value) {
112 final ConstantBuilder builder = new ConstantBuilderImpl(type, name,
114 constantDefintions.add(builder);
119 public MethodSignatureBuilder addMethod(String name) {
120 final MethodSignatureBuilder builder = new MethodSignatureBuilderImpl(
122 methodDefinitions.add(builder);
127 public GeneratedPropertyBuilder addProperty(String name) {
128 final GeneratedPropertyBuilder builder = new GeneratedPropertyBuilderImpl(
130 properties.add(builder);
135 public boolean addEqualsIdentity(GeneratedPropertyBuilder property) {
136 return equalsProperties.add(property);
140 public boolean addHashIdentity(GeneratedPropertyBuilder property) {
141 return hashProperties.add(property);
145 public boolean addToStringProperty(GeneratedPropertyBuilder property) {
146 return toStringProperties.add(property);
150 public GeneratedTransferObject toInstance() {
151 return new GeneratedTransferObjectImpl(packageName, name, comment,
152 annotationBuilders, extendsType, implementsTypes, constantDefintions, enumerations,
153 methodDefinitions, properties, equalsProperties,
154 hashProperties, toStringProperties);
157 private static final class GeneratedPropertyBuilderImpl implements
158 GeneratedPropertyBuilder {
160 private final String name;
161 private final List<AnnotationTypeBuilder> annotationBuilders = new ArrayList<AnnotationTypeBuilder>();
162 private Type returnType;
163 private final List<MethodSignature.Parameter> parameters;
164 private String comment = "";
165 private AccessModifier accessModifier;
166 private boolean isFinal;
167 private boolean isReadOnly;
169 public GeneratedPropertyBuilderImpl(final String name) {
172 parameters = new ArrayList<MethodSignature.Parameter>();
174 this.isReadOnly = true;
175 accessModifier = AccessModifier.PUBLIC;
178 public String getName() {
183 public AnnotationTypeBuilder addAnnotation(String packageName,
185 if (packageName != null && name != null) {
186 final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(
188 if (annotationBuilders.add(builder)) {
196 public boolean addReturnType(Type returnType) {
197 if (returnType != null) {
198 this.returnType = returnType;
199 this.parameters.add(new MethodParameterImpl(name, returnType));
206 public void accessorModifier(final AccessModifier modifier) {
207 accessModifier = modifier;
211 public void addComment(String comment) {
212 if (comment != null) {
213 this.comment = comment;
218 public void setFinal(boolean isFinal) {
219 this.isFinal = isFinal;
223 public void setReadOnly(boolean isReadOnly) {
224 this.isReadOnly = isReadOnly;
228 public GeneratedProperty toInstance(final Type definingType) {
229 return new GeneratedPropertyImpl(name, comment, annotationBuilders, definingType,
230 returnType, isFinal, isReadOnly, parameters, accessModifier);
234 private static final class GeneratedPropertyImpl implements
237 private final String name;
238 private List<AnnotationType> annotations;
239 private final String comment;
240 private final Type parent;
241 private final Type returnType;
242 private final boolean isFinal;
243 private final boolean isReadOnly;
244 private final List<MethodSignature.Parameter> parameters;
245 private final AccessModifier modifier;
247 public GeneratedPropertyImpl(final String name, final String comment,
248 final List<AnnotationTypeBuilder> annotationBuilders, final Type parent, final Type returnType,
249 final boolean isFinal, final boolean isReadOnly,
250 final List<Parameter> parameters, final AccessModifier modifier) {
253 this.annotations = new ArrayList<AnnotationType>();
254 for (final AnnotationTypeBuilder builder : annotationBuilders) {
255 this.annotations.add(builder.toInstance());
257 this.annotations = Collections.unmodifiableList(this.annotations);
258 this.comment = comment;
259 this.parent = parent;
260 this.returnType = returnType;
261 this.isFinal = isFinal;
262 this.isReadOnly = isReadOnly;
263 this.parameters = Collections.unmodifiableList(parameters);
264 this.modifier = modifier;
268 public String getName() {
273 public String getComment() {
278 public Type getDefiningType() {
283 public List<AnnotationType> getAnnotations() {
288 public Type getReturnType() {
293 public List<Parameter> getParameters() {
298 public AccessModifier getAccessModifier() {
303 public boolean isReadOnly() {
308 public boolean isFinal() {
315 public int hashCode() {
316 final int prime = 31;
318 result = prime * result + ((name == null) ? 0 : name.hashCode());
319 result = prime * result
320 + ((parameters == null) ? 0 : parameters.hashCode());
321 result = prime * result
322 + ((returnType == null) ? 0 : returnType.hashCode());
327 public boolean equals(Object obj) {
334 if (getClass() != obj.getClass()) {
337 GeneratedPropertyImpl other = (GeneratedPropertyImpl) obj;
339 if (other.name != null) {
342 } else if (!name.equals(other.name)) {
345 if (parameters == null) {
346 if (other.parameters != null) {
349 } else if (!parameters.equals(other.parameters)) {
352 if (returnType == null) {
353 if (other.returnType != null) {
356 } else if (!returnType.getPackageName().equals(other.returnType.getPackageName())) {
358 } else if (!returnType.getName().equals(other.returnType.getName())) {
365 public String toString() {
366 StringBuilder builder = new StringBuilder();
367 builder.append("GeneratedPropertyImpl [name=");
368 builder.append(name);
369 builder.append(", annotations=");
370 builder.append(annotations);
371 builder.append(", comment=");
372 builder.append(comment);
373 if (parent != null) {
374 builder.append(", parent=");
375 builder.append(parent.getPackageName());
377 builder.append(parent.getName());
379 builder.append(", parent=null");
381 builder.append(", returnType=");
382 builder.append(returnType);
383 builder.append(", isFinal=");
384 builder.append(isFinal);
385 builder.append(", isReadOnly=");
386 builder.append(isReadOnly);
387 builder.append(", parameters=");
388 builder.append(parameters);
389 builder.append(", modifier=");
390 builder.append(modifier);
392 return builder.toString();
396 private static final class GeneratedTransferObjectImpl implements
397 GeneratedTransferObject {
399 private final String packageName;
400 private final String name;
401 private final String comment;
402 private final List<Constant> constants;
403 private final List<Enumeration> enumerations;
404 private final List<GeneratedProperty> properties;
405 private final List<GeneratedProperty> equalsProperties;
406 private final List<GeneratedProperty> hashCodeProperties;
407 private final List<GeneratedProperty> stringProperties;
408 private final List<AnnotationType> annotations;
409 private final List<MethodSignature> methods;
410 private final GeneratedTransferObject extendsType;
411 private final List<Type> implementsTypes;
413 public GeneratedTransferObjectImpl(final String packageName,
415 final String comment,
416 final List<AnnotationTypeBuilder> annotationBuilders,
417 final GeneratedTransferObject extendsType,
418 final List<Type> implementsTypes,
419 final List<ConstantBuilder> constantBuilders,
420 final List<EnumBuilder> enumBuilders,
421 final List<MethodSignatureBuilder> methodBuilders,
422 final List<GeneratedPropertyBuilder> propBuilers,
423 final List<GeneratedPropertyBuilder> equalsBuilers,
424 final List<GeneratedPropertyBuilder> hashCodeBuilers,
425 final List<GeneratedPropertyBuilder> stringBuilers) {
427 this.packageName = packageName;
429 this.comment = comment;
430 this.annotations = toUnmodifiableAnnotations(annotationBuilders);
431 this.extendsType = extendsType;
432 this.implementsTypes = Collections.unmodifiableList(implementsTypes);
433 this.constants = toUnmodifiableConstant(constantBuilders);
434 this.enumerations = toUnmodifiableEnumerations(enumBuilders);
435 this.properties = toUnmodifiableProperties(propBuilers);
436 this.methods = toUnmodifiableMethods(methodBuilders);
437 this.equalsProperties = toUnmodifiableProperties(equalsBuilers);
438 this.hashCodeProperties = toUnmodifiableProperties(hashCodeBuilers);
439 this.stringProperties = toUnmodifiableProperties(stringBuilers);
442 private List<AnnotationType> toUnmodifiableAnnotations(
443 final List<AnnotationTypeBuilder> annotationBuilders) {
444 final List<AnnotationType> annotations = new ArrayList<AnnotationType>();
445 for (final AnnotationTypeBuilder builder : annotationBuilders) {
446 annotations.add(builder.toInstance());
448 return Collections.unmodifiableList(annotations);
451 private List<Enumeration> toUnmodifiableEnumerations(
452 final List<EnumBuilder> enumBuilders) {
453 final List<Enumeration> enumerations = new ArrayList<Enumeration>();
454 for (final EnumBuilder builder : enumBuilders) {
455 enumerations.add(builder.toInstance(this));
457 return Collections.unmodifiableList(enumerations);
460 private List<Constant> toUnmodifiableConstant(
461 final List<ConstantBuilder> constBuilders) {
462 final List<Constant> constants = new ArrayList<Constant>();
463 for (final ConstantBuilder builder : constBuilders) {
464 constants.add(builder.toInstance(this));
466 return Collections.unmodifiableList(constants);
469 private List<MethodSignature> toUnmodifiableMethods(
470 final List<MethodSignatureBuilder> methodBuilders) {
471 final List<MethodSignature> methods = new ArrayList<MethodSignature>();
472 for (final MethodSignatureBuilder builder : methodBuilders) {
473 methods.add(builder.toInstance(this));
475 return Collections.unmodifiableList(methods);
478 private List<GeneratedProperty> toUnmodifiableProperties(
479 final List<GeneratedPropertyBuilder> propBuilders) {
480 final List<GeneratedProperty> constants = new ArrayList<GeneratedProperty>();
481 for (final GeneratedPropertyBuilder builder : propBuilders) {
482 constants.add(builder.toInstance(this));
484 return Collections.unmodifiableList(constants);
488 public String getPackageName() {
493 public String getName() {
498 public Type getParentType() {
503 public String getComment() {
508 public List<AnnotationType> getAnnotations() {
513 public List<Type> getImplements() {
514 return implementsTypes;
518 public GeneratedTransferObject getExtends() {
523 public List<Enumeration> getEnumDefintions() {
528 public List<Constant> getConstantDefinitions() {
533 public List<MethodSignature> getMethodDefinitions() {
538 public List<GeneratedProperty> getProperties() {
543 public List<GeneratedProperty> getEqualsIdentifiers() {
544 return equalsProperties;
548 public List<GeneratedProperty> getHashCodeIdentifiers() {
549 return hashCodeProperties;
553 public List<GeneratedProperty> getToStringIdentifiers() {
554 return stringProperties;
558 public int hashCode() {
559 final int prime = 31;
561 result = prime * result + ((name == null) ? 0 : name.hashCode());
562 result = prime * result
563 + ((packageName == null) ? 0 : packageName.hashCode());
568 public boolean equals(Object obj) {
575 if (getClass() != obj.getClass()) {
578 GeneratedTransferObjectImpl other = (GeneratedTransferObjectImpl) obj;
580 if (other.name != null) {
583 } else if (!name.equals(other.name)) {
586 if (packageName == null) {
587 if (other.packageName != null) {
590 } else if (!packageName.equals(other.packageName)) {
597 public String toString() {
598 StringBuilder builder = new StringBuilder();
599 builder.append("GeneratedTransferObject [packageName=");
600 builder.append(packageName);
601 builder.append(", name=");
602 builder.append(name);
603 builder.append(", comment=");
604 builder.append(comment);
605 builder.append(", constants=");
606 builder.append(constants);
607 builder.append(", enumerations=");
608 builder.append(enumerations);
609 builder.append(", properties=");
610 builder.append(properties);
611 builder.append(", equalsProperties=");
612 builder.append(equalsProperties);
613 builder.append(", hashCodeProperties=");
614 builder.append(hashCodeProperties);
615 builder.append(", stringProperties=");
616 builder.append(stringProperties);
617 builder.append(", annotations=");
618 builder.append(annotations);
619 builder.append(", methods=");
620 builder.append(methods);
622 return builder.toString();