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 final List<EnumBuilder> enumerations = new ArrayList<EnumBuilder>();
35 private final List<GeneratedPropertyBuilder> properties = new ArrayList<GeneratedPropertyBuilder>();
36 private final List<GeneratedPropertyBuilder> equalsProperties = new ArrayList<GeneratedPropertyBuilder>();
37 private final List<GeneratedPropertyBuilder> hashProperties = new ArrayList<GeneratedPropertyBuilder>();
38 private final List<GeneratedPropertyBuilder> toStringProperties = new ArrayList<GeneratedPropertyBuilder>();
40 private final List<ConstantBuilder> constantDefintions = new ArrayList<ConstantBuilder>();
41 private final List<MethodSignatureBuilder> methodDefinitions = new ArrayList<MethodSignatureBuilder>();
42 private final List<AnnotationTypeBuilder> annotationBuilders = new ArrayList<AnnotationTypeBuilder>();
44 public GeneratedTOBuilderImpl(String packageName, String name) {
46 this.packageName = packageName;
51 public String getPackageName() {
56 public String getName() {
61 public Type getParentType() {
62 // TODO Auto-generated method stub
67 public void addComment(final String comment) {
68 this.comment = comment;
72 public AnnotationTypeBuilder addAnnotation(String packageName, String name) {
73 if (packageName != null && name != null) {
74 final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(
76 if (annotationBuilders.add(builder)) {
84 public EnumBuilder addEnumeration(String name) {
85 final EnumBuilder builder = new EnumerationBuilderImpl(packageName,
87 enumerations.add(builder);
92 public ConstantBuilder addConstant(Type type, String name, Object value) {
93 final ConstantBuilder builder = new ConstantBuilderImpl(type, name,
95 constantDefintions.add(builder);
100 public MethodSignatureBuilder addMethod(String name) {
101 final MethodSignatureBuilder builder = new MethodSignatureBuilderImpl(
103 methodDefinitions.add(builder);
108 public GeneratedPropertyBuilder addProperty(String name) {
109 final GeneratedPropertyBuilder builder = new GeneratedPropertyBuilderImpl(
111 properties.add(builder);
116 public boolean addEqualsIdentity(GeneratedPropertyBuilder property) {
117 return equalsProperties.add(property);
121 public boolean addHashIdentity(GeneratedPropertyBuilder property) {
122 return hashProperties.add(property);
126 public boolean addToStringProperty(GeneratedPropertyBuilder property) {
127 return toStringProperties.add(property);
131 public GeneratedTransferObject toInstance() {
132 return new GeneratedTransferObjectImpl(packageName, name, comment,
133 annotationBuilders, constantDefintions, enumerations,
134 methodDefinitions, properties, equalsProperties,
135 hashProperties, toStringProperties);
138 private static final class GeneratedPropertyBuilderImpl implements
139 GeneratedPropertyBuilder {
141 private final String name;
142 private final List<AnnotationTypeBuilder> annotationBuilders = new ArrayList<AnnotationTypeBuilder>();
143 private Type returnType;
144 private final List<MethodSignature.Parameter> parameters;
145 private String comment = "";
146 private AccessModifier accessModifier;
147 private boolean isFinal;
148 private boolean isReadOnly;
150 public GeneratedPropertyBuilderImpl(final String name) {
153 parameters = new ArrayList<MethodSignature.Parameter>();
155 this.isReadOnly = true;
156 accessModifier = AccessModifier.PUBLIC;
159 public String getName() {
164 public AnnotationTypeBuilder addAnnotation(String packageName,
166 if (packageName != null && name != null) {
167 final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(
169 if (annotationBuilders.add(builder)) {
177 public boolean addReturnType(Type returnType) {
178 if (returnType != null) {
179 this.returnType = returnType;
180 this.parameters.add(new MethodParameterImpl(name, returnType));
187 public void accessorModifier(final AccessModifier modifier) {
188 accessModifier = modifier;
192 public void addComment(String comment) {
193 if (comment != null) {
194 this.comment = comment;
199 public void setFinal(boolean isFinal) {
200 this.isFinal = isFinal;
204 public void setReadOnly(boolean isReadOnly) {
205 this.isReadOnly = isReadOnly;
209 public GeneratedProperty toInstance(final Type definingType) {
210 return new GeneratedPropertyImpl(name, comment, annotationBuilders, definingType,
211 returnType, isFinal, isReadOnly, parameters, accessModifier);
215 private static final class GeneratedPropertyImpl implements
218 private final String name;
219 private List<AnnotationType> annotations;
220 private final String comment;
221 private final Type parent;
222 private final Type returnType;
223 private final boolean isFinal;
224 private final boolean isReadOnly;
225 private final List<MethodSignature.Parameter> parameters;
226 private final AccessModifier modifier;
228 public GeneratedPropertyImpl(final String name, final String comment,
229 final List<AnnotationTypeBuilder> annotationBuilders, final Type parent, final Type returnType,
230 final boolean isFinal, final boolean isReadOnly,
231 final List<Parameter> parameters, final AccessModifier modifier) {
234 this.annotations = new ArrayList<AnnotationType>();
235 for (final AnnotationTypeBuilder builder : annotationBuilders) {
236 this.annotations.add(builder.toInstance());
238 this.annotations = Collections.unmodifiableList(this.annotations);
239 this.comment = comment;
240 this.parent = parent;
241 this.returnType = returnType;
242 this.isFinal = isFinal;
243 this.isReadOnly = isReadOnly;
244 this.parameters = Collections.unmodifiableList(parameters);
245 this.modifier = modifier;
249 public String getName() {
254 public String getComment() {
259 public Type getDefiningType() {
264 public List<AnnotationType> getAnnotations() {
269 public Type getReturnType() {
274 public List<Parameter> getParameters() {
279 public AccessModifier getAccessModifier() {
284 public boolean isReadOnly() {
289 public boolean isFinal() {
296 public int hashCode() {
297 final int prime = 31;
299 result = prime * result + ((name == null) ? 0 : name.hashCode());
300 result = prime * result
301 + ((parameters == null) ? 0 : parameters.hashCode());
302 result = prime * result
303 + ((returnType == null) ? 0 : returnType.hashCode());
308 public boolean equals(Object obj) {
315 if (getClass() != obj.getClass()) {
318 GeneratedPropertyImpl other = (GeneratedPropertyImpl) obj;
320 if (other.name != null) {
323 } else if (!name.equals(other.name)) {
326 if (parameters == null) {
327 if (other.parameters != null) {
330 } else if (!parameters.equals(other.parameters)) {
333 if (returnType == null) {
334 if (other.returnType != null) {
337 } else if (!returnType.getPackageName().equals(other.returnType.getPackageName())) {
339 } else if (!returnType.getName().equals(other.returnType.getName())) {
346 public String toString() {
347 StringBuilder builder = new StringBuilder();
348 builder.append("GeneratedPropertyImpl [name=");
349 builder.append(name);
350 builder.append(", annotations=");
351 builder.append(annotations);
352 builder.append(", comment=");
353 builder.append(comment);
354 if (parent != null) {
355 builder.append(", parent=");
356 builder.append(parent.getPackageName());
358 builder.append(parent.getName());
360 builder.append(", parent=null");
362 builder.append(", returnType=");
363 builder.append(returnType);
364 builder.append(", isFinal=");
365 builder.append(isFinal);
366 builder.append(", isReadOnly=");
367 builder.append(isReadOnly);
368 builder.append(", parameters=");
369 builder.append(parameters);
370 builder.append(", modifier=");
371 builder.append(modifier);
373 return builder.toString();
377 private static final class GeneratedTransferObjectImpl implements
378 GeneratedTransferObject {
380 private final String packageName;
381 private final String name;
382 private final String comment;
383 private final List<Constant> constants;
384 private final List<Enumeration> enumerations;
385 private final List<GeneratedProperty> properties;
386 private final List<GeneratedProperty> equalsProperties;
387 private final List<GeneratedProperty> hashCodeProperties;
388 private final List<GeneratedProperty> stringProperties;
389 private final List<AnnotationType> annotations;
390 private final List<MethodSignature> methods;
392 public GeneratedTransferObjectImpl(final String packageName,
394 final String comment,
395 final List<AnnotationTypeBuilder> annotationBuilders,
396 final List<ConstantBuilder> constantBuilders,
397 final List<EnumBuilder> enumBuilders,
398 final List<MethodSignatureBuilder> methodBuilders,
399 final List<GeneratedPropertyBuilder> propBuilers,
400 final List<GeneratedPropertyBuilder> equalsBuilers,
401 final List<GeneratedPropertyBuilder> hashCodeBuilers,
402 final List<GeneratedPropertyBuilder> stringBuilers) {
404 this.packageName = packageName;
406 this.comment = comment;
407 this.annotations = toUnmodifiableAnnotations(annotationBuilders);
408 this.constants = toUnmodifiableConstant(constantBuilders);
409 this.enumerations = toUnmodifiableEnumerations(enumBuilders);
410 this.properties = toUnmodifiableProperties(propBuilers);
411 this.methods = toUnmodifiableMethods(methodBuilders);
412 this.equalsProperties = toUnmodifiableProperties(equalsBuilers);
413 this.hashCodeProperties = toUnmodifiableProperties(hashCodeBuilers);
414 this.stringProperties = toUnmodifiableProperties(stringBuilers);
417 private List<AnnotationType> toUnmodifiableAnnotations(
418 final List<AnnotationTypeBuilder> annotationBuilders) {
419 final List<AnnotationType> annotations = new ArrayList<AnnotationType>();
420 for (final AnnotationTypeBuilder builder : annotationBuilders) {
421 annotations.add(builder.toInstance());
423 return Collections.unmodifiableList(annotations);
426 private List<Enumeration> toUnmodifiableEnumerations(
427 final List<EnumBuilder> enumBuilders) {
428 final List<Enumeration> enumerations = new ArrayList<Enumeration>();
429 for (final EnumBuilder builder : enumBuilders) {
430 enumerations.add(builder.toInstance(this));
432 return Collections.unmodifiableList(enumerations);
435 private List<Constant> toUnmodifiableConstant(
436 final List<ConstantBuilder> constBuilders) {
437 final List<Constant> constants = new ArrayList<Constant>();
438 for (final ConstantBuilder builder : constBuilders) {
439 constants.add(builder.toInstance(this));
441 return Collections.unmodifiableList(constants);
444 private List<MethodSignature> toUnmodifiableMethods(
445 final List<MethodSignatureBuilder> methodBuilders) {
446 final List<MethodSignature> methods = new ArrayList<MethodSignature>();
447 for (final MethodSignatureBuilder builder : methodBuilders) {
448 methods.add(builder.toInstance(this));
450 return Collections.unmodifiableList(methods);
453 private List<GeneratedProperty> toUnmodifiableProperties(
454 final List<GeneratedPropertyBuilder> propBuilders) {
455 final List<GeneratedProperty> constants = new ArrayList<GeneratedProperty>();
456 for (final GeneratedPropertyBuilder builder : propBuilders) {
457 constants.add(builder.toInstance(this));
459 return Collections.unmodifiableList(constants);
463 public String getPackageName() {
468 public String getName() {
473 public Type getParentType() {
478 public String getComment() {
483 public List<AnnotationType> getAnnotations() {
488 public List<Enumeration> getEnumDefintions() {
493 public List<Constant> getConstantDefinitions() {
498 public List<MethodSignature> getMethodDefinitions() {
503 public List<GeneratedProperty> getProperties() {
508 public List<GeneratedProperty> getEqualsIdentifiers() {
509 return equalsProperties;
513 public List<GeneratedProperty> getHashCodeIdentifiers() {
514 return hashCodeProperties;
518 public List<GeneratedProperty> getToStringIdentifiers() {
519 return stringProperties;
523 public int hashCode() {
524 final int prime = 31;
526 result = prime * result + ((name == null) ? 0 : name.hashCode());
527 result = prime * result
528 + ((packageName == null) ? 0 : packageName.hashCode());
533 public boolean equals(Object obj) {
540 if (getClass() != obj.getClass()) {
543 GeneratedTransferObjectImpl other = (GeneratedTransferObjectImpl) obj;
545 if (other.name != null) {
548 } else if (!name.equals(other.name)) {
551 if (packageName == null) {
552 if (other.packageName != null) {
555 } else if (!packageName.equals(other.packageName)) {
562 public String toString() {
563 StringBuilder builder = new StringBuilder();
564 builder.append("GeneratedTransferObject [packageName=");
565 builder.append(packageName);
566 builder.append(", name=");
567 builder.append(name);
568 builder.append(", comment=");
569 builder.append(comment);
570 builder.append(", constants=");
571 builder.append(constants);
572 builder.append(", enumerations=");
573 builder.append(enumerations);
574 builder.append(", properties=");
575 builder.append(properties);
576 builder.append(", equalsProperties=");
577 builder.append(equalsProperties);
578 builder.append(", hashCodeProperties=");
579 builder.append(hashCodeProperties);
580 builder.append(", stringProperties=");
581 builder.append(stringProperties);
582 builder.append(", annotations=");
583 builder.append(annotations);
584 builder.append(", methods=");
585 builder.append(methods);
587 return builder.toString();