blob: 6ca68291067645cdc1e3cf0f169a4146bbfb23f8 [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;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053022import org.onosproject.yangutils.utils.io.impl.YangIoUtils;
Bharat saraswal870c56f2016-02-20 21:57:16 +053023
24/**
25 * Generated methods for generated files based on the file type.
26 */
27public final class MethodsGenerator {
28
Bharat saraswal870c56f2016-02-20 21:57:16 +053029 /**
30 * Default constructor.
31 */
32 private MethodsGenerator() {
33 }
34
35 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +053036 * Returns the methods strings for builder interface.
37 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053038 * @param attr attribute info
39 * @param className name of the java class being generated
40 * @return method string for builder interface
Bharat saraswal870c56f2016-02-20 21:57:16 +053041 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +053042 static String parseBuilderInterfaceMethodString(AttributeInfo attr, String className) {
Bharat saraswal870c56f2016-02-20 21:57:16 +053043
Vinod Kumar Sc4216002016-03-03 19:55:30 +053044 return getGetterString(attr) + UtilConstants.NEW_LINE + getSetterString(attr, className);
Bharat saraswal870c56f2016-02-20 21:57:16 +053045 }
46
47 /**
48 * Returns the methods strings for builder interface.
49 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053050 * @param name attribute name
51 * @return method string for builder interface
Bharat saraswal870c56f2016-02-20 21:57:16 +053052 */
53 public static String parseBuilderInterfaceBuildMethodString(String name) {
54
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053055 return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.BUILD, name, false)
Vinod Kumar Sc4216002016-03-03 19:55:30 +053056 + getBuildForInterface(name);
Bharat saraswal870c56f2016-02-20 21:57:16 +053057 }
58
59 /**
60 * Returns getter string.
61 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053062 * @param attr attribute info
Bharat saraswal870c56f2016-02-20 21:57:16 +053063 * @return getter string
64 */
65 public static String getGetterString(AttributeInfo attr) {
66
Vinod Kumar Sc4216002016-03-03 19:55:30 +053067 String returnType = "";
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053068 boolean isList = attr.isListAttr();
69 if (attr.isQualifiedName() && (attr.getImportInfo().getPkgInfo() != null)) {
Vinod Kumar Sc4216002016-03-03 19:55:30 +053070 returnType = attr.getImportInfo().getPkgInfo() + ".";
71 }
72
73 returnType = returnType + attr.getImportInfo().getClassInfo();
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053074 String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
Vinod Kumar Sc4216002016-03-03 19:55:30 +053075
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053076 return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.GETTER, attributeName, isList) +
77
78 getGetterForInterface(attributeName, returnType, attr.isListAttr())
Bharat saraswal870c56f2016-02-20 21:57:16 +053079 + UtilConstants.NEW_LINE;
80 }
81
82 /**
83 * Returns setter string.
84 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053085 * @param attr attribute info
86 * @param className java class name
Bharat saraswal870c56f2016-02-20 21:57:16 +053087 * @return setter string
88 */
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053089 public static String getSetterString(AttributeInfo attr, String className) {
Vinod Kumar Sc4216002016-03-03 19:55:30 +053090
91 String attrType = "";
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053092 boolean isList = attr.isListAttr();
93 if (attr.isQualifiedName() && (attr.getImportInfo().getPkgInfo() != null)) {
Vinod Kumar Sc4216002016-03-03 19:55:30 +053094 attrType = attr.getImportInfo().getPkgInfo() + ".";
95 }
96
97 attrType = attrType + attr.getImportInfo().getClassInfo();
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053098 String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
Bharat saraswal870c56f2016-02-20 21:57:16 +053099
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530100 return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.SETTER, attributeName, isList)
101 + getSetterForInterface(attributeName, attrType, className, attr.isListAttr())
102 + UtilConstants.NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530103 }
104
105 /**
106 * Returns constructor method string.
107 *
108 * @param name class name
109 * @return constructor string
110 */
111 public static String getConstructorString(String name) {
112
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530113 return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.CONSTRUCTOR, name, false);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530114 }
115
116 /**
117 * Returns default constructor method string.
118 *
Bharat saraswal870c56f2016-02-20 21:57:16 +0530119 * @param name class name
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530120 * @param modifierType modifier type
Bharat saraswal870c56f2016-02-20 21:57:16 +0530121 * @return default constructor string
122 */
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530123 public static String getDefaultConstructorString(String name, String modifierType) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530124
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530125 return JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.DEFAULT_CONSTRUCTOR, name, false)
126 + getDefaultConstructor(name, modifierType);
127 }
128
129 /**
130 * Returns default constructor method string.
131 *
132 * @param attr attribute info
133 * @param className class name
134 * @return default constructor string
135 */
136 public static String getTypeDefConstructor(AttributeInfo attr, String className) {
137
138 String attrQuaifiedType = "";
139 if (attr.isQualifiedName() && (attr.getImportInfo().getPkgInfo() != null)) {
140 attrQuaifiedType = attr.getImportInfo().getPkgInfo() + ".";
141 }
142 attrQuaifiedType = attrQuaifiedType + attr.getImportInfo().getClassInfo();
143 String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
144
145 if (!attr.isListAttr()) {
146 return getTypeDefConstructorString(attrQuaifiedType, attributeName, className);
147 }
148 String listAttr = getListString() + attrQuaifiedType + UtilConstants.DIAMOND_CLOSE_BRACKET;
149 return getTypeDefConstructorString(listAttr, attributeName, className);
150 }
151
152 /**
153 * Returns type def's constructor for attribute.
154 *
155 * @param type data type
156 * @param name attribute name
157 * @param className class name
158 * @return setter for type def's attribute
159 */
160 private static String getTypeDefConstructorString(String type, String name, String className) {
161
162 return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
163 + className + UtilConstants.OPEN_PARENTHESIS
164 + type + UtilConstants.SPACE + "value" + UtilConstants.CLOSE_PARENTHESIS
165 + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE
166 + UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.THIS
167 + UtilConstants.PERIOD + name + UtilConstants.SPACE + UtilConstants.EQUAL + UtilConstants.SPACE
168 + "value" + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
169 + UtilConstants.CLOSE_CURLY_BRACKET;
170 }
171
172 /**
173 * Returns check not null string.
174 *
175 * @param name attribute name
176 * @return check not null string
177 */
178 public static String getCheckNotNull(String name) {
179 return UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.CHECK_NOT_NULL_STRING
180 + UtilConstants.OPEN_PARENTHESIS + name + UtilConstants.COMMA + UtilConstants.SPACE + name
181 + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530182 }
183
184 /**
185 * Returns build method string.
186 *
187 * @param name class name
188 * @return build string
189 */
190 public static String getBuildString(String name) {
191
192 return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.OVERRIDE + UtilConstants.NEW_LINE
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530193 + getBuild(name);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530194 }
195
196 /**
197 * Returns the getter method strings for class file.
198 *
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530199 * @param attr attribute info
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530200 * @return getter method for class
Bharat saraswal870c56f2016-02-20 21:57:16 +0530201 */
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530202 public static String getGetterForClass(AttributeInfo attr) {
203
204 String attrQuaifiedType = "";
205 if (attr.isQualifiedName() && (attr.getImportInfo().getPkgInfo() != null)) {
206 attrQuaifiedType = attr.getImportInfo().getPkgInfo() + ".";
207 }
208 attrQuaifiedType = attrQuaifiedType + attr.getImportInfo().getClassInfo();
209 String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
210
211 if (!attr.isListAttr()) {
212 return getGetter(attrQuaifiedType, attributeName);
213 }
214 String listAttr = getListString() + attrQuaifiedType + UtilConstants.DIAMOND_CLOSE_BRACKET;
215 return getGetter(listAttr, attributeName);
216 }
217
218 /**
219 * Returns getter for attribute.
220 *
221 * @param type return type
222 * @param name attribute name
223 * @return getter for attribute
224 */
225 private static String getGetter(String type, String name) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530226
227 return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530228 + type + UtilConstants.SPACE + UtilConstants.GET_METHOD_PREFIX
229 + JavaIdentifierSyntax.getCaptialCase(name) + UtilConstants.OPEN_PARENTHESIS
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530230 + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET
231 + UtilConstants.NEW_LINE + UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.RETURN
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530232 + UtilConstants.SPACE + name + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530233 + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530234 }
235
236 /**
237 * Returns the setter method strings for class file.
238 *
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530239 * @param attr attribute info
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530240 * @param className name of the class
241 * @return setter method for class
Bharat saraswal870c56f2016-02-20 21:57:16 +0530242 */
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530243 public static String getSetterForClass(AttributeInfo attr, String className) {
244
245 String attrQuaifiedType = "";
246 if (attr.isQualifiedName() && (attr.getImportInfo().getPkgInfo() != null)) {
247 attrQuaifiedType = attr.getImportInfo().getPkgInfo() + ".";
248 }
249 attrQuaifiedType = attrQuaifiedType + attr.getImportInfo().getClassInfo();
250 String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
251 if (!attr.isListAttr()) {
252 return getSetter(className, attributeName, attrQuaifiedType);
253 }
254 String listAttr = getListString() + attrQuaifiedType + UtilConstants.DIAMOND_CLOSE_BRACKET;
255 return getSetter(className, attributeName, listAttr);
256 }
257
258 /**
259 * Returns setter for attribute.
260 *
261 * @param className class name
262 * @param name attribute name
263 * @param type return type
264 * @return setter for attribute
265 */
266 private static String getSetter(String className, String name, String type) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530267
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530268 return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
269 + className + UtilConstants.BUILDER + UtilConstants.SPACE + UtilConstants.SET_METHOD_PREFIX
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530270 + JavaIdentifierSyntax.getCaptialCase(name) + UtilConstants.OPEN_PARENTHESIS
271 + type + UtilConstants.SPACE + name + UtilConstants.CLOSE_PARENTHESIS
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530272 + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE
273 + UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.THIS + UtilConstants.PERIOD
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530274 + name + UtilConstants.SPACE + UtilConstants.EQUAL + UtilConstants.SPACE
275 + name + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE + UtilConstants.EIGHT_SPACE_INDENTATION
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530276 + UtilConstants.RETURN + UtilConstants.SPACE + UtilConstants.THIS + UtilConstants.SEMI_COLAN
277 + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530278 }
279
280 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530281 * Returns the setter method strings for class file.
282 *
283 * @param attr attribute info
284 * @return setter method for class
285 */
286 public static String getSetterForTypeDefClass(AttributeInfo attr) {
287
288 String attrQuaifiedType = "";
289 if (attr.isQualifiedName() && (attr.getImportInfo().getPkgInfo() != null)) {
290 attrQuaifiedType = attr.getImportInfo().getPkgInfo() + ".";
291 }
292 attrQuaifiedType = attrQuaifiedType + attr.getImportInfo().getClassInfo();
293 String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
294
295 if (!attr.isListAttr()) {
296 return getTypeDefSetter(attrQuaifiedType, attributeName);
297 }
298 String listAttr = getListString() + attrQuaifiedType + UtilConstants.DIAMOND_CLOSE_BRACKET;
299 return getTypeDefSetter(listAttr, attributeName);
300 }
301
302 /**
303 * Returns type def's setter for attribute.
304 *
305 * @param type data type
306 * @param name attribute name
307 * @return setter for type def's attribute
308 */
309 private static String getTypeDefSetter(String type, String name) {
310
311 return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
312 + UtilConstants.VOID + UtilConstants.SPACE + UtilConstants.SET_METHOD_PREFIX
313 + JavaIdentifierSyntax.getCaptialCase(name) + UtilConstants.OPEN_PARENTHESIS
314 + type + UtilConstants.SPACE + "value" + UtilConstants.CLOSE_PARENTHESIS
315 + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE
316 + UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.THIS + UtilConstants.PERIOD
317 + name + UtilConstants.SPACE + UtilConstants.EQUAL + UtilConstants.SPACE
318 + "value" + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
319 + UtilConstants.CLOSE_CURLY_BRACKET;
320 }
321
322 /**
323 * Returns override string.
324 *
325 * @return override string
326 */
327 public static String getOverRideString() {
328 return UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
329 + UtilConstants.OVERRIDE + UtilConstants.NEW_LINE;
330 }
331
332 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +0530333 * Returns the getter method strings for interface file.
334 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530335 * @param yangName name of the attribute
Bharat saraswal870c56f2016-02-20 21:57:16 +0530336 * @param returnType return type of attribute
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530337 * @param isList is list attribute
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530338 * @return getter method for interface
Bharat saraswal870c56f2016-02-20 21:57:16 +0530339 */
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530340 public static String getGetterForInterface(String yangName, String returnType, boolean isList) {
341
342 if (!isList) {
343 return getGetterInterfaceString(returnType, yangName);
344 }
345 String listAttr = getListString() + returnType + UtilConstants.DIAMOND_CLOSE_BRACKET;
346 return getGetterInterfaceString(listAttr, yangName);
347 }
348
349 /**
350 * Returns getter for attribute in interface.
351 *
352 * @param returnType return type
353 * @param yangName attribute name
354 * @return getter for interface
355 */
356 private static String getGetterInterfaceString(String returnType, String yangName) {
357
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530358 return UtilConstants.FOUR_SPACE_INDENTATION + returnType
359 + UtilConstants.SPACE + UtilConstants.GET_METHOD_PREFIX
360 + JavaIdentifierSyntax.getCaptialCase(yangName)
361 + UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS
362 + UtilConstants.SEMI_COLAN;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530363
Bharat saraswal870c56f2016-02-20 21:57:16 +0530364 }
365
366 /**
367 * Returns the setter method strings for interface file.
368 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530369 * @param attrName name of the attribute
370 * @param attrType return type of attribute
371 * @param className name of the java class being generated
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530372 * @param isList is list attribute
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530373 * @return setter method for interface
Bharat saraswal870c56f2016-02-20 21:57:16 +0530374 */
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530375 public static String getSetterForInterface(String attrName, String attrType, String className, boolean isList) {
376
377 if (!isList) {
378 return getSetterInterfaceString(className, attrName, attrType);
379 }
380 String listAttr = getListString() + attrType + UtilConstants.DIAMOND_CLOSE_BRACKET;
381 return getSetterInterfaceString(className, attrName, listAttr);
382 }
383
384 /**
385 * Returns setter string for interface.
386 *
387 * @param className class name
388 * @param attrName attribute name
389 * @param attrType attribute type
390 * @return setter string
391 */
392 private static String getSetterInterfaceString(String className, String attrName, String attrType) {
393
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530394 return UtilConstants.FOUR_SPACE_INDENTATION + className + UtilConstants.BUILDER
395 + UtilConstants.SPACE + UtilConstants.SET_METHOD_PREFIX
396 + JavaIdentifierSyntax.getCaptialCase(attrName) + UtilConstants.OPEN_PARENTHESIS
397 + attrType + UtilConstants.SPACE + attrName + UtilConstants.CLOSE_PARENTHESIS
398 + UtilConstants.SEMI_COLAN;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530399 }
400
401 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530402 * Returns list string.
403 *
404 * @return list string
405 */
406 private static String getListString() {
407 return UtilConstants.LIST + UtilConstants.DIAMOND_OPEN_BRACKET;
408 }
409
410 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +0530411 * Returns the build method strings for interface file.
412 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530413 * @param yangName name of the interface
414 * @return build method for interface
Bharat saraswal870c56f2016-02-20 21:57:16 +0530415 */
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530416 public static String getBuildForInterface(String yangName) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530417
418 return UtilConstants.FOUR_SPACE_INDENTATION + yangName + UtilConstants.SPACE + UtilConstants.BUILD
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530419 + UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN
420 + UtilConstants.NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530421 }
422
423 /**
424 * Returns the constructor strings for class file.
425 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530426 * @param yangName name of the class
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530427 * @param attr attribute info
Bharat saraswal870c56f2016-02-20 21:57:16 +0530428 * @return constructor for class
429 */
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530430 public static String getConstructor(String yangName, AttributeInfo attr) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530431
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530432 String builderAttribute = JavaIdentifierSyntax.getLowerCase(yangName);
433 String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
434 String constructor = UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.THIS
435 + UtilConstants.PERIOD + JavaIdentifierSyntax.getCamelCase(attributeName)
436 + UtilConstants.SPACE + UtilConstants.EQUAL + UtilConstants.SPACE + builderAttribute
437 + UtilConstants.OBJECT + UtilConstants.PERIOD + UtilConstants.GET_METHOD_PREFIX
438 + JavaIdentifierSyntax.getCaptialCase(JavaIdentifierSyntax.getCamelCase(attributeName))
439 + UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN
440 + UtilConstants.NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530441
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530442 return constructor;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530443 }
444
445 /**
446 * Returns the build method strings for class file.
447 *
448 * @param yangName class name
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530449 * @return build method string for class
Bharat saraswal870c56f2016-02-20 21:57:16 +0530450 */
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530451 public static String getBuild(String yangName) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530452
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530453 return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
454 + yangName + UtilConstants.SPACE + UtilConstants.BUILD + UtilConstants.OPEN_PARENTHESIS
Bharat saraswal870c56f2016-02-20 21:57:16 +0530455 + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET
456 + UtilConstants.NEW_LINE + UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.RETURN
457 + UtilConstants.SPACE + UtilConstants.NEW + UtilConstants.SPACE + yangName + UtilConstants.IMPL
458 + UtilConstants.OPEN_PARENTHESIS + UtilConstants.THIS + UtilConstants.CLOSE_PARENTHESIS
459 + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
460 + UtilConstants.CLOSE_CURLY_BRACKET;
461 }
462
463 /**
464 * Returns the Default constructor strings for class file.
465 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530466 * @param name name of the class
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530467 * @param modifierType modifier type for default constructor
Bharat saraswal870c56f2016-02-20 21:57:16 +0530468 * @return Default constructor for class
469 */
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530470 private static String getDefaultConstructor(String name, String modifierType) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530471
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530472 return UtilConstants.FOUR_SPACE_INDENTATION + modifierType + UtilConstants.SPACE + name
Bharat saraswal870c56f2016-02-20 21:57:16 +0530473 + UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE
474 + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
475 + UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE;
476 }
477
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530478 /**
479 * Returns to string method open strings.
480 *
481 * @return to string method open string
482 */
483 public static String getToStringMethodOpen() {
484
485 return getOverRideString() + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
486 + UtilConstants.STRING + UtilConstants.SPACE + "to" + UtilConstants.STRING
487 + UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE
488 + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE + UtilConstants.EIGHT_SPACE_INDENTATION
489 + UtilConstants.RETURN + " MoreObjects.toStringHelper(getClass())" + UtilConstants.NEW_LINE;
490 }
491
492 /**
493 * Returns to string methods close string.
494 *
495 * @return to string method close string
496 */
497 public static String getToStringMethodClose() {
498 return UtilConstants.TWELVE_SPACE_INDENTATION + ".to" + UtilConstants.STRING
499 + UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN
500 + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
501 + UtilConstants.CLOSE_CURLY_BRACKET;
502 }
503
504 /**
505 * To string method for class.
506 *
507 * @param attr attribute info
508 * @return to string method
509 */
510 public static String getToStringMethod(AttributeInfo attr) {
511 String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
512 return UtilConstants.TWELVE_SPACE_INDENTATION + UtilConstants.PERIOD + UtilConstants.ADD_STRING
513 + UtilConstants.OPEN_PARENTHESIS + UtilConstants.QUOTES
514 + attributeName + UtilConstants.QUOTES + UtilConstants.COMMA + UtilConstants.SPACE + attributeName
515 + UtilConstants.CLOSE_PARENTHESIS;
516
517 }
518
519 /**
520 * Returns to hash code method open strings.
521 *
522 * @return to hash code method open string
523 */
524 public static String getHashCodeMethodOpen() {
525
526 return getOverRideString() + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
527 + UtilConstants.INT + UtilConstants.SPACE + UtilConstants.HASH_CODE_STRING
528 + UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE
529 + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE + UtilConstants.EIGHT_SPACE_INDENTATION
530 + UtilConstants.RETURN + " Objects.hash" + UtilConstants.OPEN_PARENTHESIS;
531 }
532
533 /**
534 * Returns to hash code methods close string.
535 *
536 * @param hashcodeString hash code string
537 * @return to hash code method close string
538 */
539 public static String getHashCodeMethodClose(String hashcodeString) {
540 hashcodeString = YangIoUtils.trimAtLast(hashcodeString, UtilConstants.COMMA);
541 hashcodeString = YangIoUtils.trimAtLast(hashcodeString, UtilConstants.SPACE);
542 hashcodeString = YangIoUtils.partString(hashcodeString);
543 return hashcodeString + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE
544 + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET;
545 }
546
547 /**
548 * Hash code method for class.
549 *
550 * @param attr attribute info
551 * @return hash code method
552 */
553 public static String getHashCodeMethod(AttributeInfo attr) {
554 String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
555 return attributeName
556 + UtilConstants.COMMA + UtilConstants.SPACE;
557
558 }
559
560 /**
561 * Returns to equals method open strings.
562 *
563 * @param className class name
564 * @return to equals method open string
565 */
566 public static String getEqualsMethodOpen(String className) {
567
568 return getOverRideString() + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
569 + UtilConstants.BOOLEAN + UtilConstants.SPACE + UtilConstants.EQUALS_STRING
570 + UtilConstants.OPEN_PARENTHESIS + UtilConstants.OBJECT_STRING + UtilConstants.SPACE + "obj"
571 + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET
572 + UtilConstants.NEW_LINE + getEqualsMethodsCommonIfCondition()
573 + getEqualsMethodsSpecificIfCondition(className);
574 }
575
576 /**
577 * Returns equal methods if condition string.
578 *
579 * @return if condition string
580 */
581 private static String getEqualsMethodsCommonIfCondition() {
582 return UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.IF + UtilConstants.SPACE
583 + UtilConstants.OPEN_PARENTHESIS + UtilConstants.THIS
584 + UtilConstants.SPACE + UtilConstants.EQUAL + UtilConstants.EQUAL + UtilConstants.SPACE + "obj"
585 + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET
586 + UtilConstants.NEW_LINE + UtilConstants.TWELVE_SPACE_INDENTATION + UtilConstants.RETURN
587 + UtilConstants.SPACE + UtilConstants.TRUE + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE
588 + UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE;
589 }
590
591 /**
592 * Returns if condition for specific class object in equals method.
593 *
594 * @param className class name
595 * @return if condition string
596 */
597 private static String getEqualsMethodsSpecificIfCondition(String className) {
598 return UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.IF + UtilConstants.SPACE
599 + UtilConstants.OPEN_PARENTHESIS + "obj" + UtilConstants.INSTANCE_OF + className
600 + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET
601 + UtilConstants.NEW_LINE + UtilConstants.TWELVE_SPACE_INDENTATION + className + UtilConstants.SPACE
602 + "other " + UtilConstants.EQUAL + UtilConstants.SPACE + UtilConstants.OPEN_PARENTHESIS + className
603 + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE + "obj" + UtilConstants.SEMI_COLAN
604 + UtilConstants.NEW_LINE + UtilConstants.TWELVE_SPACE_INDENTATION + UtilConstants.RETURN
605 + UtilConstants.NEW_LINE;
606 }
607
608 /**
609 * Returns to equals methods close string.
610 *
611 * @param equalMethodString equal method string
612 * @return to equals method close string
613 */
614 public static String getEqualsMethodClose(String equalMethodString) {
615 equalMethodString = YangIoUtils.trimAtLast(equalMethodString, UtilConstants.AND);
616 equalMethodString = YangIoUtils.trimAtLast(equalMethodString, UtilConstants.AND);
617 equalMethodString = YangIoUtils.trimAtLast(equalMethodString, UtilConstants.SPACE);
618 equalMethodString = YangIoUtils.trimAtLast(equalMethodString, UtilConstants.NEW_LINE) + UtilConstants.SEMI_COLAN
619 + UtilConstants.NEW_LINE;
620 return equalMethodString + UtilConstants.EIGHT_SPACE_INDENTATION
621 + UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE
622 + UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.RETURN + UtilConstants.SPACE
623 + UtilConstants.FALSE + UtilConstants.SEMI_COLAN
624 + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION
625 + UtilConstants.CLOSE_CURLY_BRACKET;
626 }
627
628 /**
629 * Equals method for class.
630 *
631 * @param attr attribute info
632 * @return equals method
633 */
634 public static String getEqualsMethod(AttributeInfo attr) {
635 String attributeName = JavaIdentifierSyntax.getLowerCase(attr.getAttributeName());
636 return UtilConstants.SIXTEEN_SPACE_INDENTATION + UtilConstants.SPACE + UtilConstants.OBJECT_STRING + "s"
637 + UtilConstants.PERIOD + UtilConstants.EQUALS_STRING + UtilConstants.OPEN_PARENTHESIS + attributeName
638 + UtilConstants.COMMA + UtilConstants.SPACE + "other." + attributeName + UtilConstants.CLOSE_PARENTHESIS
639 + UtilConstants.SPACE + UtilConstants.AND + UtilConstants.AND;
640
641 }
642
643 /**
644 * Returns of method string for class.
645 *
646 * @param name class name
647 * @param attr attribute info
648 * @return of method string
649 */
650 public static String getOfMethod(String name, AttributeInfo attr) {
651
652 String attrQuaifiedType = "";
653 if (attr.isQualifiedName() && (attr.getImportInfo().getPkgInfo() != null)) {
654 attrQuaifiedType = attr.getImportInfo().getPkgInfo() + ".";
655 }
656 attrQuaifiedType = attrQuaifiedType + attr.getImportInfo().getClassInfo();
657
658 return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE + UtilConstants.STATIC
659 + UtilConstants.SPACE + name + UtilConstants.SPACE + UtilConstants.OF + UtilConstants.OPEN_PARENTHESIS
660 + attrQuaifiedType + UtilConstants.SPACE + UtilConstants.VALUE + UtilConstants.CLOSE_PARENTHESIS
661 + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE
662 + UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.RETURN + UtilConstants.SPACE + UtilConstants.NEW
663 + UtilConstants.SPACE + name + UtilConstants.OPEN_PARENTHESIS + UtilConstants.VALUE
664 + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE
665 + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET + UtilConstants.NEW_LINE;
666 }
667
Bharat saraswal870c56f2016-02-20 21:57:16 +0530668}