2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
\r
4 * This program and the accompanying materials are made available under the
\r
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
\r
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
\r
8 package org.opendaylight.controller.sal.binding.generator.impl;
\r
10 import java.util.ArrayList;
\r
11 import java.util.Arrays;
\r
12 import java.util.Collections;
\r
13 import java.util.HashSet;
\r
14 import java.util.List;
\r
15 import java.util.Set;
\r
17 import org.opendaylight.controller.sal.binding.model.api.AccessModifier;
\r
18 import org.opendaylight.controller.sal.binding.model.api.Constant;
\r
19 import org.opendaylight.controller.sal.binding.model.api.Enumeration;
\r
20 import org.opendaylight.controller.sal.binding.model.api.GeneratedType;
\r
21 import org.opendaylight.controller.sal.binding.model.api.MethodSignature;
\r
22 import org.opendaylight.controller.sal.binding.model.api.Type;
\r
23 import org.opendaylight.controller.sal.binding.model.api.type.builder.ConstantBuilder;
\r
24 import org.opendaylight.controller.sal.binding.model.api.type.builder.EnumBuilder;
\r
25 import org.opendaylight.controller.sal.binding.model.api.type.builder.GeneratedTypeBuilder;
\r
26 import org.opendaylight.controller.sal.binding.model.api.type.builder.MethodSignatureBuilder;
\r
28 public final class GeneratedTypeBuilderImpl implements GeneratedTypeBuilder {
\r
30 private static final String[] SET_VALUES = new String[] { "abstract",
\r
31 "assert", "boolean", "break", "byte", "case", "catch", "char",
\r
32 "class", "const", "continue", "default", "double", "do", "else",
\r
33 "enum", "extends", "false", "final", "finally", "float", "for",
\r
34 "goto", "if", "implements", "import", "instanceof", "int",
\r
35 "interface", "long", "native", "new", "null", "package", "private",
\r
36 "protected", "public", "return", "short", "static", "strictfp",
\r
37 "super", "switch", "synchronized", "this", "throw", "throws",
\r
38 "transient", "true", "try", "void", "volatile", "while" };
\r
40 public static final Set<String> JAVA_RESERVED_WORDS = new HashSet<String>(
\r
41 Arrays.asList(SET_VALUES));
\r
43 private String packageName;
\r
44 private String comment;
\r
45 private final String name;
\r
46 private final List<EnumBuilder> enumDefinitions = new ArrayList<EnumBuilder>();
\r
47 private final List<ConstantBuilder> constantDefintions = new ArrayList<ConstantBuilder>();
\r
48 private final List<MethodSignatureBuilder> methodDefinitions = new ArrayList<MethodSignatureBuilder>();
\r
50 public GeneratedTypeBuilderImpl(final String packageName, final String name) {
\r
51 this.packageName = validatePackage(packageName);
\r
55 public static String validatePackage(final String packageName) {
\r
56 if (packageName != null) {
\r
57 final String[] packNameParts = packageName.split("\\.");
\r
58 if (packNameParts != null) {
\r
59 final StringBuilder builder = new StringBuilder();
\r
60 for (int i = 0; i < packNameParts.length; ++i) {
\r
61 if (JAVA_RESERVED_WORDS.contains(packNameParts[i])) {
\r
62 packNameParts[i] = "_" + packNameParts[i];
\r
65 builder.append(".");
\r
68 builder.append(packNameParts[i]);
\r
70 return builder.toString();
\r
77 public Type getParentType() {
\r
82 public String getPackageName() {
\r
87 public String getName() {
\r
92 public void addComment(String comment) {
\r
93 this.comment = comment;
\r
97 public ConstantBuilder addConstant(Type type, String name, Object value) {
\r
98 final ConstantBuilder builder = new ConstantBuilderImpl(type, name,
\r
100 constantDefintions.add(builder);
\r
106 public EnumBuilder addEnumeration(final String name) {
\r
107 final EnumBuilder builder = new EnumerationBuilderImpl(packageName,
\r
109 enumDefinitions.add(builder);
\r
114 public MethodSignatureBuilder addMethod(final String name) {
\r
115 final MethodSignatureBuilder builder = new MethodSignatureBuilderImpl(
\r
117 methodDefinitions.add(builder);
\r
122 public GeneratedType toInstance() {
\r
123 packageName = (packageName);
\r
125 return new GeneratedTypeImpl(this, packageName, name, enumDefinitions,
\r
126 constantDefintions, methodDefinitions);
\r
129 private static final class MethodSignatureBuilderImpl implements
\r
130 MethodSignatureBuilder {
\r
131 private final String name;
\r
132 private Type returnType;
\r
133 private final List<MethodSignature.Parameter> parameters;
\r
134 private String comment = "";
\r
135 private final Type parent;
\r
137 public MethodSignatureBuilderImpl(final Type parent, final String name) {
\r
140 this.parent = parent;
\r
141 parameters = new ArrayList<MethodSignature.Parameter>();
\r
142 // TODO: move implementation elsewhere!
\r
147 public void addReturnType(Type returnType) {
\r
148 if (returnType != null) {
\r
149 this.returnType = returnType;
\r
154 public void addParameter(Type type, String name) {
\r
155 parameters.add(new MethodParameterImpl(name, type));
\r
159 public void addComment(String comment) {
\r
160 this.comment = comment;
\r
164 public MethodSignature toInstance(Type definingType) {
\r
165 return new MethodSignatureImpl(definingType, name, comment,
\r
166 returnType, parameters);
\r
170 public int hashCode() {
\r
171 final int prime = 31;
\r
173 result = prime * result + ((name == null) ? 0 : name.hashCode());
\r
178 public boolean equals(Object obj) {
\r
185 if (getClass() != obj.getClass()) {
\r
188 MethodSignatureBuilderImpl other = (MethodSignatureBuilderImpl) obj;
\r
189 if (name == null) {
\r
190 if (other.name != null) {
\r
193 } else if (!name.equals(other.name)) {
\r
200 public String toString() {
\r
201 StringBuilder builder = new StringBuilder();
\r
202 builder.append("MethodBuilderImpl [name=");
\r
203 builder.append(name);
\r
204 builder.append(", returnType=");
\r
205 builder.append(returnType);
\r
206 builder.append(", parameters=");
\r
207 builder.append(parameters);
\r
208 builder.append(", comment=");
\r
209 builder.append(comment);
\r
210 builder.append(", parent=");
\r
211 builder.append(parent.getName());
\r
212 builder.append("]");
\r
213 return builder.toString();
\r
218 private static final class MethodSignatureImpl implements MethodSignature {
\r
220 private final String name;
\r
221 private final String comment;
\r
222 private final Type definingType;
\r
223 private final Type returnType;
\r
224 private final List<Parameter> params;
\r
226 public MethodSignatureImpl(final Type definingType, final String name,
\r
227 final String comment, final Type returnType,
\r
228 final List<Parameter> params) {
\r
231 this.comment = comment;
\r
232 this.definingType = definingType;
\r
233 this.returnType = returnType;
\r
234 this.params = Collections.unmodifiableList(params);
\r
238 public String getName() {
\r
243 public String getComment() {
\r
248 public Type getDefiningType() {
\r
249 return definingType;
\r
253 public Type getReturnType() {
\r
258 public List<Parameter> getParameters() {
\r
263 public AccessModifier getAccessModifier() {
\r
264 return AccessModifier.PUBLIC;
\r
268 public int hashCode() {
\r
269 final int prime = 31;
\r
271 result = prime * result
\r
272 + ((comment == null) ? 0 : comment.hashCode());
\r
273 result = prime * result + ((name == null) ? 0 : name.hashCode());
\r
274 result = prime * result
\r
275 + ((params == null) ? 0 : params.hashCode());
\r
276 result = prime * result
\r
277 + ((returnType == null) ? 0 : returnType.hashCode());
\r
279 if (definingType != null) {
\r
282 + ((definingType.getPackageName() == null) ? 0
\r
283 : definingType.getPackageName().hashCode());
\r
286 + ((definingType.getName() == null) ? 0 : definingType
\r
287 .getName().hashCode());
\r
294 public boolean equals(Object obj) {
\r
301 if (getClass() != obj.getClass()) {
\r
304 MethodSignatureImpl other = (MethodSignatureImpl) obj;
\r
305 if (comment == null) {
\r
306 if (other.comment != null) {
\r
309 } else if (!comment.equals(other.comment)) {
\r
312 if (name == null) {
\r
313 if (other.name != null) {
\r
316 } else if (!name.equals(other.name)) {
\r
319 if (params == null) {
\r
320 if (other.params != null) {
\r
323 } else if (!params.equals(other.params)) {
\r
326 if (definingType == null) {
\r
327 if (other.definingType != null) {
\r
330 } else if ((definingType != null) && (other.definingType != null)) {
\r
331 if (!definingType.getPackageName().equals(
\r
332 other.definingType.getPackageName())
\r
333 && !definingType.getName().equals(
\r
334 other.definingType.getName())) {
\r
338 if (returnType == null) {
\r
339 if (other.returnType != null) {
\r
342 } else if (!returnType.equals(other.returnType)) {
\r
349 public String toString() {
\r
350 StringBuilder builder = new StringBuilder();
\r
351 builder.append("MethodImpl [name=");
\r
352 builder.append(name);
\r
353 builder.append(", comment=");
\r
354 builder.append(comment);
\r
355 if (definingType != null) {
\r
356 builder.append(", definingType=");
\r
357 builder.append(definingType.getPackageName());
\r
358 builder.append(".");
\r
359 builder.append(definingType.getName());
\r
361 builder.append(", definingType= null");
\r
363 builder.append(", returnType=");
\r
364 builder.append(returnType);
\r
365 builder.append(", params=");
\r
366 builder.append(params);
\r
367 builder.append("]");
\r
368 return builder.toString();
\r
372 private static final class GeneratedTypeImpl implements GeneratedType {
\r
374 private final Type parent;
\r
375 private final String packageName;
\r
376 private final String name;
\r
377 private final List<Enumeration> enumDefinitions;
\r
378 private final List<Constant> constantDefintions;
\r
379 private final List<MethodSignature> methodDefinitions;
\r
381 public GeneratedTypeImpl(final Type parent, final String packageName,
\r
382 final String name, final List<EnumBuilder> enumBuilders,
\r
383 final List<ConstantBuilder> constantBuilders,
\r
384 final List<MethodSignatureBuilder> methodBuilders) {
\r
386 this.parent = parent;
\r
387 this.packageName = packageName;
\r
390 this.constantDefintions = toUnmodifiableConstants(constantBuilders);
\r
391 this.enumDefinitions = toUnmodifiableEnums(enumBuilders);
\r
392 this.methodDefinitions = toUnmodifiableMethods(methodBuilders);
\r
395 private List<MethodSignature> toUnmodifiableMethods(
\r
396 List<MethodSignatureBuilder> methodBuilders) {
\r
397 final List<MethodSignature> methods = new ArrayList<MethodSignature>();
\r
398 for (final MethodSignatureBuilder methodBuilder : methodBuilders) {
\r
399 methods.add(methodBuilder.toInstance(this));
\r
401 return Collections.unmodifiableList(methods);
\r
404 private List<Enumeration> toUnmodifiableEnums(
\r
405 List<EnumBuilder> enumBuilders) {
\r
406 final List<Enumeration> enums = new ArrayList<Enumeration>();
\r
407 for (final EnumBuilder enumBuilder : enumBuilders) {
\r
408 enums.add(enumBuilder.toInstance(this));
\r
410 return Collections.unmodifiableList(enums);
\r
413 private List<Constant> toUnmodifiableConstants(
\r
414 List<ConstantBuilder> constantBuilders) {
\r
415 final List<Constant> constants = new ArrayList<Constant>();
\r
416 for (final ConstantBuilder enumBuilder : constantBuilders) {
\r
417 constants.add(enumBuilder.toInstance(this));
\r
419 return Collections.unmodifiableList(constants);
\r
423 public String getPackageName() {
\r
424 return packageName;
\r
428 public String getName() {
\r
433 public Type getParentType() {
\r
438 public List<Enumeration> getEnumDefintions() {
\r
439 return enumDefinitions;
\r
443 public List<Constant> getConstantDefinitions() {
\r
444 return constantDefintions;
\r
448 public List<MethodSignature> getMethodDefinitions() {
\r
449 return methodDefinitions;
\r
453 public int hashCode() {
\r
454 final int prime = 31;
\r
458 + ((constantDefintions == null) ? 0 : constantDefintions
\r
462 + ((enumDefinitions == null) ? 0 : enumDefinitions
\r
466 + ((methodDefinitions == null) ? 0 : methodDefinitions
\r
468 result = prime * result + ((name == null) ? 0 : name.hashCode());
\r
469 result = prime * result
\r
470 + ((packageName == null) ? 0 : packageName.hashCode());
\r
475 public boolean equals(Object obj) {
\r
482 if (getClass() != obj.getClass()) {
\r
485 GeneratedTypeImpl other = (GeneratedTypeImpl) obj;
\r
486 if (constantDefintions == null) {
\r
487 if (other.constantDefintions != null) {
\r
490 } else if (!constantDefintions.equals(other.constantDefintions)) {
\r
493 if (enumDefinitions == null) {
\r
494 if (other.enumDefinitions != null) {
\r
497 } else if (!enumDefinitions.equals(other.enumDefinitions)) {
\r
500 if (methodDefinitions == null) {
\r
501 if (other.methodDefinitions != null) {
\r
504 } else if (!methodDefinitions.equals(other.methodDefinitions)) {
\r
507 if (name == null) {
\r
508 if (other.name != null) {
\r
511 } else if (!name.equals(other.name)) {
\r
514 if (packageName == null) {
\r
515 if (other.packageName != null) {
\r
518 } else if (!packageName.equals(other.packageName)) {
\r
525 public String toString() {
\r
526 StringBuilder builder = new StringBuilder();
\r
527 builder.append("GeneratedTypeImpl [parent=");
\r
528 builder.append(parent.getName());
\r
529 builder.append(", packageName=");
\r
530 builder.append(packageName);
\r
531 builder.append(", name=");
\r
532 builder.append(name);
\r
533 builder.append(", enumDefinitions=");
\r
534 builder.append(enumDefinitions);
\r
535 builder.append(", constantDefintions=");
\r
536 builder.append(constantDefintions);
\r
537 builder.append(", methodDefinitions=");
\r
538 builder.append(methodDefinitions);
\r
539 builder.append("]");
\r
540 return builder.toString();
\r