Update the API generation code and code generation sample
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-generator-impl / src / main / java / org / opendaylight / controller / sal / binding / generator / impl / GeneratedTypeBuilderImpl.java
1 /*\r
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
3  *\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
7  */\r
8 package org.opendaylight.controller.sal.binding.generator.impl;\r
9 \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
16 \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
27 \r
28 public final class GeneratedTypeBuilderImpl implements GeneratedTypeBuilder {\r
29 \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
39 \r
40         public static final Set<String> JAVA_RESERVED_WORDS = new HashSet<String>(\r
41                         Arrays.asList(SET_VALUES));\r
42 \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
49 \r
50         public GeneratedTypeBuilderImpl(final String packageName, final String name) {\r
51                 this.packageName = validatePackage(packageName);\r
52                 this.name = name;\r
53         }\r
54 \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
63                                         }\r
64                                         if (i > 0) {\r
65                                                 builder.append(".");\r
66                                         }\r
67 \r
68                                         builder.append(packNameParts[i]);\r
69                                 }\r
70                                 return builder.toString();\r
71                         }\r
72                 }\r
73                 return packageName;\r
74         }\r
75 \r
76         @Override\r
77         public Type getParentType() {\r
78                 return this;\r
79         }\r
80 \r
81         @Override\r
82         public String getPackageName() {\r
83                 return packageName;\r
84         }\r
85 \r
86         @Override\r
87         public String getName() {\r
88                 return name;\r
89         }\r
90 \r
91         @Override\r
92         public void addComment(String comment) {\r
93                 this.comment = comment;\r
94         }\r
95 \r
96         @Override\r
97         public ConstantBuilder addConstant(Type type, String name, Object value) {\r
98                 final ConstantBuilder builder = new ConstantBuilderImpl(type, name,\r
99                                 value);\r
100                 constantDefintions.add(builder);\r
101 \r
102                 return builder;\r
103         }\r
104 \r
105         @Override\r
106         public EnumBuilder addEnumeration(final String name) {\r
107                 final EnumBuilder builder = new EnumerationBuilderImpl(packageName,\r
108                                 name);\r
109                 enumDefinitions.add(builder);\r
110                 return builder;\r
111         }\r
112 \r
113         @Override\r
114         public MethodSignatureBuilder addMethod(final String name) {\r
115                 final MethodSignatureBuilder builder = new MethodSignatureBuilderImpl(\r
116                                 this, name);\r
117                 methodDefinitions.add(builder);\r
118                 return builder;\r
119         }\r
120 \r
121         @Override\r
122         public GeneratedType toInstance() {\r
123                 packageName = (packageName);\r
124 \r
125                 return new GeneratedTypeImpl(this, packageName, name, enumDefinitions,\r
126                                 constantDefintions, methodDefinitions);\r
127         }\r
128 \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
136 \r
137                 public MethodSignatureBuilderImpl(final Type parent, final String name) {\r
138                         super();\r
139                         this.name = name;\r
140                         this.parent = parent;\r
141                         parameters = new ArrayList<MethodSignature.Parameter>();\r
142                         // TODO: move implementation elsewhere!\r
143 \r
144                 }\r
145 \r
146                 @Override\r
147                 public void addReturnType(Type returnType) {\r
148                         if (returnType != null) {\r
149                                 this.returnType = returnType;\r
150                         }\r
151                 }\r
152 \r
153                 @Override\r
154                 public void addParameter(Type type, String name) {\r
155                         parameters.add(new MethodParameterImpl(name, type));\r
156                 }\r
157 \r
158                 @Override\r
159                 public void addComment(String comment) {\r
160                         this.comment = comment;\r
161                 }\r
162 \r
163                 @Override\r
164                 public MethodSignature toInstance(Type definingType) {\r
165                         return new MethodSignatureImpl(definingType, name, comment,\r
166                                         returnType, parameters);\r
167                 }\r
168 \r
169                 @Override\r
170                 public int hashCode() {\r
171                         final int prime = 31;\r
172                         int result = 1;\r
173                         result = prime * result + ((name == null) ? 0 : name.hashCode());\r
174                         return result;\r
175                 }\r
176 \r
177                 @Override\r
178                 public boolean equals(Object obj) {\r
179                         if (this == obj) {\r
180                                 return true;\r
181                         }\r
182                         if (obj == null) {\r
183                                 return false;\r
184                         }\r
185                         if (getClass() != obj.getClass()) {\r
186                                 return false;\r
187                         }\r
188                         MethodSignatureBuilderImpl other = (MethodSignatureBuilderImpl) obj;\r
189                         if (name == null) {\r
190                                 if (other.name != null) {\r
191                                         return false;\r
192                                 }\r
193                         } else if (!name.equals(other.name)) {\r
194                                 return false;\r
195                         }\r
196                         return true;\r
197                 }\r
198 \r
199                 @Override\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
214                 }\r
215 \r
216         }\r
217 \r
218         private static final class MethodSignatureImpl implements MethodSignature {\r
219 \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
225 \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
229                         super();\r
230                         this.name = name;\r
231                         this.comment = comment;\r
232                         this.definingType = definingType;\r
233                         this.returnType = returnType;\r
234                         this.params = Collections.unmodifiableList(params);\r
235                 }\r
236 \r
237                 @Override\r
238                 public String getName() {\r
239                         return name;\r
240                 }\r
241 \r
242                 @Override\r
243                 public String getComment() {\r
244                         return comment;\r
245                 }\r
246 \r
247                 @Override\r
248                 public Type getDefiningType() {\r
249                         return definingType;\r
250                 }\r
251 \r
252                 @Override\r
253                 public Type getReturnType() {\r
254                         return returnType;\r
255                 }\r
256 \r
257                 @Override\r
258                 public List<Parameter> getParameters() {\r
259                         return params;\r
260                 }\r
261 \r
262                 @Override\r
263                 public AccessModifier getAccessModifier() {\r
264                         return AccessModifier.PUBLIC;\r
265                 }\r
266 \r
267                 @Override\r
268                 public int hashCode() {\r
269                         final int prime = 31;\r
270                         int result = 1;\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
278 \r
279                         if (definingType != null) {\r
280                                 result = prime\r
281                                                 * result\r
282                                                 + ((definingType.getPackageName() == null) ? 0\r
283                                                                 : definingType.getPackageName().hashCode());\r
284                                 result = prime\r
285                                                 * result\r
286                                                 + ((definingType.getName() == null) ? 0 : definingType\r
287                                                                 .getName().hashCode());\r
288                         }\r
289 \r
290                         return result;\r
291                 }\r
292 \r
293                 @Override\r
294                 public boolean equals(Object obj) {\r
295                         if (this == obj) {\r
296                                 return true;\r
297                         }\r
298                         if (obj == null) {\r
299                                 return false;\r
300                         }\r
301                         if (getClass() != obj.getClass()) {\r
302                                 return false;\r
303                         }\r
304                         MethodSignatureImpl other = (MethodSignatureImpl) obj;\r
305                         if (comment == null) {\r
306                                 if (other.comment != null) {\r
307                                         return false;\r
308                                 }\r
309                         } else if (!comment.equals(other.comment)) {\r
310                                 return false;\r
311                         }\r
312                         if (name == null) {\r
313                                 if (other.name != null) {\r
314                                         return false;\r
315                                 }\r
316                         } else if (!name.equals(other.name)) {\r
317                                 return false;\r
318                         }\r
319                         if (params == null) {\r
320                                 if (other.params != null) {\r
321                                         return false;\r
322                                 }\r
323                         } else if (!params.equals(other.params)) {\r
324                                 return false;\r
325                         }\r
326                         if (definingType == null) {\r
327                                 if (other.definingType != null) {\r
328                                         return false;\r
329                                 }\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
335                                         return false;\r
336                                 }\r
337                         }\r
338                         if (returnType == null) {\r
339                                 if (other.returnType != null) {\r
340                                         return false;\r
341                                 }\r
342                         } else if (!returnType.equals(other.returnType)) {\r
343                                 return false;\r
344                         }\r
345                         return true;\r
346                 }\r
347 \r
348                 @Override\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
360                         } else {\r
361                                 builder.append(", definingType= null");\r
362                         }\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
369                 }\r
370         }\r
371 \r
372         private static final class GeneratedTypeImpl implements GeneratedType {\r
373 \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
380 \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
385                         super();\r
386                         this.parent = parent;\r
387                         this.packageName = packageName;\r
388                         this.name = name;\r
389 \r
390                         this.constantDefintions = toUnmodifiableConstants(constantBuilders);\r
391                         this.enumDefinitions = toUnmodifiableEnums(enumBuilders);\r
392                         this.methodDefinitions = toUnmodifiableMethods(methodBuilders);\r
393                 }\r
394 \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
400                         }\r
401                         return Collections.unmodifiableList(methods);\r
402                 }\r
403 \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
409                         }\r
410                         return Collections.unmodifiableList(enums);\r
411                 }\r
412 \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
418                         }\r
419                         return Collections.unmodifiableList(constants);\r
420                 }\r
421 \r
422                 @Override\r
423                 public String getPackageName() {\r
424                         return packageName;\r
425                 }\r
426 \r
427                 @Override\r
428                 public String getName() {\r
429                         return name;\r
430                 }\r
431 \r
432                 @Override\r
433                 public Type getParentType() {\r
434                         return parent;\r
435                 }\r
436 \r
437                 @Override\r
438                 public List<Enumeration> getEnumDefintions() {\r
439                         return enumDefinitions;\r
440                 }\r
441 \r
442                 @Override\r
443                 public List<Constant> getConstantDefinitions() {\r
444                         return constantDefintions;\r
445                 }\r
446 \r
447                 @Override\r
448                 public List<MethodSignature> getMethodDefinitions() {\r
449                         return methodDefinitions;\r
450                 }\r
451 \r
452                 @Override\r
453                 public int hashCode() {\r
454                         final int prime = 31;\r
455                         int result = 1;\r
456                         result = prime\r
457                                         * result\r
458                                         + ((constantDefintions == null) ? 0 : constantDefintions\r
459                                                         .hashCode());\r
460                         result = prime\r
461                                         * result\r
462                                         + ((enumDefinitions == null) ? 0 : enumDefinitions\r
463                                                         .hashCode());\r
464                         result = prime\r
465                                         * result\r
466                                         + ((methodDefinitions == null) ? 0 : methodDefinitions\r
467                                                         .hashCode());\r
468                         result = prime * result + ((name == null) ? 0 : name.hashCode());\r
469                         result = prime * result\r
470                                         + ((packageName == null) ? 0 : packageName.hashCode());\r
471                         return result;\r
472                 }\r
473 \r
474                 @Override\r
475                 public boolean equals(Object obj) {\r
476                         if (this == obj) {\r
477                                 return true;\r
478                         }\r
479                         if (obj == null) {\r
480                                 return false;\r
481                         }\r
482                         if (getClass() != obj.getClass()) {\r
483                                 return false;\r
484                         }\r
485                         GeneratedTypeImpl other = (GeneratedTypeImpl) obj;\r
486                         if (constantDefintions == null) {\r
487                                 if (other.constantDefintions != null) {\r
488                                         return false;\r
489                                 }\r
490                         } else if (!constantDefintions.equals(other.constantDefintions)) {\r
491                                 return false;\r
492                         }\r
493                         if (enumDefinitions == null) {\r
494                                 if (other.enumDefinitions != null) {\r
495                                         return false;\r
496                                 }\r
497                         } else if (!enumDefinitions.equals(other.enumDefinitions)) {\r
498                                 return false;\r
499                         }\r
500                         if (methodDefinitions == null) {\r
501                                 if (other.methodDefinitions != null) {\r
502                                         return false;\r
503                                 }\r
504                         } else if (!methodDefinitions.equals(other.methodDefinitions)) {\r
505                                 return false;\r
506                         }\r
507                         if (name == null) {\r
508                                 if (other.name != null) {\r
509                                         return false;\r
510                                 }\r
511                         } else if (!name.equals(other.name)) {\r
512                                 return false;\r
513                         }\r
514                         if (packageName == null) {\r
515                                 if (other.packageName != null) {\r
516                                         return false;\r
517                                 }\r
518                         } else if (!packageName.equals(other.packageName)) {\r
519                                 return false;\r
520                         }\r
521                         return true;\r
522                 }\r
523 \r
524                 @Override\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
541                 }\r
542         }\r
543 }\r