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