blob: e2e621158bf35026b2c105793d6a63f0b888e24c [file] [log] [blame]
Bharat saraswal870c56f2016-02-20 21:57:16 +05301/*
2 * Copyright 2016 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.yangutils.translator.tojava.utils;
18
Bharat saraswal870c56f2016-02-20 21:57:16 +053019import org.onosproject.yangutils.translator.tojava.AttributeInfo;
Bharat saraswal870c56f2016-02-20 21:57:16 +053020import org.onosproject.yangutils.utils.UtilConstants;
21import org.onosproject.yangutils.utils.io.impl.JavaDocGen;
22
23/**
24 * Generated methods for generated files based on the file type.
25 */
26public final class MethodsGenerator {
27
Bharat saraswal870c56f2016-02-20 21:57:16 +053028 /**
29 * Default constructor.
30 */
31 private MethodsGenerator() {
32 }
33
34 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +053035 * Returns the methods strings for builder class.
36 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053037 * @param attr attribute info
38 * @param className java class name
39 * @return method string for builder class
Bharat saraswal870c56f2016-02-20 21:57:16 +053040 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +053041 static String parseBuilderMethodString(AttributeInfo attr, String className) {
42 String attrQuaifiedType = "";
43 if (attr.getImportInfo().getPkgInfo() != null) {
44 attrQuaifiedType = attr.getImportInfo().getPkgInfo() + ".";
45 }
46 attrQuaifiedType = attrQuaifiedType + attr.getImportInfo().getClassInfo();
Bharat saraswal870c56f2016-02-20 21:57:16 +053047
Vinod Kumar Sc4216002016-03-03 19:55:30 +053048 String overrideString = UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
49 + UtilConstants.OVERRIDE + UtilConstants.NEW_LINE;
50 String getterString = getGetterForClass(attr.getAttributeName(), attrQuaifiedType);
51 String setterString = getSetterForClass(attr.getAttributeName(), attrQuaifiedType, className);
Bharat saraswal4bf8b152016-02-25 02:26:43 +053052 return overrideString + getterString + UtilConstants.NEW_LINE + overrideString + setterString;
Bharat saraswal870c56f2016-02-20 21:57:16 +053053 }
54
55 /**
56 * Returns the methods strings for builder class.
57 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053058 * @param attr attribute info
59 * @return method string for builder class
Bharat saraswal870c56f2016-02-20 21:57:16 +053060 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +053061 static String parseImplMethodString(AttributeInfo attr) {
62
63 String attrQuaifiedType = "";
64 if (attr.getImportInfo().getPkgInfo() != null) {
65 attrQuaifiedType = attr.getImportInfo().getPkgInfo() + ".";
66 }
67 attrQuaifiedType = attrQuaifiedType + attr.getImportInfo().getClassInfo();
Bharat saraswal870c56f2016-02-20 21:57:16 +053068
69 return UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.OVERRIDE
Vinod Kumar Sc4216002016-03-03 19:55:30 +053070 + UtilConstants.NEW_LINE + getGetterForClass(attr.getAttributeName(), attrQuaifiedType);
Bharat saraswal870c56f2016-02-20 21:57:16 +053071 }
72
73 /**
74 * Returns the methods strings for builder interface.
75 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053076 * @param attr attribute info
77 * @param className name of the java class being generated
78 * @return method string for builder interface
Bharat saraswal870c56f2016-02-20 21:57:16 +053079 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +053080 static String parseBuilderInterfaceMethodString(AttributeInfo attr, String className) {
Bharat saraswal870c56f2016-02-20 21:57:16 +053081
Vinod Kumar Sc4216002016-03-03 19:55:30 +053082 return getGetterString(attr) + UtilConstants.NEW_LINE + getSetterString(attr, className);
Bharat saraswal870c56f2016-02-20 21:57:16 +053083 }
84
85 /**
86 * Returns the methods strings for builder interface.
87 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053088 * @param name attribute name
89 * @return method string for builder interface
Bharat saraswal870c56f2016-02-20 21:57:16 +053090 */
91 public static String parseBuilderInterfaceBuildMethodString(String name) {
92
Vinod Kumar Sc4216002016-03-03 19:55:30 +053093 return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.BUILD, name)
94 + getBuildForInterface(name);
Bharat saraswal870c56f2016-02-20 21:57:16 +053095 }
96
97 /**
98 * Returns getter string.
99 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530100 * @param attr attribute info
Bharat saraswal870c56f2016-02-20 21:57:16 +0530101 * @return getter string
102 */
103 public static String getGetterString(AttributeInfo attr) {
104
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530105 String returnType = "";
106 if (attr.getImportInfo().getPkgInfo() != null) {
107 returnType = attr.getImportInfo().getPkgInfo() + ".";
108 }
109
110 returnType = returnType + attr.getImportInfo().getClassInfo();
111
Bharat saraswal870c56f2016-02-20 21:57:16 +0530112 return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.GETTER, attr.getAttributeName())
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530113 + getGetterForInterface(attr.getAttributeName(), returnType)
Bharat saraswal870c56f2016-02-20 21:57:16 +0530114 + UtilConstants.NEW_LINE;
115 }
116
117 /**
118 * Returns setter string.
119 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530120 * @param attr attribute info
121 * @param className java class name
Bharat saraswal870c56f2016-02-20 21:57:16 +0530122 * @return setter string
123 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530124 private static String getSetterString(AttributeInfo attr, String className) {
125
126 String attrType = "";
127 if (attr.getImportInfo().getPkgInfo() != null) {
128 attrType = attr.getImportInfo().getPkgInfo() + ".";
129 }
130
131 attrType = attrType + attr.getImportInfo().getClassInfo();
Bharat saraswal870c56f2016-02-20 21:57:16 +0530132
133 return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.SETTER, attr.getAttributeName())
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530134 + getSetterForInterface(attr.getAttributeName(), attrType, className);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530135 }
136
137 /**
138 * Returns constructor method string.
139 *
140 * @param name class name
141 * @return constructor string
142 */
143 public static String getConstructorString(String name) {
144
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530145 return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.CONSTRUCTOR, name)
146 + getConstructor(name);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530147 }
148
149 /**
150 * Returns default constructor method string.
151 *
152 * @param type generated file type
153 * @param name class name
154 * @return default constructor string
155 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530156 public static String getDefaultConstructorString(int type, String name) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530157
158 return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.DEFAULT_CONSTRUCTOR, name)
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530159 + getDefaultConstructor(name + UtilConstants.BUILDER);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530160 }
161
162 /**
163 * Returns build method string.
164 *
165 * @param name class name
166 * @return build string
167 */
168 public static String getBuildString(String name) {
169
170 return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.OVERRIDE + UtilConstants.NEW_LINE
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530171 + getBuild(name);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530172 }
173
174 /**
175 * Returns the getter method strings for class file.
176 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530177 * @param attrName name of the attribute
178 * @param attrType return type of attribute
179 * @return getter method for class
Bharat saraswal870c56f2016-02-20 21:57:16 +0530180 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530181 private static String getGetterForClass(String attrName, String attrType) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530182
183 return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530184 + attrType + UtilConstants.SPACE + UtilConstants.GET_METHOD_PREFIX
185 + JavaIdentifierSyntax.getCaptialCase(attrName) + UtilConstants.OPEN_PARENTHESIS
186 + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET
187 + UtilConstants.NEW_LINE + UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.RETURN
188 + UtilConstants.SPACE + attrName + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE
189 + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530190 }
191
192 /**
193 * Returns the setter method strings for class file.
194 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530195 * @param attrName name of the attribute
196 * @param attrType return type of attribute
197 * @param className name of the class
198 * @return setter method for class
Bharat saraswal870c56f2016-02-20 21:57:16 +0530199 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530200 private static String getSetterForClass(String attrName, String attrType, String className) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530201
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530202 return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
203 + className + UtilConstants.BUILDER + UtilConstants.SPACE + UtilConstants.SET_METHOD_PREFIX
204 + JavaIdentifierSyntax.getCaptialCase(attrName) + UtilConstants.OPEN_PARENTHESIS
205 + attrType + UtilConstants.SPACE + attrName + UtilConstants.CLOSE_PARENTHESIS
206 + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE
207 + UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.THIS + UtilConstants.PERIOD
208 + attrName + UtilConstants.SPACE + UtilConstants.EQUAL + UtilConstants.SPACE + attrName
209 + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE + UtilConstants.EIGHT_SPACE_INDENTATION
210 + UtilConstants.RETURN + UtilConstants.SPACE + UtilConstants.THIS + UtilConstants.SEMI_COLAN
211 + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530212 }
213
214 /**
215 * Returns the getter method strings for interface file.
216 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530217 * @param yangName name of the attribute
Bharat saraswal870c56f2016-02-20 21:57:16 +0530218 * @param returnType return type of attribute
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530219 * @return getter method for interface
Bharat saraswal870c56f2016-02-20 21:57:16 +0530220 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530221 private static String getGetterForInterface(String yangName, String returnType) {
222 return UtilConstants.FOUR_SPACE_INDENTATION + returnType
223 + UtilConstants.SPACE + UtilConstants.GET_METHOD_PREFIX
224 + JavaIdentifierSyntax.getCaptialCase(yangName)
225 + UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS
226 + UtilConstants.SEMI_COLAN;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530227 }
228
229 /**
230 * Returns the setter method strings for interface file.
231 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530232 * @param attrName name of the attribute
233 * @param attrType return type of attribute
234 * @param className name of the java class being generated
235 * @return setter method for interface
Bharat saraswal870c56f2016-02-20 21:57:16 +0530236 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530237 private static String getSetterForInterface(String attrName, String attrType, String className) {
238 return UtilConstants.FOUR_SPACE_INDENTATION + className + UtilConstants.BUILDER
239 + UtilConstants.SPACE + UtilConstants.SET_METHOD_PREFIX
240 + JavaIdentifierSyntax.getCaptialCase(attrName) + UtilConstants.OPEN_PARENTHESIS
241 + attrType + UtilConstants.SPACE + attrName + UtilConstants.CLOSE_PARENTHESIS
242 + UtilConstants.SEMI_COLAN;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530243 }
244
245 /**
246 * Returns the build method strings for interface file.
247 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530248 * @param yangName name of the interface
249 * @return build method for interface
Bharat saraswal870c56f2016-02-20 21:57:16 +0530250 */
251 private static String getBuildForInterface(String yangName) {
252
253 return UtilConstants.FOUR_SPACE_INDENTATION + yangName + UtilConstants.SPACE + UtilConstants.BUILD
254 + UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN;
255 }
256
257 /**
258 * Returns the constructor strings for class file.
259 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530260 * @param yangName name of the class
Bharat saraswal870c56f2016-02-20 21:57:16 +0530261 * @return constructor for class
262 */
263 private static String getConstructor(String yangName) {
264
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530265 String builderAttribute = yangName.substring(0, 1).toLowerCase() + yangName.substring(1);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530266 String constructor = UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
267 + yangName + UtilConstants.IMPL + UtilConstants.OPEN_PARENTHESIS + yangName + UtilConstants.BUILDER
268 + UtilConstants.SPACE + builderAttribute + UtilConstants.OBJECT + UtilConstants.CLOSE_PARENTHESIS
269 + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE;
270
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530271 // TODO: need to get the partial constructor from constructor temp file.
272 // if (getAttrInfo() != null) {
273 // for (AttributeInfo attribute : getAttrInfo()) {
274 // attribute.setAttributeName(JavaIdentifierSyntax.getCamelCase(attribute.getAttributeName()));
275 // constructor = constructor + UtilConstants.TWELVE_SPACE_INDENTATION + UtilConstants.THIS
276 // + UtilConstants.PERIOD + attribute.getAttributeName() + UtilConstants.SPACE
277 // + UtilConstants.EQUAL + UtilConstants.SPACE + builderAttribute + UtilConstants.OBJECT
278 // + UtilConstants.PERIOD + UtilConstants.GET_METHOD_PREFIX
279 // + JavaIdentifierSyntax.getCaptialCase(attribute.getAttributeName())
280 // + UtilConstants.OPEN_PARENTHESIS
281 // + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN
282 // + UtilConstants.NEW_LINE;
283 // }
284 // getAttrInfo().clear();
285 // }
286
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530287 return constructor + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530288 }
289
290 /**
291 * Returns the build method strings for class file.
292 *
293 * @param yangName class name
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530294 * @return build method string for class
Bharat saraswal870c56f2016-02-20 21:57:16 +0530295 */
296 private static String getBuild(String yangName) {
297
298 return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE + yangName
299 + UtilConstants.SPACE + UtilConstants.BUILD + UtilConstants.OPEN_PARENTHESIS
300 + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET
301 + UtilConstants.NEW_LINE + UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.RETURN
302 + UtilConstants.SPACE + UtilConstants.NEW + UtilConstants.SPACE + yangName + UtilConstants.IMPL
303 + UtilConstants.OPEN_PARENTHESIS + UtilConstants.THIS + UtilConstants.CLOSE_PARENTHESIS
304 + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
305 + UtilConstants.CLOSE_CURLY_BRACKET;
306 }
307
308 /**
309 * Returns the Default constructor strings for class file.
310 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530311 * @param name name of the class
Bharat saraswal870c56f2016-02-20 21:57:16 +0530312 * @return Default constructor for class
313 */
314 private static String getDefaultConstructor(String name) {
315
316 return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE + name
317 + UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE
318 + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
319 + UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE;
320 }
321
322}