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