Added generation of Transfer Objects from Type Definitions.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-java-api-generator / src / main / java / org / opendaylight / controller / sal / java / api / generator / GeneratorUtil.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.sal.java.api.generator;
9
10 import static org.opendaylight.controller.sal.java.api.generator.Constants.CLASS;
11 import static org.opendaylight.controller.sal.java.api.generator.Constants.COMMA;
12 import static org.opendaylight.controller.sal.java.api.generator.Constants.ENUM;
13 import static org.opendaylight.controller.sal.java.api.generator.Constants.FINAL;
14 import static org.opendaylight.controller.sal.java.api.generator.Constants.GAP;
15 import static org.opendaylight.controller.sal.java.api.generator.Constants.IFC;
16 import static org.opendaylight.controller.sal.java.api.generator.Constants.LB;
17 import static org.opendaylight.controller.sal.java.api.generator.Constants.LCB;
18 import static org.opendaylight.controller.sal.java.api.generator.Constants.NL;
19 import static org.opendaylight.controller.sal.java.api.generator.Constants.PKG;
20 import static org.opendaylight.controller.sal.java.api.generator.Constants.PRIVATE;
21 import static org.opendaylight.controller.sal.java.api.generator.Constants.PUBLIC;
22 import static org.opendaylight.controller.sal.java.api.generator.Constants.RB;
23 import static org.opendaylight.controller.sal.java.api.generator.Constants.RCB;
24 import static org.opendaylight.controller.sal.java.api.generator.Constants.SC;
25 import static org.opendaylight.controller.sal.java.api.generator.Constants.STATIC;
26 import static org.opendaylight.controller.sal.java.api.generator.Constants.TAB;
27
28 import java.util.List;
29
30 import org.opendaylight.controller.binding.generator.util.BindingGeneratorUtil;
31 import org.opendaylight.controller.sal.binding.model.api.AnnotationType;
32 import org.opendaylight.controller.sal.binding.model.api.Constant;
33 import org.opendaylight.controller.sal.binding.model.api.Enumeration;
34 import org.opendaylight.controller.sal.binding.model.api.Enumeration.Pair;
35 import org.opendaylight.controller.sal.binding.model.api.GeneratedProperty;
36 import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;
37 import org.opendaylight.controller.sal.binding.model.api.GeneratedType;
38 import org.opendaylight.controller.sal.binding.model.api.MethodSignature;
39 import org.opendaylight.controller.sal.binding.model.api.MethodSignature.Parameter;
40 import org.opendaylight.controller.sal.binding.model.api.ParameterizedType;
41 import org.opendaylight.controller.sal.binding.model.api.Type;
42
43 public class GeneratorUtil {
44
45     private GeneratorUtil() {
46     }
47
48     public static String createIfcDeclarationWithPkgName(
49             final GeneratedType genType, final String indent) {
50         return createFileDeclarationWithPkgName(IFC, genType, indent);
51     }
52
53     public static String createClassDeclarationWithPkgName(
54             final GeneratedTransferObject genTransferObject, final String indent) {
55         return createFileDeclarationWithPkgName(CLASS, genTransferObject,
56                 indent);
57     }
58
59     private static String createFileDeclarationWithPkgName(final String type,
60             final GeneratedType genType, final String indent) {
61         final StringBuilder builder = new StringBuilder();
62         builder.append(PKG + GAP + genType.getPackageName() + SC);
63         builder.append(NL);
64         builder.append(NL);
65         createComment(builder, genType.getComment(), indent);
66         
67         if (!genType.getAnnotations().isEmpty()) {
68             final List<AnnotationType> annotations = genType.getAnnotations();
69             appendAnnotations(builder, annotations);
70             builder.append(NL);
71         }
72         builder.append(PUBLIC + GAP + type + GAP + genType.getName() + GAP
73                 + LCB);
74         return builder.toString();
75     }
76
77     private static StringBuilder appendAnnotations(final StringBuilder builder,
78             final List<AnnotationType> annotations) {
79         if ((builder != null) && (annotations != null)) {
80             for (final AnnotationType annotation : annotations) {
81                 builder.append("@");
82                 builder.append(annotation.getPackageName());
83                 builder.append(".");
84                 builder.append(annotation.getName());
85
86                 if (annotation.containsParameters()) {
87                     builder.append("(");
88                     final List<AnnotationType.Parameter> parameters = annotation
89                             .getParameters();
90                     appendAnnotationParams(builder, parameters);
91                     builder.append(")");
92                 }
93             }
94         }
95         return builder;
96     }
97
98     private static StringBuilder appendAnnotationParams(
99             final StringBuilder builder,
100             final List<AnnotationType.Parameter> parameters) {
101         if (parameters != null) {
102             int i = 0;
103             for (final AnnotationType.Parameter param : parameters) {
104                 if (param == null) {
105                     continue;
106                 }
107                 if (i > 0) {
108                     builder.append(", ");
109                 }
110                 final String paramName = param.getName();
111                 if (param.getValue() != null) {
112                     builder.append(paramName);
113                     builder.append(" = ");
114                     builder.append(param.getValue());
115                 } else {
116                     builder.append(paramName);
117                     builder.append(" = {");
118                     final List<String> values = param.getValues();
119                     builder.append(values.get(0));
120                     for (int j = 1; j < values.size(); ++j) {
121                         builder.append(", ");
122                         builder.append(values.get(j));
123                     }
124                     builder.append("}");
125                 }
126                 i++;
127             }
128         }
129         return builder;
130     }
131
132     public static String createConstant(final Constant constant,
133             final String indent) {
134         final StringBuilder builder = new StringBuilder();
135         builder.append(indent + PUBLIC + GAP + STATIC + GAP + FINAL + GAP);
136         builder.append(getExplicitType(constant.getType()) + GAP
137                 + constant.getName());
138         builder.append(GAP + "=" + GAP);
139         builder.append(constant.getValue() + SC);
140         return builder.toString();
141     }
142
143     public static String createField(final GeneratedProperty property,
144             final String indent) {
145         final StringBuilder builder = new StringBuilder();
146         builder.append(indent);
147         if (!property.getAnnotations().isEmpty()) {
148             final List<AnnotationType> annotations = property.getAnnotations();
149             appendAnnotations(builder, annotations);
150             builder.append(NL);
151         }
152         builder.append(indent + PRIVATE + GAP);
153         builder.append(getExplicitType(property.getReturnType()) + GAP
154                 + property.getName());
155         builder.append(SC);
156         return builder.toString();
157     }
158
159     /**
160      * Create method declaration in interface.
161      * 
162      * @param method
163      * @param indent
164      * @return
165      */
166     public static String createMethodDeclaration(final MethodSignature method,
167             final String indent) {
168         final String comment = method.getComment();
169         final Type type = method.getReturnType();
170         final String name = method.getName();
171         final List<Parameter> parameters = method.getParameters();
172
173         final StringBuilder builder = new StringBuilder();
174         createComment(builder, comment, indent);
175         builder.append(NL);
176         builder.append(indent);
177
178         if (!method.getAnnotations().isEmpty()) {
179             final List<AnnotationType> annotations = method.getAnnotations();
180             appendAnnotations(builder, annotations);
181             builder.append(NL);
182         }
183
184         builder.append(indent + getExplicitType(type) + GAP + name);
185         builder.append(LB);
186         for (int i = 0; i < parameters.size(); i++) {
187             Parameter p = parameters.get(i);
188             String separator = COMMA;
189             if (i + 1 == parameters.size()) {
190                 separator = "";
191             }
192             builder.append(getExplicitType(p.getType()) + GAP
193                     + BindingGeneratorUtil.validateParameterName(p.getName())
194                     + separator);
195         }
196         builder.append(RB);
197         builder.append(SC);
198
199         return builder.toString();
200     }
201
202     public static String createConstructor(
203             GeneratedTransferObject genTransferObject, final String indent) {
204         final StringBuilder builder = new StringBuilder();
205
206         final List<GeneratedProperty> properties = genTransferObject
207                 .getProperties();
208         builder.append(indent);
209         builder.append(PUBLIC);
210         builder.append(GAP);
211         builder.append(genTransferObject.getName());
212         builder.append(LB);
213
214         boolean first = true;
215         if (properties != null) {
216             for (final GeneratedProperty property : properties) {
217                 if (first) {
218                     builder.append(getExplicitType(property.getReturnType()));
219                     builder.append(" ");
220                     builder.append(property.getName());
221                     first = false;
222                 } else {
223                     builder.append(", ");
224                     builder.append(getExplicitType(property.getReturnType()));
225                     builder.append(builder.append(" "));
226                     builder.append(property.getName());
227                 }
228             }
229         }
230
231         builder.append(RB);
232         builder.append(GAP);
233         builder.append(LCB);
234         builder.append(NL);
235         builder.append(indent);
236         builder.append(TAB);
237         builder.append("super();");
238         builder.append(NL);
239
240         if (properties != null) {
241             for (final GeneratedProperty property : properties) {
242                 builder.append(indent);
243                 builder.append(TAB);
244                 builder.append("this.");
245                 builder.append(property.getName());
246                 builder.append(" = ");
247                 builder.append(property.getName());
248                 builder.append(SC);
249                 builder.append(NL);
250             }
251         }
252
253         builder.append(indent);
254         builder.append(RCB);
255
256         return builder.toString();
257     }
258
259     public static String createGetter(final GeneratedProperty property,
260             final String indent) {
261         final StringBuilder builder = new StringBuilder();
262
263         final Type type = property.getReturnType();
264         final String varName = property.getName();
265         final char first = Character.toUpperCase(varName.charAt(0));
266         final String methodName = "get" + first + varName.substring(1);
267
268         builder.append(indent + PUBLIC + GAP + getExplicitType(type) + GAP
269                 + methodName);
270         builder.append(LB + RB + LCB + NL);
271
272         String currentIndent = indent + TAB;
273
274         builder.append(currentIndent + "return " + varName + SC + NL);
275
276         builder.append(indent + RCB);
277         return builder.toString();
278     }
279
280     public static String createHashCode(
281             final List<GeneratedProperty> properties, final String indent) {
282         StringBuilder builder = new StringBuilder();
283         builder.append(indent + "public int hashCode() {" + NL);
284         builder.append(indent + TAB + "final int prime = 31;" + NL);
285         builder.append(indent + TAB + "int result = 1;" + NL);
286
287         for (GeneratedProperty property : properties) {
288             String fieldName = property.getName();
289             builder.append(indent + TAB + "result = prime * result + (("
290                     + fieldName + " == null) ? 0 : " + fieldName
291                     + ".hashCode());" + NL);
292         }
293
294         builder.append(indent + TAB + "return result;" + NL);
295         builder.append(indent + RCB + NL);
296         return builder.toString();
297     }
298
299     public static String createEquals(final GeneratedTransferObject type,
300             final List<GeneratedProperty> properties, final String indent) {
301         StringBuilder builder = new StringBuilder();
302         final String indent1 = indent + TAB;
303         final String indent2 = indent + TAB + TAB;
304         final String indent3 = indent + TAB + TAB + TAB;
305
306         builder.append(indent + "public boolean equals(Object obj) {" + NL);
307         builder.append(indent1 + "if (this == obj) {" + NL);
308         builder.append(indent2 + "return true;" + NL);
309         builder.append(indent1 + "}" + NL);
310         builder.append(indent1 + "if (obj == null) {" + NL);
311         builder.append(indent2 + "return false;" + NL);
312         builder.append(indent1 + "}" + NL);
313         builder.append(indent1 + "if (getClass() != obj.getClass()) {" + NL);
314         builder.append(indent2 + "return false;" + NL);
315         builder.append(indent1 + "}" + NL);
316
317         String typeStr = type.getPackageName() + "." + type.getName();
318         builder.append(indent1 + typeStr + " other = (" + typeStr + ") obj;"
319                 + NL);
320
321         for (GeneratedProperty property : properties) {
322             String fieldName = property.getName();
323             builder.append(indent1 + "if (" + fieldName + " == null) {" + NL);
324             builder.append(indent2 + "if (other." + fieldName + " != null) {"
325                     + NL);
326             builder.append(indent3 + "return false;" + NL);
327             builder.append(indent2 + "}" + NL);
328             builder.append(indent1 + "} else if (!" + fieldName
329                     + ".equals(other." + fieldName + ")) {" + NL);
330             builder.append(indent2 + "return false;" + NL);
331             builder.append(indent1 + "}" + NL);
332         }
333
334         builder.append(indent1 + "return true;" + NL);
335
336         builder.append(indent + RCB + NL);
337         return builder.toString();
338     }
339
340     public static String createToString(final GeneratedTransferObject type,
341             final List<GeneratedProperty> properties, final String indent) {
342         StringBuilder builder = new StringBuilder();
343         builder.append(indent);
344         builder.append("public String toString() {");
345         builder.append(NL);
346         builder.append(indent);
347         builder.append(TAB);
348         builder.append("StringBuilder builder = new StringBuilder();");
349         builder.append(NL);
350         builder.append(indent);
351         builder.append(TAB);
352         builder.append("builder.append(\"");
353         builder.append(type.getName());
354         builder.append(" [");
355
356         boolean first = true;
357         for (GeneratedProperty property : properties) {
358             if (first) {
359                 builder.append(property.getName());
360                 builder.append("=\");");
361                 builder.append(NL);
362                 builder.append(indent);
363                 builder.append(TAB);
364                 builder.append("builder.append(");
365                 builder.append(property.getName());
366                 builder.append(");");
367                 first = false;
368             } else {
369                 builder.append(NL);
370                 builder.append(indent);
371                 builder.append(TAB);
372                 builder.append("builder.append(\", ");
373                 builder.append(property.getName());
374                 builder.append("=\");");
375                 builder.append(NL);
376                 builder.append(indent);
377                 builder.append(TAB);
378                 builder.append("builder.append(\", ");
379                 builder.append(property.getName());
380                 builder.append(");");
381             }
382         }
383         builder.append(NL);
384         builder.append(indent);
385         builder.append(TAB);
386         builder.append("builder.append(\"]\");");
387         builder.append(NL);
388         builder.append(indent);
389         builder.append(TAB);
390         builder.append("return builder.toString();");
391
392         builder.append(NL);
393         builder.append(indent);
394         builder.append(RCB);
395         builder.append(NL);
396         return builder.toString();
397     }
398
399     public static String createEnum(final Enumeration enumeration,
400             final String indent) {
401         final StringBuilder builder = new StringBuilder(indent + ENUM + GAP
402                 + enumeration.getName() + GAP + LCB + NL);
403
404         String separator = COMMA;
405         final List<Pair> values = enumeration.getValues();
406         builder.append(indent + TAB);
407         for (int i = 0; i < values.size(); i++) {
408             if (i + 1 == values.size()) {
409                 separator = SC;
410             }
411             builder.append(values.get(i).getName() + separator);
412         }
413         builder.append(NL);
414         builder.append(indent + RCB);
415         return builder.toString();
416     }
417
418     private static String getExplicitType(final Type type) {
419         String packageName = type.getPackageName();
420         if (packageName.endsWith(".")) {
421             packageName = packageName.substring(0, packageName.length() - 1);
422         }
423         final StringBuilder builder = new StringBuilder(packageName + "."
424                 + type.getName());
425         if (type instanceof ParameterizedType) {
426             ParameterizedType pType = (ParameterizedType) type;
427             Type[] pTypes = pType.getActualTypeArguments();
428             builder.append("<");
429             builder.append(getParameters(pTypes));
430             builder.append(">");
431         }
432         if (builder.toString().equals("java.lang.Void")) {
433             return "void";
434         }
435         return builder.toString();
436     }
437
438     private static String getParameters(final Type[] pTypes) {
439         final StringBuilder builder = new StringBuilder();
440         for (int i = 0; i < pTypes.length; i++) {
441             Type t = pTypes[i];
442
443             String separator = COMMA;
444             if (i + 1 == pTypes.length) {
445                 separator = "";
446             }
447             builder.append(getExplicitType(t) + separator);
448         }
449         return builder.toString();
450     }
451
452     private static void createComment(final StringBuilder builder,
453             final String comment, final String indent) {
454         if (comment != null && comment.length() > 0) {
455             builder.append(indent + "/*" + NL);
456             builder.append(indent + comment + NL);
457             builder.append(indent + "*/" + NL);
458         }
459     }
460
461 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.