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 EnumBuilder builder = new EnumerationBuilderImpl(packageName,
72 enumDefinitions.add(builder);
77 public MethodSignatureBuilder addMethod(final String name) {
78 final MethodSignatureBuilder builder = new MethodSignatureBuilderImpl(
80 methodDefinitions.add(builder);
85 public GeneratedType toInstance() {
86 return new GeneratedTypeImpl(this, packageName, name, enumDefinitions,
87 constantDefintions, methodDefinitions);
90 private static final class MethodSignatureBuilderImpl implements
91 MethodSignatureBuilder {
92 private final String name;
93 private Type returnType;
94 private final List<MethodSignature.Parameter> parameters;
95 private String comment = "";
96 private final Type parent;
98 public MethodSignatureBuilderImpl(final Type parent, final String name) {
101 this.parent = parent;
102 parameters = new ArrayList<MethodSignature.Parameter>();
103 //TODO: move implementation elsewhere!
108 public void addReturnType(Type returnType) {
109 if (returnType != null) {
110 this.returnType = returnType;
115 public void addParameter(Type type, String name) {
116 parameters.add(new MethodParameterImpl(name, type));
120 public void addComment(String comment) {
121 this.comment = comment;
125 public MethodSignature toInstance(Type definingType) {
126 return new MethodSignatureImpl(definingType, name, comment,
127 returnType, parameters);
131 public int hashCode() {
132 final int prime = 31;
134 result = prime * result + ((name == null) ? 0 : name.hashCode());
139 public boolean equals(Object obj) {
146 if (getClass() != obj.getClass()) {
149 MethodSignatureBuilderImpl other = (MethodSignatureBuilderImpl) obj;
151 if (other.name != null) {
154 } else if (!name.equals(other.name)) {
161 public String toString() {
162 StringBuilder builder = new StringBuilder();
163 builder.append("MethodBuilderImpl [name=");
164 builder.append(name);
165 builder.append(", returnType=");
166 builder.append(returnType);
167 builder.append(", parameters=");
168 builder.append(parameters);
169 builder.append(", comment=");
170 builder.append(comment);
171 builder.append(", parent=");
172 builder.append(parent.getName());
174 return builder.toString();
179 private static final class MethodSignatureImpl implements MethodSignature {
181 private final String name;
182 private final String comment;
183 private final Type definingType;
184 private final Type returnType;
185 private final List<Parameter> params;
187 public MethodSignatureImpl(final Type definingType, final String name,
188 final String comment, final Type returnType,
189 final List<Parameter> params) {
192 this.comment = comment;
193 this.definingType = definingType;
194 this.returnType = returnType;
195 this.params = Collections.unmodifiableList(params);
199 public String getName() {
204 public String getComment() {
209 public Type getDefiningType() {
214 public Type getReturnType() {
219 public List<Parameter> getParameters() {
224 public AccessModifier getAccessModifier() {
225 return AccessModifier.PUBLIC;
229 public int hashCode() {
230 final int prime = 31;
232 result = prime * result
233 + ((comment == null) ? 0 : comment.hashCode());
234 result = prime * result + ((name == null) ? 0 : name.hashCode());
235 result = prime * result
236 + ((params == null) ? 0 : params.hashCode());
237 result = prime * result
238 + ((returnType == null) ? 0 : returnType.hashCode());
240 if (definingType != null) {
243 + ((definingType.getPackageName() == null) ? 0
244 : definingType.getPackageName().hashCode());
247 + ((definingType.getName() == null) ? 0 : definingType
248 .getName().hashCode());
255 public boolean equals(Object obj) {
262 if (getClass() != obj.getClass()) {
265 MethodSignatureImpl other = (MethodSignatureImpl) obj;
266 if (comment == null) {
267 if (other.comment != null) {
270 } else if (!comment.equals(other.comment)) {
274 if (other.name != null) {
277 } else if (!name.equals(other.name)) {
280 if (params == null) {
281 if (other.params != null) {
284 } else if (!params.equals(other.params)) {
287 if (definingType == null) {
288 if (other.definingType != null) {
291 } else if ((definingType != null) && (other.definingType != null)) {
292 if (!definingType.getPackageName().equals(
293 other.definingType.getPackageName())
294 && !definingType.getName().equals(
295 other.definingType.getName())) {
299 if (returnType == null) {
300 if (other.returnType != null) {
303 } else if (!returnType.equals(other.returnType)) {
310 public String toString() {
311 StringBuilder builder = new StringBuilder();
312 builder.append("MethodImpl [name=");
313 builder.append(name);
314 builder.append(", comment=");
315 builder.append(comment);
316 if (definingType != null) {
317 builder.append(", definingType=");
318 builder.append(definingType.getPackageName());
320 builder.append(definingType.getName());
322 builder.append(", definingType= null");
324 builder.append(", returnType=");
325 builder.append(returnType);
326 builder.append(", params=");
327 builder.append(params);
329 return builder.toString();
333 private static final class GeneratedTypeImpl implements GeneratedType {
335 private final Type parent;
336 private final String packageName;
337 private final String name;
338 private final List<Enumeration> enumDefinitions;
339 private final List<Constant> constantDefintions;
340 private final List<MethodSignature> methodDefinitions;
342 public GeneratedTypeImpl(final Type parent, final String packageName,
343 final String name, final List<EnumBuilder> enumBuilders,
344 final List<ConstantBuilder> constantBuilders,
345 final List<MethodSignatureBuilder> methodBuilders) {
347 this.parent = parent;
348 this.packageName = packageName;
351 this.constantDefintions = toUnmodifiableConstants(constantBuilders);
352 this.enumDefinitions = toUnmodifiableEnums(enumBuilders);
353 this.methodDefinitions = toUnmodifiableMethods(methodBuilders);
356 private List<MethodSignature> toUnmodifiableMethods(
357 List<MethodSignatureBuilder> methodBuilders) {
358 final List<MethodSignature> methods = new ArrayList<MethodSignature>();
359 for (final MethodSignatureBuilder methodBuilder : methodBuilders) {
360 methods.add(methodBuilder.toInstance(this));
362 return Collections.unmodifiableList(methods);
365 private List<Enumeration> toUnmodifiableEnums(
366 List<EnumBuilder> enumBuilders) {
367 final List<Enumeration> enums = new ArrayList<Enumeration>();
368 for (final EnumBuilder enumBuilder : enumBuilders) {
369 enums.add(enumBuilder.toInstance(this));
371 return Collections.unmodifiableList(enums);
374 private List<Constant> toUnmodifiableConstants(
375 List<ConstantBuilder> constantBuilders) {
376 final List<Constant> constants = new ArrayList<Constant>();
377 for (final ConstantBuilder enumBuilder : constantBuilders) {
378 constants.add(enumBuilder.toInstance(this));
380 return Collections.unmodifiableList(constants);
384 public String getPackageName() {
389 public String getName() {
394 public Type getParentType() {
399 public List<Enumeration> getEnumDefintions() {
400 return enumDefinitions;
404 public List<Constant> getConstantDefinitions() {
405 return constantDefintions;
409 public List<MethodSignature> getMethodDefinitions() {
410 return methodDefinitions;
414 public int hashCode() {
415 final int prime = 31;
419 + ((constantDefintions == null) ? 0 : constantDefintions
423 + ((enumDefinitions == null) ? 0 : enumDefinitions
427 + ((methodDefinitions == null) ? 0 : methodDefinitions
429 result = prime * result + ((name == null) ? 0 : name.hashCode());
430 result = prime * result
431 + ((packageName == null) ? 0 : packageName.hashCode());
436 public boolean equals(Object obj) {
443 if (getClass() != obj.getClass()) {
446 GeneratedTypeImpl other = (GeneratedTypeImpl) obj;
447 if (constantDefintions == null) {
448 if (other.constantDefintions != null) {
451 } else if (!constantDefintions.equals(other.constantDefintions)) {
454 if (enumDefinitions == null) {
455 if (other.enumDefinitions != null) {
458 } else if (!enumDefinitions.equals(other.enumDefinitions)) {
461 if (methodDefinitions == null) {
462 if (other.methodDefinitions != null) {
465 } else if (!methodDefinitions.equals(other.methodDefinitions)) {
469 if (other.name != null) {
472 } else if (!name.equals(other.name)) {
475 if (packageName == null) {
476 if (other.packageName != null) {
479 } else if (!packageName.equals(other.packageName)) {
486 public String toString() {
487 StringBuilder builder = new StringBuilder();
488 builder.append("GeneratedTypeImpl [parent=");
489 builder.append(parent.getName());
490 builder.append(", packageName=");
491 builder.append(packageName);
492 builder.append(", name=");
493 builder.append(name);
494 builder.append(", enumDefinitions=");
495 builder.append(enumDefinitions);
496 builder.append(", constantDefintions=");
497 builder.append(constantDefintions);
498 builder.append(", methodDefinitions=");
499 builder.append(methodDefinitions);
501 return builder.toString();