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 / GeneratedTOBuilderImpl.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.GeneratedProperty;\r
21 import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;\r
22 import org.opendaylight.controller.sal.binding.model.api.MethodSignature;\r
23 import org.opendaylight.controller.sal.binding.model.api.Type;\r
24 import org.opendaylight.controller.sal.binding.model.api.type.builder.ConstantBuilder;\r
25 import org.opendaylight.controller.sal.binding.model.api.type.builder.EnumBuilder;\r
26 import org.opendaylight.controller.sal.binding.model.api.type.builder.GeneratedPropertyBuilder;\r
27 import org.opendaylight.controller.sal.binding.model.api.type.builder.GeneratedTOBuilder;\r
28 \r
29 final class GeneratedTOBuilderImpl implements GeneratedTOBuilder {\r
30     \r
31     private static final String[] SET_VALUES = new String[] { "abstract",\r
32         "assert", "boolean", "break", "byte", "case", "catch", "char",\r
33         "class", "const", "continue", "default", "double", "do", "else",\r
34         "enum", "extends", "false", "final", "finally", "float", "for",\r
35         "goto", "if", "implements", "import", "instanceof", "int",\r
36         "interface", "long", "native", "new", "null", "package", "private",\r
37         "protected", "public", "return", "short", "static", "strictfp",\r
38         "super", "switch", "synchronized", "this", "throw", "throws",\r
39         "transient", "true", "try", "void", "volatile", "while" };\r
40 \r
41     public static final Set<String> JAVA_RESERVED_WORDS = new HashSet<String>(\r
42             Arrays.asList(SET_VALUES));\r
43     \r
44     private String packageName;\r
45     private final String name;\r
46 \r
47     private final List<EnumBuilder> enumerations = new ArrayList<EnumBuilder>();\r
48     private final List<GeneratedPropertyBuilder> properties = new ArrayList<GeneratedPropertyBuilder>();\r
49     private final List<GeneratedPropertyBuilder> equalsProperties = new ArrayList<GeneratedPropertyBuilder>();\r
50     private final List<GeneratedPropertyBuilder> hashProperties = new ArrayList<GeneratedPropertyBuilder>();\r
51     private final List<GeneratedPropertyBuilder> toStringProperties = new ArrayList<GeneratedPropertyBuilder>();\r
52 \r
53     public GeneratedTOBuilderImpl(String packageName, String name) {\r
54         super();\r
55         this.packageName = GeneratedTypeBuilderImpl.validatePackage(packageName);\r
56         this.name = name;\r
57     }\r
58     \r
59     @Override\r
60     public String getPackageName() {\r
61         return packageName;\r
62     }\r
63 \r
64     @Override\r
65     public String getName() {\r
66         return name;\r
67     }\r
68     \r
69     @Override\r
70     public EnumBuilder addEnumeration(String name) {\r
71         final EnumBuilder builder = new EnumerationBuilderImpl(packageName,\r
72                 name);\r
73         enumerations.add(builder);\r
74         return builder;\r
75     }\r
76 \r
77     @Override\r
78     public GeneratedPropertyBuilder addProperty(String name) {\r
79         final GeneratedPropertyBuilder builder = new GeneratedPropertyBuilderImpl(\r
80                 name);\r
81         properties.add(builder);\r
82         return builder;\r
83     }\r
84 \r
85     @Override\r
86     public boolean addEqualsIdentity(GeneratedPropertyBuilder property) {\r
87         return equalsProperties.add(property);\r
88     }\r
89 \r
90     @Override\r
91     public boolean addHashIdentity(GeneratedPropertyBuilder property) {\r
92         return hashProperties.add(property);\r
93     }\r
94 \r
95     @Override\r
96     public boolean addToStringProperty(GeneratedPropertyBuilder property) {\r
97         return toStringProperties.add(property);\r
98     }\r
99 \r
100     @Override\r
101     public GeneratedTransferObject toInstance() {\r
102        \r
103         return new GeneratedTransferObjectImpl(packageName, name, enumerations,\r
104                 properties, equalsProperties, hashProperties,\r
105                 toStringProperties);\r
106     }\r
107 \r
108     private static final class GeneratedPropertyBuilderImpl implements\r
109             GeneratedPropertyBuilder {\r
110 \r
111         private final String name;\r
112         private Type returnType;\r
113         private final List<MethodSignature.Parameter> parameters;\r
114         private String comment = "";\r
115         private AccessModifier accessModifier;\r
116         private boolean isFinal;\r
117         private boolean isReadOnly;\r
118 \r
119         public GeneratedPropertyBuilderImpl(final String name) {\r
120             super();\r
121             this.name = name;\r
122             parameters = new ArrayList<MethodSignature.Parameter>();\r
123             isFinal = true;\r
124             this.isReadOnly = true;\r
125             accessModifier = AccessModifier.PUBLIC;\r
126         }\r
127 \r
128         public String getName() {\r
129             return name;\r
130         }\r
131 \r
132         @Override\r
133         public boolean addReturnType(Type returnType) {\r
134             if (returnType != null) {\r
135                 this.returnType = returnType;\r
136                 this.parameters.add(new MethodParameterImpl(name, returnType));\r
137                 return true;\r
138             }\r
139             return false;\r
140         }\r
141 \r
142         @Override\r
143         public void accessorModifier(final AccessModifier modifier) {\r
144             accessModifier = modifier;\r
145         }\r
146 \r
147         @Override\r
148         public void addComment(String comment) {\r
149             if (comment != null) {\r
150                 this.comment = comment;\r
151             }\r
152         }\r
153 \r
154         @Override\r
155         public void setFinal(boolean isFinal) {\r
156             this.isFinal = isFinal;\r
157         }\r
158 \r
159         @Override\r
160         public void setReadOnly(boolean isReadOnly) {\r
161             this.isReadOnly = isReadOnly;\r
162         }\r
163 \r
164         @Override\r
165         public GeneratedProperty toInstance(final Type definingType) {\r
166             return new GeneratedPropertyImpl(name, comment, definingType,\r
167                     returnType, isFinal, isReadOnly, parameters, accessModifier);\r
168         }\r
169     }\r
170 \r
171     private static final class GeneratedPropertyImpl implements\r
172             GeneratedProperty {\r
173 \r
174         private final String name;\r
175         private final String comment;\r
176         private final Type parent;\r
177         private final Type returnType;\r
178         private final boolean isFinal;\r
179         private final boolean isReadOnly;\r
180         private final List<MethodSignature.Parameter> parameters;\r
181         private final AccessModifier modifier;\r
182 \r
183         public GeneratedPropertyImpl(final String name, final String comment,\r
184                 final Type parent, final Type returnType,\r
185                 final boolean isFinal, final boolean isReadOnly,\r
186                 final List<Parameter> parameters, final AccessModifier modifier) {\r
187             super();\r
188             this.name = name;\r
189             this.comment = comment;\r
190             this.parent = parent;\r
191             this.returnType = returnType;\r
192             this.isFinal = isFinal;\r
193             this.isReadOnly = isReadOnly;\r
194             this.parameters = Collections.unmodifiableList(parameters);\r
195             this.modifier = modifier;\r
196         }\r
197 \r
198         @Override\r
199         public String getName() {\r
200             return name;\r
201         }\r
202 \r
203         @Override\r
204         public String getComment() {\r
205             return comment;\r
206         }\r
207 \r
208         @Override\r
209         public Type getDefiningType() {\r
210             return parent;\r
211         }\r
212 \r
213         @Override\r
214         public Type getReturnType() {\r
215             return returnType;\r
216         }\r
217 \r
218         @Override\r
219         public List<Parameter> getParameters() {\r
220             return parameters;\r
221         }\r
222 \r
223         @Override\r
224         public AccessModifier getAccessModifier() {\r
225             return modifier;\r
226         }\r
227 \r
228         @Override\r
229         public boolean isReadOnly() {\r
230             return isReadOnly;\r
231         }\r
232 \r
233         @Override\r
234         public boolean isFinal() {\r
235             return isFinal;\r
236         }\r
237 \r
238         @Override\r
239         public int hashCode() {\r
240             final int prime = 31;\r
241             int result = 1;\r
242             result = prime * result\r
243                     + ((comment == null) ? 0 : comment.hashCode());\r
244             result = prime * result + (isFinal ? 1231 : 1237);\r
245             result = prime * result + (isReadOnly ? 1231 : 1237);\r
246             result = prime * result\r
247                     + ((modifier == null) ? 0 : modifier.hashCode());\r
248             result = prime * result + ((name == null) ? 0 : name.hashCode());\r
249             result = prime * result\r
250                     + ((parameters == null) ? 0 : parameters.hashCode());\r
251 \r
252             if (parent != null) {\r
253                 result = prime\r
254                         * result\r
255                         + ((parent.getPackageName() == null) ? 0 : parent\r
256                                 .getPackageName().hashCode());\r
257                 result = prime\r
258                         * result\r
259                         + ((parent.getName() == null) ? 0 : parent.getName()\r
260                                 .hashCode());\r
261             }\r
262 \r
263             result = prime * result\r
264                     + ((returnType == null) ? 0 : returnType.hashCode());\r
265             return result;\r
266         }\r
267 \r
268         @Override\r
269         public boolean equals(Object obj) {\r
270             if (this == obj) {\r
271                 return true;\r
272             }\r
273             if (obj == null) {\r
274                 return false;\r
275             }\r
276             if (getClass() != obj.getClass()) {\r
277                 return false;\r
278             }\r
279             GeneratedPropertyImpl other = (GeneratedPropertyImpl) obj;\r
280             if (comment == null) {\r
281                 if (other.comment != null) {\r
282                     return false;\r
283                 }\r
284             } else if (!comment.equals(other.comment)) {\r
285                 return false;\r
286             }\r
287             if (isFinal != other.isFinal) {\r
288                 return false;\r
289             }\r
290             if (isReadOnly != other.isReadOnly) {\r
291                 return false;\r
292             }\r
293             if (modifier != other.modifier) {\r
294                 return false;\r
295             }\r
296             if (name == null) {\r
297                 if (other.name != null) {\r
298                     return false;\r
299                 }\r
300             } else if (!name.equals(other.name)) {\r
301                 return false;\r
302             }\r
303             if (parameters == null) {\r
304                 if (other.parameters != null) {\r
305                     return false;\r
306                 }\r
307             } else if (!parameters.equals(other.parameters)) {\r
308                 return false;\r
309             }\r
310             if (parent == null) {\r
311                 if (other.parent != null) {\r
312                     return false;\r
313                 }\r
314             } else if ((parent != null) && (other.parent != null)) {\r
315                 if (!parent.getPackageName().equals(\r
316                         other.parent.getPackageName())\r
317                         && !parent.getName().equals(other.parent.getName())) {\r
318                     return false;\r
319                 }\r
320             }\r
321             if (returnType == null) {\r
322                 if (other.returnType != null) {\r
323                     return false;\r
324                 }\r
325             } else if (!returnType.equals(other.returnType)) {\r
326                 return false;\r
327             }\r
328             return true;\r
329         }\r
330 \r
331         @Override\r
332         public String toString() {\r
333             StringBuilder builder = new StringBuilder();\r
334             builder.append("GeneratedPropertyImpl [name=");\r
335             builder.append(name);\r
336             builder.append(", comment=");\r
337             builder.append(comment);\r
338             if (parent != null) {\r
339                 builder.append(", parent=");\r
340                 builder.append(parent.getPackageName());\r
341                 builder.append(".");\r
342                 builder.append(parent.getName());\r
343             } else {\r
344                 builder.append(", parent= null");\r
345             }\r
346             builder.append(", returnType=");\r
347             builder.append(returnType);\r
348             builder.append(", isFinal=");\r
349             builder.append(isFinal);\r
350             builder.append(", isReadOnly=");\r
351             builder.append(isReadOnly);\r
352             builder.append(", parameters=");\r
353             builder.append(parameters);\r
354             builder.append(", modifier=");\r
355             builder.append(modifier);\r
356             builder.append("]");\r
357             return builder.toString();\r
358         }\r
359     }\r
360 \r
361     private static final class GeneratedTransferObjectImpl implements\r
362             GeneratedTransferObject {\r
363 \r
364         private final String packageName;\r
365         private final String name;\r
366         // private final List<Constant> constants;\r
367         private final List<Enumeration> enumerations;\r
368         private final List<GeneratedProperty> properties;\r
369         private final List<GeneratedProperty> equalsProperties;\r
370         private final List<GeneratedProperty> hashCodeProperties;\r
371         private final List<GeneratedProperty> stringProperties;\r
372 \r
373         private List<Enumeration> toUnmodifiableEnumerations(\r
374                 final List<EnumBuilder> enumBuilders) {\r
375             final List<Enumeration> enumerations = new ArrayList<Enumeration>();\r
376             for (final EnumBuilder builder : enumBuilders) {\r
377                 enumerations.add(builder.toInstance(this));\r
378             }\r
379             return Collections.unmodifiableList(enumerations);\r
380         }\r
381 \r
382         private List<Constant> toUnmodifiableConstant(\r
383                 final List<ConstantBuilder> constBuilders) {\r
384             final List<Constant> constants = new ArrayList<Constant>();\r
385             for (final ConstantBuilder builder : constBuilders) {\r
386                 constants.add(builder.toInstance(this));\r
387             }\r
388             return Collections.unmodifiableList(constants);\r
389         }\r
390 \r
391         private List<GeneratedProperty> toUnmodifiableProperties(\r
392                 final List<GeneratedPropertyBuilder> propBuilders) {\r
393             final List<GeneratedProperty> constants = new ArrayList<GeneratedProperty>();\r
394             for (final GeneratedPropertyBuilder builder : propBuilders) {\r
395                 constants.add(builder.toInstance(this));\r
396             }\r
397             return Collections.unmodifiableList(constants);\r
398         }\r
399 \r
400         public GeneratedTransferObjectImpl(String packageName, String name,\r
401                 List<EnumBuilder> enumBuilders,\r
402                 List<GeneratedPropertyBuilder> propBuilers,\r
403                 List<GeneratedPropertyBuilder> equalsBuilers,\r
404                 List<GeneratedPropertyBuilder> hashCodeBuilers,\r
405                 List<GeneratedPropertyBuilder> stringBuilers) {\r
406             super();\r
407             this.packageName = packageName;\r
408             this.name = name;\r
409             this.enumerations = toUnmodifiableEnumerations(enumBuilders);\r
410             this.properties = toUnmodifiableProperties(propBuilers);\r
411             this.equalsProperties = toUnmodifiableProperties(equalsBuilers);\r
412             this.hashCodeProperties = toUnmodifiableProperties(hashCodeBuilers);\r
413             this.stringProperties = toUnmodifiableProperties(stringBuilers);\r
414         }\r
415 \r
416         @Override\r
417         public String getPackageName() {\r
418             return packageName;\r
419         }\r
420 \r
421         @Override\r
422         public String getName() {\r
423             return name;\r
424         }\r
425 \r
426         @Override\r
427         public List<Enumeration> getEnumDefintions() {\r
428             return enumerations;\r
429         }\r
430 \r
431         @Override\r
432         public List<GeneratedProperty> getProperties() {\r
433             return properties;\r
434         }\r
435 \r
436         @Override\r
437         public List<GeneratedProperty> getEqualsIdentifiers() {\r
438             return equalsProperties;\r
439         }\r
440 \r
441         @Override\r
442         public List<GeneratedProperty> getHashCodeIdentifiers() {\r
443             return hashCodeProperties;\r
444         }\r
445 \r
446         @Override\r
447         public List<GeneratedProperty> getToStringIdentifiers() {\r
448             return stringProperties;\r
449         }\r
450 \r
451         /*\r
452          * (non-Javadoc)\r
453          * \r
454          * @see java.lang.Object#hashCode()\r
455          */\r
456         @Override\r
457         public int hashCode() {\r
458             final int prime = 31;\r
459             int result = 1;\r
460             result = prime * result\r
461                     + ((enumerations == null) ? 0 : enumerations.hashCode());\r
462             result = prime\r
463                     * result\r
464                     + ((equalsProperties == null) ? 0 : equalsProperties\r
465                             .hashCode());\r
466             result = prime\r
467                     * result\r
468                     + ((hashCodeProperties == null) ? 0 : hashCodeProperties\r
469                             .hashCode());\r
470             result = prime * result + ((name == null) ? 0 : name.hashCode());\r
471             result = prime * result\r
472                     + ((packageName == null) ? 0 : packageName.hashCode());\r
473             result = prime * result\r
474                     + ((properties == null) ? 0 : properties.hashCode());\r
475             result = prime\r
476                     * result\r
477                     + ((stringProperties == null) ? 0 : stringProperties\r
478                             .hashCode());\r
479             return result;\r
480         }\r
481 \r
482         /*\r
483          * (non-Javadoc)\r
484          * \r
485          * @see java.lang.Object#equals(java.lang.Object)\r
486          */\r
487         @Override\r
488         public boolean equals(Object obj) {\r
489             if (this == obj) {\r
490                 return true;\r
491             }\r
492             if (obj == null) {\r
493                 return false;\r
494             }\r
495             if (getClass() != obj.getClass()) {\r
496                 return false;\r
497             }\r
498             GeneratedTransferObjectImpl other = (GeneratedTransferObjectImpl) obj;\r
499             if (enumerations == null) {\r
500                 if (other.enumerations != null) {\r
501                     return false;\r
502                 }\r
503             } else if (!enumerations.equals(other.enumerations)) {\r
504                 return false;\r
505             }\r
506             if (equalsProperties == null) {\r
507                 if (other.equalsProperties != null) {\r
508                     return false;\r
509                 }\r
510             } else if (!equalsProperties.equals(other.equalsProperties)) {\r
511                 return false;\r
512             }\r
513             if (hashCodeProperties == null) {\r
514                 if (other.hashCodeProperties != null) {\r
515                     return false;\r
516                 }\r
517             } else if (!hashCodeProperties.equals(other.hashCodeProperties)) {\r
518                 return false;\r
519             }\r
520             if (name == null) {\r
521                 if (other.name != null) {\r
522                     return false;\r
523                 }\r
524             } else if (!name.equals(other.name)) {\r
525                 return false;\r
526             }\r
527             if (packageName == null) {\r
528                 if (other.packageName != null) {\r
529                     return false;\r
530                 }\r
531             } else if (!packageName.equals(other.packageName)) {\r
532                 return false;\r
533             }\r
534             if (properties == null) {\r
535                 if (other.properties != null) {\r
536                     return false;\r
537                 }\r
538             } else if (!properties.equals(other.properties)) {\r
539                 return false;\r
540             }\r
541             if (stringProperties == null) {\r
542                 if (other.stringProperties != null) {\r
543                     return false;\r
544                 }\r
545             } else if (!stringProperties.equals(other.stringProperties)) {\r
546                 return false;\r
547             }\r
548             return true;\r
549         }\r
550 \r
551         /*\r
552          * (non-Javadoc)\r
553          * \r
554          * @see java.lang.Object#toString()\r
555          */\r
556         @Override\r
557         public String toString() {\r
558             StringBuilder builder = new StringBuilder();\r
559             builder.append("GeneratedTransferObjectImpl [packageName=");\r
560             builder.append(packageName);\r
561             builder.append(", name=");\r
562             builder.append(name);\r
563             builder.append(", enumerations=");\r
564             builder.append(enumerations);\r
565             builder.append(", properties=");\r
566             builder.append(properties);\r
567             builder.append(", equalsProperties=");\r
568             builder.append(equalsProperties);\r
569             builder.append(", hashCodeProperties=");\r
570             builder.append(hashCodeProperties);\r
571             builder.append(", stringProperties=");\r
572             builder.append(stringProperties);\r
573             builder.append("]");\r
574             return builder.toString();\r
575         }\r
576     }\r
577 }\r

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.