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