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<GeneratedType> implementsTypes = new ArrayList<GeneratedType>();
33 private final List<EnumBuilder> enumDefinitions = new ArrayList<EnumBuilder>();
34 private final List<ConstantBuilder> constantDefintions = new ArrayList<ConstantBuilder>();
35 private final List<MethodSignatureBuilder> methodDefinitions = new ArrayList<MethodSignatureBuilder>();
37 public GeneratedTypeBuilderImpl(final String packageName, final String name) {
38 this.packageName = packageName;
43 public Type getParentType() {
48 public String getPackageName() {
53 public String getName() {
58 public void addComment(String comment) {
59 this.comment = comment;
63 public AnnotationTypeBuilder addAnnotation(String packageName, String name) {
64 if (packageName != null && name != null) {
65 final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(
67 if (annotationBuilders.add(builder)) {
75 public ConstantBuilder addConstant(Type type, String name, Object value) {
76 final ConstantBuilder builder = new ConstantBuilderImpl(type, name,
78 constantDefintions.add(builder);
83 public EnumBuilder addEnumeration(final String name) {
84 final String innerPackageName = packageName + "." + this.name;
85 final EnumBuilder builder = new EnumerationBuilderImpl(
86 innerPackageName, name);
87 enumDefinitions.add(builder);
92 public MethodSignatureBuilder addMethod(final String name) {
93 final MethodSignatureBuilder builder = new MethodSignatureBuilderImpl(
95 methodDefinitions.add(builder);
100 public boolean addImplementsType(final GeneratedType genType) {
101 if (genType != null) {
102 return implementsTypes.add(genType);
108 public GeneratedType toInstance() {
109 return new GeneratedTypeImpl(this, packageName, name, comment,
110 annotationBuilders, implementsTypes, enumDefinitions, constantDefintions,
114 private static final class GeneratedTypeImpl implements GeneratedType {
116 private final Type parent;
117 private final String packageName;
118 private final String name;
119 private final String comment;
120 private final List<AnnotationType> annotations;
121 private final List<GeneratedType> implementsTypes;
122 private final List<Enumeration> enumDefinitions;
123 private final List<Constant> constantDefintions;
124 private final List<MethodSignature> methodDefinitions;
126 public GeneratedTypeImpl(final Type parent, final String packageName,
127 final String name, final String comment,
128 final List<AnnotationTypeBuilder> annotationBuilders,
129 final List<GeneratedType> implementsTypes,
130 final List<EnumBuilder> enumBuilders,
131 final List<ConstantBuilder> constantBuilders,
132 final List<MethodSignatureBuilder> methodBuilders) {
134 this.parent = parent;
135 this.packageName = packageName;
137 this.comment = comment;
138 this.annotations = toUnmodifiableAnnotations(annotationBuilders);
139 this.implementsTypes = Collections.unmodifiableList(implementsTypes);
140 this.constantDefintions = toUnmodifiableConstants(constantBuilders);
141 this.enumDefinitions = toUnmodifiableEnums(enumBuilders);
142 this.methodDefinitions = toUnmodifiableMethods(methodBuilders);
145 private List<AnnotationType> toUnmodifiableAnnotations(
146 final List<AnnotationTypeBuilder> annotationBuilders) {
147 final List<AnnotationType> annotations = new ArrayList<AnnotationType>();
148 for (final AnnotationTypeBuilder builder : annotationBuilders) {
149 annotations.add(builder.toInstance());
151 return Collections.unmodifiableList(annotations);
154 private List<MethodSignature> toUnmodifiableMethods(
155 List<MethodSignatureBuilder> methodBuilders) {
156 final List<MethodSignature> methods = new ArrayList<MethodSignature>();
157 for (final MethodSignatureBuilder methodBuilder : methodBuilders) {
158 methods.add(methodBuilder.toInstance(this));
160 return Collections.unmodifiableList(methods);
163 private List<Enumeration> toUnmodifiableEnums(
164 List<EnumBuilder> enumBuilders) {
165 final List<Enumeration> enums = new ArrayList<Enumeration>();
166 for (final EnumBuilder enumBuilder : enumBuilders) {
167 enums.add(enumBuilder.toInstance(this));
169 return Collections.unmodifiableList(enums);
172 private List<Constant> toUnmodifiableConstants(
173 List<ConstantBuilder> constantBuilders) {
174 final List<Constant> constants = new ArrayList<Constant>();
175 for (final ConstantBuilder enumBuilder : constantBuilders) {
176 constants.add(enumBuilder.toInstance(this));
178 return Collections.unmodifiableList(constants);
182 public String getPackageName() {
187 public String getName() {
192 public Type getParentType() {
197 public String getComment() {
202 public List<AnnotationType> getAnnotations() {
207 public List<GeneratedType> getImplements() {
208 return implementsTypes;
212 public List<Enumeration> getEnumDefintions() {
213 return enumDefinitions;
217 public List<Constant> getConstantDefinitions() {
218 return constantDefintions;
222 public List<MethodSignature> getMethodDefinitions() {
223 return methodDefinitions;
227 public int hashCode() {
228 final int prime = 31;
232 + ((constantDefintions == null) ? 0 : constantDefintions
236 + ((enumDefinitions == null) ? 0 : enumDefinitions
240 + ((methodDefinitions == null) ? 0 : methodDefinitions
242 result = prime * result + ((name == null) ? 0 : name.hashCode());
243 result = prime * result
244 + ((packageName == null) ? 0 : packageName.hashCode());
249 public boolean equals(Object obj) {
256 if (getClass() != obj.getClass()) {
259 GeneratedTypeImpl other = (GeneratedTypeImpl) obj;
260 if (constantDefintions == null) {
261 if (other.constantDefintions != null) {
264 } else if (!constantDefintions.equals(other.constantDefintions)) {
267 if (enumDefinitions == null) {
268 if (other.enumDefinitions != null) {
271 } else if (!enumDefinitions.equals(other.enumDefinitions)) {
274 if (methodDefinitions == null) {
275 if (other.methodDefinitions != null) {
278 } else if (!methodDefinitions.equals(other.methodDefinitions)) {
282 if (other.name != null) {
285 } else if (!name.equals(other.name)) {
288 if (packageName == null) {
289 if (other.packageName != null) {
292 } else if (!packageName.equals(other.packageName)) {
299 public String toString() {
300 StringBuilder builder = new StringBuilder();
301 builder.append("GeneratedType [packageName=");
302 builder.append(packageName);
303 builder.append(", name=");
304 builder.append(name);
305 if (parent != null) {
306 builder.append(", parent=");
307 builder.append(parent.getPackageName());
309 builder.append(parent.getName());
311 builder.append(", parent=null");
313 builder.append(", comment=");
314 builder.append(comment);
315 builder.append(", annotations=");
316 builder.append(annotations);
317 builder.append(", enumDefinitions=");
318 builder.append(enumDefinitions);
319 builder.append(", constantDefintions=");
320 builder.append(constantDefintions);
321 builder.append(", methodDefinitions=");
322 builder.append(methodDefinitions);
324 return builder.toString();