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