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.sal.binding.generator.impl;
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.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.ConstantBuilder;
21 import org.opendaylight.controller.sal.binding.model.api.type.builder.EnumBuilder;
22 import org.opendaylight.controller.sal.binding.model.api.type.builder.GeneratedTypeBuilder;
23 import org.opendaylight.controller.sal.binding.model.api.type.builder.MethodSignatureBuilder;
25 public final class GeneratedTypeBuilderImpl implements GeneratedTypeBuilder {
27 private final String packageName;
28 private String comment;
29 private final String name;
30 private final List<EnumBuilder> enumDefinitions = new ArrayList<EnumBuilder>();
31 private final List<ConstantBuilder> constantDefintions = new ArrayList<ConstantBuilder>();
32 private final List<MethodSignatureBuilder> methodDefinitions = new ArrayList<MethodSignatureBuilder>();
34 public GeneratedTypeBuilderImpl(final String packageName, final String name) {
35 this.packageName = packageName;
40 public Type getParentType() {
45 public String getPackageName() {
50 public String getName() {
55 public void addComment(String comment) {
56 this.comment = comment;
60 public ConstantBuilder addConstant(Type type, String name, Object value) {
61 final ConstantBuilder builder = new ConstantBuilderImpl(type, name,
63 constantDefintions.add(builder);
69 public EnumBuilder addEnumeration(final String name) {
70 final String innerPackageName = packageName + "." + this.name;
71 final EnumBuilder builder = new EnumerationBuilderImpl(innerPackageName,
73 enumDefinitions.add(builder);
78 public MethodSignatureBuilder addMethod(final String name) {
79 final MethodSignatureBuilder builder = new MethodSignatureBuilderImpl(
81 methodDefinitions.add(builder);
86 public GeneratedType toInstance() {
87 return new GeneratedTypeImpl(this, packageName, name, enumDefinitions,
88 constantDefintions, methodDefinitions);
91 private static final class MethodSignatureBuilderImpl implements
92 MethodSignatureBuilder {
93 private final String name;
94 private Type returnType;
95 private final List<MethodSignature.Parameter> parameters;
96 private String comment = "";
97 private final Type parent;
99 public MethodSignatureBuilderImpl(final Type parent, final String name) {
102 this.parent = parent;
103 parameters = new ArrayList<MethodSignature.Parameter>();
104 // TODO: move implementation elsewhere!
109 public void addReturnType(Type returnType) {
110 if (returnType != null) {
111 this.returnType = returnType;
116 public void addParameter(Type type, String name) {
117 parameters.add(new MethodParameterImpl(name, type));
121 public void addComment(String comment) {
122 this.comment = comment;
126 public MethodSignature toInstance(Type definingType) {
127 return new MethodSignatureImpl(definingType, name, comment,
128 returnType, parameters);
132 public int hashCode() {
133 final int prime = 31;
135 result = prime * result + ((name == null) ? 0 : name.hashCode());
140 public boolean equals(Object obj) {
147 if (getClass() != obj.getClass()) {
150 MethodSignatureBuilderImpl other = (MethodSignatureBuilderImpl) obj;
152 if (other.name != null) {
155 } else if (!name.equals(other.name)) {
162 public String toString() {
163 StringBuilder builder = new StringBuilder();
164 builder.append("MethodBuilderImpl [name=");
165 builder.append(name);
166 builder.append(", returnType=");
167 builder.append(returnType);
168 builder.append(", parameters=");
169 builder.append(parameters);
170 builder.append(", comment=");
171 builder.append(comment);
172 builder.append(", parent=");
173 builder.append(parent.getName());
175 return builder.toString();
180 private static final class MethodSignatureImpl implements MethodSignature {
182 private final String name;
183 private final String comment;
184 private final Type definingType;
185 private final Type returnType;
186 private final List<Parameter> params;
188 public MethodSignatureImpl(final Type definingType, final String name,
189 final String comment, final Type returnType,
190 final List<Parameter> params) {
193 this.comment = comment;
194 this.definingType = definingType;
195 this.returnType = returnType;
196 this.params = Collections.unmodifiableList(params);
200 public String getName() {
205 public String getComment() {
210 public Type getDefiningType() {
215 public Type getReturnType() {
220 public List<Parameter> getParameters() {
225 public AccessModifier getAccessModifier() {
226 return AccessModifier.PUBLIC;
230 public int hashCode() {
231 final int prime = 31;
233 result = prime * result
234 + ((comment == null) ? 0 : comment.hashCode());
235 result = prime * result + ((name == null) ? 0 : name.hashCode());
236 result = prime * result
237 + ((params == null) ? 0 : params.hashCode());
238 result = prime * result
239 + ((returnType == null) ? 0 : returnType.hashCode());
241 if (definingType != null) {
244 + ((definingType.getPackageName() == null) ? 0
245 : definingType.getPackageName().hashCode());
248 + ((definingType.getName() == null) ? 0 : definingType
249 .getName().hashCode());
256 public boolean equals(Object obj) {
263 if (getClass() != obj.getClass()) {
266 MethodSignatureImpl other = (MethodSignatureImpl) obj;
267 if (comment == null) {
268 if (other.comment != null) {
271 } else if (!comment.equals(other.comment)) {
275 if (other.name != null) {
278 } else if (!name.equals(other.name)) {
281 if (params == null) {
282 if (other.params != null) {
285 } else if (!params.equals(other.params)) {
288 if (definingType == null) {
289 if (other.definingType != null) {
292 } else if ((definingType != null) && (other.definingType != null)) {
293 if (!definingType.getPackageName().equals(
294 other.definingType.getPackageName())
295 && !definingType.getName().equals(
296 other.definingType.getName())) {
300 if (returnType == null) {
301 if (other.returnType != null) {
304 } else if (!returnType.equals(other.returnType)) {
311 public String toString() {
312 StringBuilder builder = new StringBuilder();
313 builder.append("MethodImpl [name=");
314 builder.append(name);
315 builder.append(", comment=");
316 builder.append(comment);
317 if (definingType != null) {
318 builder.append(", definingType=");
319 builder.append(definingType.getPackageName());
321 builder.append(definingType.getName());
323 builder.append(", definingType= null");
325 builder.append(", returnType=");
326 builder.append(returnType);
327 builder.append(", params=");
328 builder.append(params);
330 return builder.toString();
334 private static final class GeneratedTypeImpl implements GeneratedType {
336 private final Type parent;
337 private final String packageName;
338 private final String name;
339 private final List<Enumeration> enumDefinitions;
340 private final List<Constant> constantDefintions;
341 private final List<MethodSignature> methodDefinitions;
343 public GeneratedTypeImpl(final Type parent, final String packageName,
344 final String name, final List<EnumBuilder> enumBuilders,
345 final List<ConstantBuilder> constantBuilders,
346 final List<MethodSignatureBuilder> methodBuilders) {
348 this.parent = parent;
349 this.packageName = packageName;
352 this.constantDefintions = toUnmodifiableConstants(constantBuilders);
353 this.enumDefinitions = toUnmodifiableEnums(enumBuilders);
354 this.methodDefinitions = toUnmodifiableMethods(methodBuilders);
357 private List<MethodSignature> toUnmodifiableMethods(
358 List<MethodSignatureBuilder> methodBuilders) {
359 final List<MethodSignature> methods = new ArrayList<MethodSignature>();
360 for (final MethodSignatureBuilder methodBuilder : methodBuilders) {
361 methods.add(methodBuilder.toInstance(this));
363 return Collections.unmodifiableList(methods);
366 private List<Enumeration> toUnmodifiableEnums(
367 List<EnumBuilder> enumBuilders) {
368 final List<Enumeration> enums = new ArrayList<Enumeration>();
369 for (final EnumBuilder enumBuilder : enumBuilders) {
370 enums.add(enumBuilder.toInstance(this));
372 return Collections.unmodifiableList(enums);
375 private List<Constant> toUnmodifiableConstants(
376 List<ConstantBuilder> constantBuilders) {
377 final List<Constant> constants = new ArrayList<Constant>();
378 for (final ConstantBuilder enumBuilder : constantBuilders) {
379 constants.add(enumBuilder.toInstance(this));
381 return Collections.unmodifiableList(constants);
385 public String getPackageName() {
390 public String getName() {
395 public Type getParentType() {
400 public List<Enumeration> getEnumDefintions() {
401 return enumDefinitions;
405 public List<Constant> getConstantDefinitions() {
406 return constantDefintions;
410 public List<MethodSignature> getMethodDefinitions() {
411 return methodDefinitions;
415 public int hashCode() {
416 final int prime = 31;
420 + ((constantDefintions == null) ? 0 : constantDefintions
424 + ((enumDefinitions == null) ? 0 : enumDefinitions
428 + ((methodDefinitions == null) ? 0 : methodDefinitions
430 result = prime * result + ((name == null) ? 0 : name.hashCode());
431 result = prime * result
432 + ((packageName == null) ? 0 : packageName.hashCode());
437 public boolean equals(Object obj) {
444 if (getClass() != obj.getClass()) {
447 GeneratedTypeImpl other = (GeneratedTypeImpl) obj;
448 if (constantDefintions == null) {
449 if (other.constantDefintions != null) {
452 } else if (!constantDefintions.equals(other.constantDefintions)) {
455 if (enumDefinitions == null) {
456 if (other.enumDefinitions != null) {
459 } else if (!enumDefinitions.equals(other.enumDefinitions)) {
462 if (methodDefinitions == null) {
463 if (other.methodDefinitions != null) {
466 } else if (!methodDefinitions.equals(other.methodDefinitions)) {
470 if (other.name != null) {
473 } else if (!name.equals(other.name)) {
476 if (packageName == null) {
477 if (other.packageName != null) {
480 } else if (!packageName.equals(other.packageName)) {
487 public String toString() {
488 StringBuilder builder = new StringBuilder();
489 builder.append("GeneratedTypeImpl [parent=");
490 builder.append(parent.getName());
491 builder.append(", packageName=");
492 builder.append(packageName);
493 builder.append(", name=");
494 builder.append(name);
495 builder.append(", enumDefinitions=");
496 builder.append(enumDefinitions);
497 builder.append(", constantDefintions=");
498 builder.append(constantDefintions);
499 builder.append(", methodDefinitions=");
500 builder.append(methodDefinitions);
502 return builder.toString();