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