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