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