673d37e86b532cd6fa2fcf506d00e3b014bd384f
[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 /*\r
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 package org.opendaylight.controller.sal.java.api.generator;\r
9 \r
10 import static org.opendaylight.controller.sal.java.api.generator.Constants.*;\r
11 \r
12 import java.util.List;\r
13 \r
14 import org.opendaylight.controller.sal.binding.model.api.Constant;\r
15 import org.opendaylight.controller.sal.binding.model.api.Enumeration;\r
16 import org.opendaylight.controller.sal.binding.model.api.MethodSignature;\r
17 import org.opendaylight.controller.sal.binding.model.api.ParameterizedType;\r
18 import org.opendaylight.controller.sal.binding.model.api.Type;\r
19 import org.opendaylight.controller.sal.binding.model.api.Enumeration.Pair;\r
20 import org.opendaylight.controller.sal.binding.model.api.MethodSignature.Parameter;\r
21 \r
22 public class GeneratorUtil {\r
23 \r
24     private GeneratorUtil() {\r
25     }\r
26 \r
27     public static String createIfcDeclarationWithPkgName(String packageName,\r
28             String name, String indent) {\r
29         return createFileDeclarationWithPkgName(IFC, packageName, name, indent);\r
30     }\r
31 \r
32     public static String createClassDeclarationWithPkgName(String packageName,\r
33             String name, String indent) {\r
34         return createFileDeclarationWithPkgName(CLASS, packageName, name,\r
35                 indent);\r
36     }\r
37 \r
38     private static String createFileDeclarationWithPkgName(String type,\r
39             String packageName, String name, String indent) {\r
40         StringBuilder sb = new StringBuilder();\r
41         sb.append(PKG + GAP + packageName + SC);\r
42         sb.append(NL);\r
43         sb.append(NL);\r
44         sb.append(PUBLIC + GAP + type + GAP + name + GAP + LCB);\r
45         return sb.toString();\r
46     }\r
47 \r
48     public static String createConstant(Constant constant, String indent) {\r
49         StringBuilder sb = new StringBuilder();\r
50         sb.append(indent + PUBLIC + GAP + STATIC + GAP + FINAL + GAP);\r
51         sb.append(getExplicitType(constant.getType()) + GAP\r
52                 + constant.getName());\r
53         sb.append(GAP + "=" + GAP);\r
54         sb.append(constant.getValue() + SC);\r
55         return sb.toString();\r
56     }\r
57 \r
58     public static String createField(Constant field, String indent) {\r
59         StringBuilder sb = new StringBuilder();\r
60         sb.append(indent + PRIVATE + GAP);\r
61         sb.append(getExplicitType(field.getType()) + GAP + field.getName());\r
62         sb.append(GAP + "=" + GAP);\r
63         sb.append(field.getValue() + SC);\r
64         return sb.toString();\r
65     }\r
66 \r
67     /**\r
68      * Create method declaration in interface.\r
69      * \r
70      * @param method\r
71      * @param indent\r
72      * @return\r
73      */\r
74     public static String createMethodDeclaration(MethodSignature method,\r
75             String indent) {\r
76         String comment = method.getComment();\r
77         Type type = method.getReturnType();\r
78         String name = method.getName();\r
79         List<Parameter> parameters = method.getParameters();\r
80 \r
81         StringBuilder sb = new StringBuilder();\r
82         createComment(sb, comment, indent);\r
83 \r
84         sb.append(indent + getExplicitType(type) + GAP + name);\r
85         sb.append(LB);\r
86         for (int i = 0; i < parameters.size(); i++) {\r
87             Parameter p = parameters.get(i);\r
88             String separator = COMMA;\r
89             if (i + 1 == parameters.size()) {\r
90                 separator = "";\r
91             }\r
92             sb.append(getExplicitType(p.getType()) + GAP + p.getName()\r
93                     + separator);\r
94         }\r
95         sb.append(RB);\r
96         sb.append(SC);\r
97 \r
98         return sb.toString();\r
99     }\r
100 \r
101     public static String createGetter(Constant field, String indent) {\r
102         StringBuilder sb = new StringBuilder();\r
103 \r
104         Type type = field.getType();\r
105         String varName = field.getName();\r
106         char first = Character.toUpperCase(varName.charAt(0));\r
107         String methodName = "get" + first + varName.substring(1);\r
108 \r
109         sb.append(indent + PUBLIC + GAP + getExplicitType(type) + GAP\r
110                 + methodName);\r
111         sb.append(LB + RB + LCB + NL);\r
112 \r
113         String currentIndent = indent + TAB;\r
114 \r
115         sb.append(currentIndent + "return " + varName + SC + NL);\r
116 \r
117         sb.append(indent + RCB);\r
118         return sb.toString();\r
119     }\r
120 \r
121     public static String createHashCode(List<Constant> fields, String indent) {\r
122         StringBuilder sb = new StringBuilder();\r
123         sb.append(indent + "public int hashCode() {" + NL);\r
124         sb.append(indent + TAB + "final int prime = 31;" + NL);\r
125         sb.append(indent + TAB + "int result = 1;" + NL);\r
126 \r
127         for (Constant field : fields) {\r
128             String fieldName = field.getName();\r
129             sb.append(indent + TAB + "result = prime * result + ((" + fieldName\r
130                     + " == null) ? 0 : " + fieldName + ".hashCode());" + NL);\r
131         }\r
132 \r
133         sb.append(indent + TAB + "return result;" + NL);\r
134         sb.append(indent + RCB + NL);\r
135         return sb.toString();\r
136     }\r
137 \r
138     public static String createEquals(Type type, List<Constant> fields,\r
139             String indent) {\r
140         StringBuilder sb = new StringBuilder();\r
141         final String indent1 = indent + TAB;\r
142         final String indent2 = indent + TAB + TAB;\r
143         final String indent3 = indent + TAB + TAB + TAB;\r
144 \r
145         sb.append(indent + "public boolean equals(Object obj) {" + NL);\r
146         sb.append(indent1 + "if (this == obj) {" + NL);\r
147         sb.append(indent2 + "return true;" + NL);\r
148         sb.append(indent1 + "}" + NL);\r
149         sb.append(indent1 + "if (obj == null) {" + NL);\r
150         sb.append(indent2 + "return false;" + NL);\r
151         sb.append(indent1 + "}" + NL);\r
152         sb.append(indent1 + "if (getClass() != obj.getClass()) {" + NL);\r
153         sb.append(indent2 + "return false;" + NL);\r
154         sb.append(indent1 + "}" + NL);\r
155 \r
156         String typeStr = type.getPackageName() + "." + type.getName();\r
157         sb.append(indent1 + typeStr + " other = (" + typeStr + ") obj;" + NL);\r
158 \r
159         for (Constant field : fields) {\r
160             String fieldName = field.getName();\r
161             sb.append(indent1 + "if (" + fieldName + " == null) {" + NL);\r
162             sb.append(indent2 + "if (other." + fieldName + " != null) {" + NL);\r
163             sb.append(indent3 + "return false;" + NL);\r
164             sb.append(indent2 + "}" + NL);\r
165             sb.append(indent1 + "} else if (!" + fieldName + ".equals(other."\r
166                     + fieldName + ")) {" + NL);\r
167             sb.append(indent2 + "return false;" + NL);\r
168             sb.append(indent1 + "}" + NL);\r
169         }\r
170 \r
171         sb.append(indent1 + "return true;" + NL);\r
172 \r
173         sb.append(indent + RCB + NL);\r
174         return sb.toString();\r
175     }\r
176 \r
177     public static String createToString(Type type, List<Constant> fields,\r
178             String indent) {\r
179         StringBuilder sb = new StringBuilder();\r
180         String typeStr = type.getPackageName() + "." + type.getName();\r
181 \r
182         sb.append(indent + "public String toString() {" + NL);\r
183         sb.append(indent + TAB + "return \"" + typeStr + "[");\r
184 \r
185         boolean first = true;\r
186         for (Constant field : fields) {\r
187             String fieldName = field.getName();\r
188             String fieldType = field.getType().getPackageName() + "."\r
189                     + field.getType().getName();\r
190             if (first) {\r
191                 if (fieldType.equals("java.lang.String")) {\r
192                     sb.append(fieldName + "=\\\""\r
193                             + parseStringValue((String) field.getValue())\r
194                             + "\\\"");\r
195                 } else {\r
196                     sb.append(fieldName + "=" + field.getValue() + "");\r
197                 }\r
198             } else {\r
199                 if (fieldType.equals("java.lang.String")) {\r
200                     sb.append(", " + fieldName + "=\\\""\r
201                             + parseStringValue((String) field.getValue())\r
202                             + "\\\"");\r
203                 } else {\r
204                     sb.append(", " + fieldName + "=" + field.getValue() + "");\r
205                 }\r
206 \r
207             }\r
208             first = false;\r
209         }\r
210         sb.append("]\"" + SC + NL);\r
211 \r
212         sb.append(indent + RCB + NL);\r
213         return sb.toString();\r
214     }\r
215 \r
216     /**\r
217      * Remove starting and ending quote sign\r
218      * \r
219      * @param o\r
220      * @return\r
221      */\r
222     private static String parseStringValue(String str) {\r
223         return str.substring(1, str.length() - 1);\r
224     }\r
225 \r
226     public static String createEnum(Enumeration e, String indent) {\r
227         StringBuilder sb = new StringBuilder(indent + ENUM + GAP + e.getName()\r
228                 + GAP + LCB + NL);\r
229 \r
230         String separator = COMMA;\r
231         List<Pair> values = e.getValues();\r
232         sb.append(indent + TAB);\r
233         for (int i = 0; i < values.size(); i++) {\r
234             if (i + 1 == values.size()) {\r
235                 separator = SC;\r
236             }\r
237             sb.append(values.get(i).getName() + separator);\r
238         }\r
239         sb.append(NL);\r
240         sb.append(indent + RCB);\r
241         return sb.toString();\r
242     }\r
243 \r
244     private static String getExplicitType(Type type) {\r
245         String packageName = type.getPackageName();\r
246         if (packageName.endsWith(".")) {\r
247             packageName = packageName.substring(0, packageName.length() - 1);\r
248         }\r
249         StringBuilder sb = new StringBuilder(packageName + "." + type.getName());\r
250         if (type instanceof ParameterizedType) {\r
251             ParameterizedType pType = (ParameterizedType) type;\r
252             Type[] pTypes = pType.getActualTypeArguments();\r
253             sb.append("<");\r
254             sb.append(getParameters(pTypes));\r
255             sb.append(">");\r
256         }\r
257         if (sb.toString().equals("java.lang.Void")) {\r
258             return "void";\r
259         }\r
260         return sb.toString();\r
261     }\r
262 \r
263     private static String getParameters(Type[] pTypes) {\r
264         StringBuilder sb = new StringBuilder();\r
265         for (int i = 0; i < pTypes.length; i++) {\r
266             Type t = pTypes[i];\r
267 \r
268             String separator = COMMA;\r
269             if (i + 1 == pTypes.length) {\r
270                 separator = "";\r
271             }\r
272             sb.append(getExplicitType(t) + separator);\r
273         }\r
274         return sb.toString();\r
275     }\r
276 \r
277     private static void createComment(StringBuilder sb, String comment,\r
278             String indent) {\r
279         if (comment != null && comment.length() > 0) {\r
280             sb.append(indent + "/*" + NL);\r
281             sb.append(indent + comment + NL);\r
282             sb.append(indent + "*/" + NL);\r
283         }\r
284     }\r
285 \r
286 }\r

©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.