Added support for implements and extends of GeneratedType;
[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<GeneratedType> implementsTypes = new ArrayList<GeneratedType>();
33     private final List<EnumBuilder> enumDefinitions = new ArrayList<EnumBuilder>();
34     private final List<ConstantBuilder> constantDefintions = new ArrayList<ConstantBuilder>();
35     private final List<MethodSignatureBuilder> methodDefinitions = new ArrayList<MethodSignatureBuilder>();
36     
37     public GeneratedTypeBuilderImpl(final String packageName, final String name) {
38         this.packageName = packageName;
39         this.name = name;
40     }
41
42     @Override
43     public Type getParentType() {
44         return this;
45     }
46
47     @Override
48     public String getPackageName() {
49         return packageName;
50     }
51
52     @Override
53     public String getName() {
54         return name;
55     }
56
57     @Override
58     public void addComment(String comment) {
59         this.comment = comment;
60     }
61
62     @Override
63     public AnnotationTypeBuilder addAnnotation(String packageName, String name) {
64         if (packageName != null && name != null) {
65             final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(
66                     packageName, name);
67             if (annotationBuilders.add(builder)) {
68                 return builder;
69             }
70         }
71         return null;
72     }
73
74     @Override
75     public ConstantBuilder addConstant(Type type, String name, Object value) {
76         final ConstantBuilder builder = new ConstantBuilderImpl(type, name,
77                 value);
78         constantDefintions.add(builder);
79         return builder;
80     }
81
82     @Override
83     public EnumBuilder addEnumeration(final String name) {
84         final String innerPackageName = packageName + "." + this.name;
85         final EnumBuilder builder = new EnumerationBuilderImpl(
86                 innerPackageName, name);
87         enumDefinitions.add(builder);
88         return builder;
89     }
90
91     @Override
92     public MethodSignatureBuilder addMethod(final String name) {
93         final MethodSignatureBuilder builder = new MethodSignatureBuilderImpl(
94                 this, name);
95         methodDefinitions.add(builder);
96         return builder;
97     }
98
99     @Override
100     public boolean addImplementsType(final GeneratedType genType) {
101         if (genType != null) {
102             return implementsTypes.add(genType);
103         }
104         return false;
105     }
106     
107     @Override
108     public GeneratedType toInstance() {
109         return new GeneratedTypeImpl(this, packageName, name, comment,
110                 annotationBuilders, implementsTypes, enumDefinitions, constantDefintions,
111                 methodDefinitions);
112     }
113     
114     private static final class GeneratedTypeImpl implements GeneratedType {
115
116         private final Type parent;
117         private final String packageName;
118         private final String name;
119         private final String comment;
120         private final List<AnnotationType> annotations;
121         private final List<GeneratedType> implementsTypes;
122         private final List<Enumeration> enumDefinitions;
123         private final List<Constant> constantDefintions;
124         private final List<MethodSignature> methodDefinitions;
125
126         public GeneratedTypeImpl(final Type parent, final String packageName,
127                 final String name, final String comment,
128                 final List<AnnotationTypeBuilder> annotationBuilders,
129                 final List<GeneratedType> implementsTypes,
130                 final List<EnumBuilder> enumBuilders,
131                 final List<ConstantBuilder> constantBuilders,
132                 final List<MethodSignatureBuilder> methodBuilders) {
133             super();
134             this.parent = parent;
135             this.packageName = packageName;
136             this.name = name;
137             this.comment = comment;
138             this.annotations = toUnmodifiableAnnotations(annotationBuilders);
139             this.implementsTypes = Collections.unmodifiableList(implementsTypes); 
140             this.constantDefintions = toUnmodifiableConstants(constantBuilders);
141             this.enumDefinitions = toUnmodifiableEnums(enumBuilders);
142             this.methodDefinitions = toUnmodifiableMethods(methodBuilders);
143         }
144
145         private List<AnnotationType> toUnmodifiableAnnotations(
146                 final List<AnnotationTypeBuilder> annotationBuilders) {
147             final List<AnnotationType> annotations = new ArrayList<AnnotationType>();
148             for (final AnnotationTypeBuilder builder : annotationBuilders) {
149                 annotations.add(builder.toInstance());
150             }
151             return Collections.unmodifiableList(annotations);
152         }
153
154         private List<MethodSignature> toUnmodifiableMethods(
155                 List<MethodSignatureBuilder> methodBuilders) {
156             final List<MethodSignature> methods = new ArrayList<MethodSignature>();
157             for (final MethodSignatureBuilder methodBuilder : methodBuilders) {
158                 methods.add(methodBuilder.toInstance(this));
159             }
160             return Collections.unmodifiableList(methods);
161         }
162
163         private List<Enumeration> toUnmodifiableEnums(
164                 List<EnumBuilder> enumBuilders) {
165             final List<Enumeration> enums = new ArrayList<Enumeration>();
166             for (final EnumBuilder enumBuilder : enumBuilders) {
167                 enums.add(enumBuilder.toInstance(this));
168             }
169             return Collections.unmodifiableList(enums);
170         }
171
172         private List<Constant> toUnmodifiableConstants(
173                 List<ConstantBuilder> constantBuilders) {
174             final List<Constant> constants = new ArrayList<Constant>();
175             for (final ConstantBuilder enumBuilder : constantBuilders) {
176                 constants.add(enumBuilder.toInstance(this));
177             }
178             return Collections.unmodifiableList(constants);
179         }
180
181         @Override
182         public String getPackageName() {
183             return packageName;
184         }
185
186         @Override
187         public String getName() {
188             return name;
189         }
190
191         @Override
192         public Type getParentType() {
193             return parent;
194         }
195
196         @Override
197         public String getComment() {
198             return comment;
199         }
200
201         @Override
202         public List<AnnotationType> getAnnotations() {
203             return annotations;
204         }
205         
206         @Override
207         public List<GeneratedType> getImplements() {
208             return implementsTypes;
209         }
210         
211         @Override
212         public List<Enumeration> getEnumDefintions() {
213             return enumDefinitions;
214         }
215
216         @Override
217         public List<Constant> getConstantDefinitions() {
218             return constantDefintions;
219         }
220
221         @Override
222         public List<MethodSignature> getMethodDefinitions() {
223             return methodDefinitions;
224         }
225
226         @Override
227         public int hashCode() {
228             final int prime = 31;
229             int result = 1;
230             result = prime
231                     * result
232                     + ((constantDefintions == null) ? 0 : constantDefintions
233                             .hashCode());
234             result = prime
235                     * result
236                     + ((enumDefinitions == null) ? 0 : enumDefinitions
237                             .hashCode());
238             result = prime
239                     * result
240                     + ((methodDefinitions == null) ? 0 : methodDefinitions
241                             .hashCode());
242             result = prime * result + ((name == null) ? 0 : name.hashCode());
243             result = prime * result
244                     + ((packageName == null) ? 0 : packageName.hashCode());
245             return result;
246         }
247
248         @Override
249         public boolean equals(Object obj) {
250             if (this == obj) {
251                 return true;
252             }
253             if (obj == null) {
254                 return false;
255             }
256             if (getClass() != obj.getClass()) {
257                 return false;
258             }
259             GeneratedTypeImpl other = (GeneratedTypeImpl) obj;
260             if (constantDefintions == null) {
261                 if (other.constantDefintions != null) {
262                     return false;
263                 }
264             } else if (!constantDefintions.equals(other.constantDefintions)) {
265                 return false;
266             }
267             if (enumDefinitions == null) {
268                 if (other.enumDefinitions != null) {
269                     return false;
270                 }
271             } else if (!enumDefinitions.equals(other.enumDefinitions)) {
272                 return false;
273             }
274             if (methodDefinitions == null) {
275                 if (other.methodDefinitions != null) {
276                     return false;
277                 }
278             } else if (!methodDefinitions.equals(other.methodDefinitions)) {
279                 return false;
280             }
281             if (name == null) {
282                 if (other.name != null) {
283                     return false;
284                 }
285             } else if (!name.equals(other.name)) {
286                 return false;
287             }
288             if (packageName == null) {
289                 if (other.packageName != null) {
290                     return false;
291                 }
292             } else if (!packageName.equals(other.packageName)) {
293                 return false;
294             }
295             return true;
296         }
297
298         @Override
299         public String toString() {
300             StringBuilder builder = new StringBuilder();
301             builder.append("GeneratedType [packageName=");
302             builder.append(packageName);
303             builder.append(", name=");
304             builder.append(name);
305             if (parent != null) {
306                 builder.append(", parent=");
307                 builder.append(parent.getPackageName());
308                 builder.append(".");
309                 builder.append(parent.getName());
310             } else {
311                 builder.append(", parent=null");
312             }
313             builder.append(", comment=");
314             builder.append(comment);
315             builder.append(", annotations=");
316             builder.append(annotations);
317             builder.append(", enumDefinitions=");
318             builder.append(enumDefinitions);
319             builder.append(", constantDefintions=");
320             builder.append(constantDefintions);
321             builder.append(", methodDefinitions=");
322             builder.append(methodDefinitions);
323             builder.append("]");
324             return builder.toString();
325         }
326     }
327 }