Added support for annotations in generated APIs.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-generator-util / src / main / java / org / opendaylight / controller / binding / generator / util / generated / type / builder / GeneratedTypeBuilderImpl.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.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.AnnotationType;
15 import org.opendaylight.controller.sal.binding.model.api.Constant;
16 import org.opendaylight.controller.sal.binding.model.api.Enumeration;
17 import org.opendaylight.controller.sal.binding.model.api.GeneratedType;
18 import org.opendaylight.controller.sal.binding.model.api.MethodSignature;
19 import org.opendaylight.controller.sal.binding.model.api.Type;
20 import org.opendaylight.controller.sal.binding.model.api.type.builder.AnnotationTypeBuilder;
21 import org.opendaylight.controller.sal.binding.model.api.type.builder.ConstantBuilder;
22 import org.opendaylight.controller.sal.binding.model.api.type.builder.EnumBuilder;
23 import org.opendaylight.controller.sal.binding.model.api.type.builder.GeneratedTypeBuilder;
24 import org.opendaylight.controller.sal.binding.model.api.type.builder.MethodSignatureBuilder;
25
26 public final class GeneratedTypeBuilderImpl implements GeneratedTypeBuilder {
27
28     private final String packageName;
29     private String comment = "";
30     private final String name;
31     private final List<AnnotationTypeBuilder> annotationBuilders = new ArrayList<AnnotationTypeBuilder>();
32     private final List<EnumBuilder> enumDefinitions = new ArrayList<EnumBuilder>();
33     private final List<ConstantBuilder> constantDefintions = new ArrayList<ConstantBuilder>();
34     private final List<MethodSignatureBuilder> methodDefinitions = new ArrayList<MethodSignatureBuilder>();
35
36     public GeneratedTypeBuilderImpl(final String packageName, final String name) {
37         this.packageName = packageName;
38         this.name = name;
39     }
40
41     @Override
42     public Type getParentType() {
43         return this;
44     }
45
46     @Override
47     public String getPackageName() {
48         return packageName;
49     }
50
51     @Override
52     public String getName() {
53         return name;
54     }
55
56     @Override
57     public void addComment(String comment) {
58         this.comment = comment;
59     }
60
61     @Override
62     public AnnotationTypeBuilder addAnnotation(String packageName, String name) {
63         if (packageName != null && name != null) {
64             final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(
65                     packageName, name);
66             if (annotationBuilders.add(builder)) {
67                 return builder;
68             }
69         }
70         return null;
71     }
72
73     @Override
74     public ConstantBuilder addConstant(Type type, String name, Object value) {
75         final ConstantBuilder builder = new ConstantBuilderImpl(type, name,
76                 value);
77         constantDefintions.add(builder);
78         return builder;
79     }
80
81     @Override
82     public EnumBuilder addEnumeration(final String name) {
83         final String innerPackageName = packageName + "." + this.name;
84         final EnumBuilder builder = new EnumerationBuilderImpl(
85                 innerPackageName, name);
86         enumDefinitions.add(builder);
87         return builder;
88     }
89
90     @Override
91     public MethodSignatureBuilder addMethod(final String name) {
92         final MethodSignatureBuilder builder = new MethodSignatureBuilderImpl(
93                 this, name);
94         methodDefinitions.add(builder);
95         return builder;
96     }
97
98     @Override
99     public GeneratedType toInstance() {
100         return new GeneratedTypeImpl(this, packageName, name, comment,
101                 annotationBuilders, enumDefinitions, constantDefintions,
102                 methodDefinitions);
103     }
104
105     private static final class GeneratedTypeImpl implements GeneratedType {
106
107         private final Type parent;
108         private final String packageName;
109         private final String name;
110         private final String comment;
111         private final List<AnnotationType> annotations;
112         private final List<Enumeration> enumDefinitions;
113         private final List<Constant> constantDefintions;
114         private final List<MethodSignature> methodDefinitions;
115
116         public GeneratedTypeImpl(final Type parent, final String packageName,
117                 final String name, final String comment,
118                 final List<AnnotationTypeBuilder> annotationBuilders,
119                 final List<EnumBuilder> enumBuilders,
120                 final List<ConstantBuilder> constantBuilders,
121                 final List<MethodSignatureBuilder> methodBuilders) {
122             super();
123             this.parent = parent;
124             this.packageName = packageName;
125             this.name = name;
126             this.comment = comment;
127             this.annotations = toUnmodifiableAnnotations(annotationBuilders);
128             this.constantDefintions = toUnmodifiableConstants(constantBuilders);
129             this.enumDefinitions = toUnmodifiableEnums(enumBuilders);
130             this.methodDefinitions = toUnmodifiableMethods(methodBuilders);
131         }
132
133         private List<AnnotationType> toUnmodifiableAnnotations(
134                 final List<AnnotationTypeBuilder> annotationBuilders) {
135             final List<AnnotationType> annotations = new ArrayList<AnnotationType>();
136             for (final AnnotationTypeBuilder builder : annotationBuilders) {
137                 annotations.add(builder.toInstance());
138             }
139             return Collections.unmodifiableList(annotations);
140         }
141
142         private List<MethodSignature> toUnmodifiableMethods(
143                 List<MethodSignatureBuilder> methodBuilders) {
144             final List<MethodSignature> methods = new ArrayList<MethodSignature>();
145             for (final MethodSignatureBuilder methodBuilder : methodBuilders) {
146                 methods.add(methodBuilder.toInstance(this));
147             }
148             return Collections.unmodifiableList(methods);
149         }
150
151         private List<Enumeration> toUnmodifiableEnums(
152                 List<EnumBuilder> enumBuilders) {
153             final List<Enumeration> enums = new ArrayList<Enumeration>();
154             for (final EnumBuilder enumBuilder : enumBuilders) {
155                 enums.add(enumBuilder.toInstance(this));
156             }
157             return Collections.unmodifiableList(enums);
158         }
159
160         private List<Constant> toUnmodifiableConstants(
161                 List<ConstantBuilder> constantBuilders) {
162             final List<Constant> constants = new ArrayList<Constant>();
163             for (final ConstantBuilder enumBuilder : constantBuilders) {
164                 constants.add(enumBuilder.toInstance(this));
165             }
166             return Collections.unmodifiableList(constants);
167         }
168
169         @Override
170         public String getPackageName() {
171             return packageName;
172         }
173
174         @Override
175         public String getName() {
176             return name;
177         }
178
179         @Override
180         public Type getParentType() {
181             return parent;
182         }
183
184         @Override
185         public String getComment() {
186             return comment;
187         }
188
189         @Override
190         public List<AnnotationType> getAnnotations() {
191             return annotations;
192         }
193
194         @Override
195         public List<Enumeration> getEnumDefintions() {
196             return enumDefinitions;
197         }
198
199         @Override
200         public List<Constant> getConstantDefinitions() {
201             return constantDefintions;
202         }
203
204         @Override
205         public List<MethodSignature> getMethodDefinitions() {
206             return methodDefinitions;
207         }
208
209         @Override
210         public int hashCode() {
211             final int prime = 31;
212             int result = 1;
213             result = prime
214                     * result
215                     + ((constantDefintions == null) ? 0 : constantDefintions
216                             .hashCode());
217             result = prime
218                     * result
219                     + ((enumDefinitions == null) ? 0 : enumDefinitions
220                             .hashCode());
221             result = prime
222                     * result
223                     + ((methodDefinitions == null) ? 0 : methodDefinitions
224                             .hashCode());
225             result = prime * result + ((name == null) ? 0 : name.hashCode());
226             result = prime * result
227                     + ((packageName == null) ? 0 : packageName.hashCode());
228             return result;
229         }
230
231         @Override
232         public boolean equals(Object obj) {
233             if (this == obj) {
234                 return true;
235             }
236             if (obj == null) {
237                 return false;
238             }
239             if (getClass() != obj.getClass()) {
240                 return false;
241             }
242             GeneratedTypeImpl other = (GeneratedTypeImpl) obj;
243             if (constantDefintions == null) {
244                 if (other.constantDefintions != null) {
245                     return false;
246                 }
247             } else if (!constantDefintions.equals(other.constantDefintions)) {
248                 return false;
249             }
250             if (enumDefinitions == null) {
251                 if (other.enumDefinitions != null) {
252                     return false;
253                 }
254             } else if (!enumDefinitions.equals(other.enumDefinitions)) {
255                 return false;
256             }
257             if (methodDefinitions == null) {
258                 if (other.methodDefinitions != null) {
259                     return false;
260                 }
261             } else if (!methodDefinitions.equals(other.methodDefinitions)) {
262                 return false;
263             }
264             if (name == null) {
265                 if (other.name != null) {
266                     return false;
267                 }
268             } else if (!name.equals(other.name)) {
269                 return false;
270             }
271             if (packageName == null) {
272                 if (other.packageName != null) {
273                     return false;
274                 }
275             } else if (!packageName.equals(other.packageName)) {
276                 return false;
277             }
278             return true;
279         }
280
281         @Override
282         public String toString() {
283             StringBuilder builder = new StringBuilder();
284             builder.append("GeneratedType [packageName=");
285             builder.append(packageName);
286             builder.append(", name=");
287             builder.append(name);
288             if (parent != null) {
289                 builder.append(", parent=");
290                 builder.append(parent.getPackageName());
291                 builder.append(".");
292                 builder.append(parent.getName());
293             } else {
294                 builder.append(", parent=null");
295             }
296             builder.append(", comment=");
297             builder.append(comment);
298             builder.append(", annotations=");
299             builder.append(annotations);
300             builder.append(", enumDefinitions=");
301             builder.append(enumDefinitions);
302             builder.append(", constantDefintions=");
303             builder.append(constantDefintions);
304             builder.append(", methodDefinitions=");
305             builder.append(methodDefinitions);
306             builder.append("]");
307             return builder.toString();
308         }
309     }
310 }