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.AnnotationType;
15 import org.opendaylight.controller.sal.binding.model.api.Constant;
16 import org.opendaylight.controller.sal.binding.model.api.Enumeration;
17 import org.opendaylight.controller.sal.binding.model.api.GeneratedType;
18 import org.opendaylight.controller.sal.binding.model.api.MethodSignature;
19 import org.opendaylight.controller.sal.binding.model.api.Type;
20 import org.opendaylight.controller.sal.binding.model.api.type.builder.AnnotationTypeBuilder;
21 import org.opendaylight.controller.sal.binding.model.api.type.builder.ConstantBuilder;
22 import org.opendaylight.controller.sal.binding.model.api.type.builder.EnumBuilder;
23 import org.opendaylight.controller.sal.binding.model.api.type.builder.GeneratedTypeBuilder;
24 import org.opendaylight.controller.sal.binding.model.api.type.builder.MethodSignatureBuilder;
26 public final class GeneratedTypeBuilderImpl implements GeneratedTypeBuilder {
28 private final String packageName;
29 private String comment = "";
30 private final String name;
31 private final List<AnnotationTypeBuilder> annotationBuilders = new ArrayList<AnnotationTypeBuilder>();
32 private final List<EnumBuilder> enumDefinitions = new ArrayList<EnumBuilder>();
33 private final List<ConstantBuilder> constantDefintions = new ArrayList<ConstantBuilder>();
34 private final List<MethodSignatureBuilder> methodDefinitions = new ArrayList<MethodSignatureBuilder>();
36 public GeneratedTypeBuilderImpl(final String packageName, final String name) {
37 this.packageName = packageName;
42 public Type getParentType() {
47 public String getPackageName() {
52 public String getName() {
57 public void addComment(String comment) {
58 this.comment = comment;
62 public AnnotationTypeBuilder addAnnotation(String packageName, String name) {
63 if (packageName != null && name != null) {
64 final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(
66 if (annotationBuilders.add(builder)) {
74 public ConstantBuilder addConstant(Type type, String name, Object value) {
75 final ConstantBuilder builder = new ConstantBuilderImpl(type, name,
77 constantDefintions.add(builder);
82 public EnumBuilder addEnumeration(final String name) {
83 final String innerPackageName = packageName + "." + this.name;
84 final EnumBuilder builder = new EnumerationBuilderImpl(
85 innerPackageName, name);
86 enumDefinitions.add(builder);
91 public MethodSignatureBuilder addMethod(final String name) {
92 final MethodSignatureBuilder builder = new MethodSignatureBuilderImpl(
94 methodDefinitions.add(builder);
99 public GeneratedType toInstance() {
100 return new GeneratedTypeImpl(this, packageName, name, comment,
101 annotationBuilders, enumDefinitions, constantDefintions,
105 private static final class GeneratedTypeImpl implements GeneratedType {
107 private final Type parent;
108 private final String packageName;
109 private final String name;
110 private final String comment;
111 private final List<AnnotationType> annotations;
112 private final List<Enumeration> enumDefinitions;
113 private final List<Constant> constantDefintions;
114 private final List<MethodSignature> methodDefinitions;
116 public GeneratedTypeImpl(final Type parent, final String packageName,
117 final String name, final String comment,
118 final List<AnnotationTypeBuilder> annotationBuilders,
119 final List<EnumBuilder> enumBuilders,
120 final List<ConstantBuilder> constantBuilders,
121 final List<MethodSignatureBuilder> methodBuilders) {
123 this.parent = parent;
124 this.packageName = packageName;
126 this.comment = comment;
127 this.annotations = toUnmodifiableAnnotations(annotationBuilders);
128 this.constantDefintions = toUnmodifiableConstants(constantBuilders);
129 this.enumDefinitions = toUnmodifiableEnums(enumBuilders);
130 this.methodDefinitions = toUnmodifiableMethods(methodBuilders);
133 private List<AnnotationType> toUnmodifiableAnnotations(
134 final List<AnnotationTypeBuilder> annotationBuilders) {
135 final List<AnnotationType> annotations = new ArrayList<AnnotationType>();
136 for (final AnnotationTypeBuilder builder : annotationBuilders) {
137 annotations.add(builder.toInstance());
139 return Collections.unmodifiableList(annotations);
142 private List<MethodSignature> toUnmodifiableMethods(
143 List<MethodSignatureBuilder> methodBuilders) {
144 final List<MethodSignature> methods = new ArrayList<MethodSignature>();
145 for (final MethodSignatureBuilder methodBuilder : methodBuilders) {
146 methods.add(methodBuilder.toInstance(this));
148 return Collections.unmodifiableList(methods);
151 private List<Enumeration> toUnmodifiableEnums(
152 List<EnumBuilder> enumBuilders) {
153 final List<Enumeration> enums = new ArrayList<Enumeration>();
154 for (final EnumBuilder enumBuilder : enumBuilders) {
155 enums.add(enumBuilder.toInstance(this));
157 return Collections.unmodifiableList(enums);
160 private List<Constant> toUnmodifiableConstants(
161 List<ConstantBuilder> constantBuilders) {
162 final List<Constant> constants = new ArrayList<Constant>();
163 for (final ConstantBuilder enumBuilder : constantBuilders) {
164 constants.add(enumBuilder.toInstance(this));
166 return Collections.unmodifiableList(constants);
170 public String getPackageName() {
175 public String getName() {
180 public Type getParentType() {
185 public String getComment() {
190 public List<AnnotationType> getAnnotations() {
195 public List<Enumeration> getEnumDefintions() {
196 return enumDefinitions;
200 public List<Constant> getConstantDefinitions() {
201 return constantDefintions;
205 public List<MethodSignature> getMethodDefinitions() {
206 return methodDefinitions;
210 public int hashCode() {
211 final int prime = 31;
215 + ((constantDefintions == null) ? 0 : constantDefintions
219 + ((enumDefinitions == null) ? 0 : enumDefinitions
223 + ((methodDefinitions == null) ? 0 : methodDefinitions
225 result = prime * result + ((name == null) ? 0 : name.hashCode());
226 result = prime * result
227 + ((packageName == null) ? 0 : packageName.hashCode());
232 public boolean equals(Object obj) {
239 if (getClass() != obj.getClass()) {
242 GeneratedTypeImpl other = (GeneratedTypeImpl) obj;
243 if (constantDefintions == null) {
244 if (other.constantDefintions != null) {
247 } else if (!constantDefintions.equals(other.constantDefintions)) {
250 if (enumDefinitions == null) {
251 if (other.enumDefinitions != null) {
254 } else if (!enumDefinitions.equals(other.enumDefinitions)) {
257 if (methodDefinitions == null) {
258 if (other.methodDefinitions != null) {
261 } else if (!methodDefinitions.equals(other.methodDefinitions)) {
265 if (other.name != null) {
268 } else if (!name.equals(other.name)) {
271 if (packageName == null) {
272 if (other.packageName != null) {
275 } else if (!packageName.equals(other.packageName)) {
282 public String toString() {
283 StringBuilder builder = new StringBuilder();
284 builder.append("GeneratedType [packageName=");
285 builder.append(packageName);
286 builder.append(", name=");
287 builder.append(name);
288 if (parent != null) {
289 builder.append(", parent=");
290 builder.append(parent.getPackageName());
292 builder.append(parent.getName());
294 builder.append(", parent=null");
296 builder.append(", comment=");
297 builder.append(comment);
298 builder.append(", annotations=");
299 builder.append(annotations);
300 builder.append(", enumDefinitions=");
301 builder.append(enumDefinitions);
302 builder.append(", constantDefintions=");
303 builder.append(constantDefintions);
304 builder.append(", methodDefinitions=");
305 builder.append(methodDefinitions);
307 return builder.toString();