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