Merge "Added support for annotations in generated APIs."
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-generator-impl / src / main / java / org / opendaylight / controller / sal / binding / generator / impl / MethodSignatureBuilderImpl.java
1 package org.opendaylight.controller.sal.binding.generator.impl;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6
7 import org.opendaylight.controller.sal.binding.model.api.AccessModifier;
8 import org.opendaylight.controller.sal.binding.model.api.AnnotationType;
9 import org.opendaylight.controller.sal.binding.model.api.MethodSignature;
10 import org.opendaylight.controller.sal.binding.model.api.Type;
11 import org.opendaylight.controller.sal.binding.model.api.type.builder.AnnotationTypeBuilder;
12 import org.opendaylight.controller.sal.binding.model.api.type.builder.MethodSignatureBuilder;
13
14 final class MethodSignatureBuilderImpl implements MethodSignatureBuilder {
15     private final String name;
16     private Type returnType;
17     private final List<MethodSignature.Parameter> parameters;
18     private final List<AnnotationTypeBuilder> annotationBuilders;
19     private String comment = "";
20     private final Type parent;
21
22     public MethodSignatureBuilderImpl(final Type parent, final String name) {
23         super();
24         this.name = name;
25         this.parent = parent;
26         this.parameters = new ArrayList<MethodSignature.Parameter>();
27         this.annotationBuilders = new ArrayList<AnnotationTypeBuilder>();
28     }
29
30     @Override
31     public AnnotationTypeBuilder addAnnotation(String packageName, String name) {
32         if (packageName != null && name != null) {
33             final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(
34                     packageName, name);
35             if (annotationBuilders.add(builder)) {
36                 return builder;
37             }
38         }
39         return null;
40     }
41
42     @Override
43     public void addReturnType(Type returnType) {
44         if (returnType != null) {
45             this.returnType = returnType;
46         }
47     }
48
49     @Override
50     public void addParameter(Type type, String name) {
51         parameters.add(new MethodParameterImpl(name, type));
52     }
53
54     @Override
55     public void addComment(String comment) {
56         this.comment = comment;
57     }
58
59     @Override
60     public MethodSignature toInstance(Type definingType) {
61         return new MethodSignatureImpl(definingType, name, annotationBuilders,
62                 comment, returnType, parameters);
63     }
64     
65     @Override
66     public int hashCode() {
67         final int prime = 31;
68         int result = 1;
69         result = prime * result + ((name == null) ? 0 : name.hashCode());
70         result = prime * result
71                 + ((parameters == null) ? 0 : parameters.hashCode());
72         result = prime * result
73                 + ((returnType == null) ? 0 : returnType.hashCode());
74         return result;
75     }
76
77     @Override
78     public boolean equals(Object obj) {
79         if (this == obj) {
80             return true;
81         }
82         if (obj == null) {
83             return false;
84         }
85         if (getClass() != obj.getClass()) {
86             return false;
87         }
88         MethodSignatureBuilderImpl other = (MethodSignatureBuilderImpl) obj;
89         if (name == null) {
90             if (other.name != null) {
91                 return false;
92             }
93         } else if (!name.equals(other.name)) {
94             return false;
95         }
96         if (parameters == null) {
97             if (other.parameters != null) {
98                 return false;
99             }
100         } else if (!parameters.equals(other.parameters)) {
101             return false;
102         }
103         if (returnType == null) {
104             if (other.returnType != null) {
105                 return false;
106             }
107         } else if (!returnType.equals(other.returnType)) {
108             return false;
109         }
110         return true;
111     }
112
113     @Override
114     public String toString() {
115         StringBuilder builder = new StringBuilder();
116         builder.append("MethodSignatureBuilderImpl [name=");
117         builder.append(name);
118         builder.append(", returnType=");
119         builder.append(returnType);
120         builder.append(", parameters=");
121         builder.append(parameters);
122         builder.append(", annotationBuilders=");
123         builder.append(annotationBuilders);
124         builder.append(", comment=");
125         builder.append(comment);
126         if (parent != null) {
127             builder.append(", parent=");
128             builder.append(parent.getPackageName());
129             builder.append(".");
130             builder.append(parent.getName());
131         } else {
132             builder.append(", parent=null");
133         }
134         builder.append("]");
135         return builder.toString();
136     }
137
138     private static final class MethodSignatureImpl implements MethodSignature {
139
140         private final String name;
141         private final String comment;
142         private final Type definingType;
143         private final Type returnType;
144         private final List<Parameter> params;
145         private List<AnnotationType> annotations;
146
147         public MethodSignatureImpl(final Type definingType, final String name,
148                 final List<AnnotationTypeBuilder> annotationBuilders,
149                 final String comment, final Type returnType,
150                 final List<Parameter> params) {
151             super();
152             this.name = name;
153             this.comment = comment;
154             this.definingType = definingType;
155             this.returnType = returnType;
156             this.params = Collections.unmodifiableList(params);
157             
158             this.annotations = new ArrayList<AnnotationType>();
159             for (final AnnotationTypeBuilder builder : annotationBuilders) {
160                 this.annotations.add(builder.toInstance());
161             }
162             this.annotations = Collections.unmodifiableList(this.annotations);
163         }
164
165         @Override
166         public List<AnnotationType> getAnnotations() {
167             return annotations;
168         }
169
170         @Override
171         public String getName() {
172             return name;
173         }
174
175         @Override
176         public String getComment() {
177             return comment;
178         }
179
180         @Override
181         public Type getDefiningType() {
182             return definingType;
183         }
184
185         @Override
186         public Type getReturnType() {
187             return returnType;
188         }
189
190         @Override
191         public List<Parameter> getParameters() {
192             return params;
193         }
194
195         @Override
196         public AccessModifier getAccessModifier() {
197             return AccessModifier.PUBLIC;
198         }
199
200         @Override
201         public int hashCode() {
202             final int prime = 31;
203             int result = 1;
204             result = prime * result + ((name == null) ? 0 : name.hashCode());
205             result = prime * result
206                     + ((params == null) ? 0 : params.hashCode());
207             result = prime * result
208                     + ((returnType == null) ? 0 : returnType.hashCode());
209             return result;
210         }
211
212         @Override
213         public boolean equals(Object obj) {
214             if (this == obj) {
215                 return true;
216             }
217             if (obj == null) {
218                 return false;
219             }
220             if (getClass() != obj.getClass()) {
221                 return false;
222             }
223             MethodSignatureImpl other = (MethodSignatureImpl) obj;
224             if (name == null) {
225                 if (other.name != null) {
226                     return false;
227                 }
228             } else if (!name.equals(other.name)) {
229                 return false;
230             }
231             if (params == null) {
232                 if (other.params != null) {
233                     return false;
234                 }
235             } else if (!params.equals(other.params)) {
236                 return false;
237             }
238             if (returnType == null) {
239                 if (other.returnType != null) {
240                     return false;
241                 }
242             } else if (!returnType.equals(other.returnType)) {
243                 return false;
244             }
245             return true;
246         }
247
248         @Override
249         public String toString() {
250             StringBuilder builder = new StringBuilder();
251             builder.append("MethodSignatureImpl [name=");
252             builder.append(name);
253             builder.append(", comment=");
254             builder.append(comment);
255             if (definingType != null) {
256                 builder.append(", definingType=");
257                 builder.append(definingType.getPackageName());
258                 builder.append(".");
259                 builder.append(definingType.getName());
260             } else {
261                 builder.append(", definingType= null");
262             }
263             builder.append(", returnType=");
264             builder.append(returnType);
265             builder.append(", params=");
266             builder.append(params);
267             builder.append(", annotations=");
268             builder.append(annotations);
269             builder.append("]");
270             return builder.toString();
271         }
272     }
273 }