blob: abccc4cd19023e10b133f6c2638d8759fb82b3d2 [file] [log] [blame]
Vinod Kumar S38046502016-03-23 15:30:27 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S38046502016-03-23 15:30:27 +05303 *
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 */
16package org.onosproject.yangutils.translator.tojava;
17
Bharat saraswale2d51d62016-03-23 19:40:35 +053018import java.util.ArrayList;
19import java.util.List;
Vinod Kumar S38046502016-03-23 15:30:27 +053020import java.util.SortedSet;
21import java.util.TreeSet;
Vinod Kumar S38046502016-03-23 15:30:27 +053022
Bharat saraswal33dfa012016-05-17 19:59:16 +053023import static org.onosproject.yangutils.utils.UtilConstants.ABSTRACT_EVENT;
Bharat saraswale707f032016-07-14 23:33:55 +053024import static org.onosproject.yangutils.utils.UtilConstants.BIG_INTEGER;
Bharat saraswalb551aae2016-07-14 15:18:20 +053025import static org.onosproject.yangutils.utils.UtilConstants.YANG_AUGMENTED_INFO_CLASS_IMPORT_CLASS;
26import static org.onosproject.yangutils.utils.UtilConstants.YANG_AUGMENTED_INFO_CLASS_IMPORT_PKG;
Bharat saraswale2d51d62016-03-23 19:40:35 +053027import static org.onosproject.yangutils.utils.UtilConstants.COLLECTION_IMPORTS;
28import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
Bharat saraswal33dfa012016-05-17 19:59:16 +053029import static org.onosproject.yangutils.utils.UtilConstants.EVENT_LISTENER;
Bharat saraswale2d51d62016-03-23 19:40:35 +053030import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_CLASS;
31import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_PKG;
Bharat saraswalb551aae2016-07-14 15:18:20 +053032import static org.onosproject.yangutils.utils.UtilConstants.HASH_MAP;
Bharat saraswale2d51d62016-03-23 19:40:35 +053033import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
34import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG;
Bharat saraswale707f032016-07-14 23:33:55 +053035import static org.onosproject.yangutils.utils.UtilConstants.JAVA_MATH;
Bharat saraswale2d51d62016-03-23 19:40:35 +053036import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_CLASS;
Shankara-Huaweib9999eb2016-07-14 16:53:09 +053037import static org.onosproject.yangutils.utils.UtilConstants.BITSET;
Bharat saraswale2d51d62016-03-23 19:40:35 +053038import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_PKG;
39import static org.onosproject.yangutils.utils.UtilConstants.LIST;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053040import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_REG;
41import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_SERVICE;
Bharat saraswalb551aae2016-07-14 15:18:20 +053042import static org.onosproject.yangutils.utils.UtilConstants.MAP;
Bharat saraswale2d51d62016-03-23 19:40:35 +053043import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswal33dfa012016-05-17 19:59:16 +053044import static org.onosproject.yangutils.utils.UtilConstants.ONOS_EVENT_PKG;
Bharat saraswale2d51d62016-03-23 19:40:35 +053045import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
46import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
Bharat saraswald50c6382016-07-14 21:57:13 +053047import static org.onosproject.yangutils.utils.UtilConstants.YANG_AUGMENTED_OP_PARAM_INFO_CLASS;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053048import static java.util.Collections.sort;
49
Vinod Kumar S38046502016-03-23 15:30:27 +053050/**
Bharat saraswald9822e92016-04-05 15:13:44 +053051 * Represents that generated Java file can contain imports.
Vinod Kumar S38046502016-03-23 15:30:27 +053052 */
53public class JavaImportData {
54
55 /**
56 * Flag to denote if any list in imported.
57 */
58 private boolean isListToImport;
59
60 /**
61 * Sorted set of import info, to be used to maintain the set of classes to
62 * be imported in the generated class.
63 */
64 private SortedSet<JavaQualifiedTypeInfo> importSet;
65
66 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053067 * Creates java import data object.
Vinod Kumar S38046502016-03-23 15:30:27 +053068 */
69 public JavaImportData() {
70 setImportSet(new TreeSet<JavaQualifiedTypeInfo>());
71 }
72
73 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053074 * Returns if the list needs to be imported.
Vinod Kumar S38046502016-03-23 15:30:27 +053075 *
Vidyashree Rama74453712016-04-18 12:29:39 +053076 * @return true if any of the attribute needs to be maintained as a list
Vinod Kumar S38046502016-03-23 15:30:27 +053077 */
78 public boolean getIfListImported() {
79 return isListToImport;
80 }
81
82 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053083 * Sets the status of importing list.
Vinod Kumar S38046502016-03-23 15:30:27 +053084 *
Vidyashree Rama74453712016-04-18 12:29:39 +053085 * @param isList status to mention list is bing imported
Vinod Kumar S38046502016-03-23 15:30:27 +053086 */
87 public void setIfListImported(boolean isList) {
88 isListToImport = isList;
89 }
90
91 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053092 * Returns the set containing the imported class/interface info.
Vinod Kumar S38046502016-03-23 15:30:27 +053093 *
94 * @return the set containing the imported class/interface info
95 */
96 public SortedSet<JavaQualifiedTypeInfo> getImportSet() {
97 return importSet;
98 }
99
100 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530101 * Assigns the set containing the imported class/interface info.
Vinod Kumar S38046502016-03-23 15:30:27 +0530102 *
103 * @param importSet the set containing the imported class/interface info
104 */
105 private void setImportSet(SortedSet<JavaQualifiedTypeInfo> importSet) {
106 this.importSet = importSet;
107 }
108
109 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530110 * Adds an imported class/interface info if it is not already part of the
Vinod Kumar S38046502016-03-23 15:30:27 +0530111 * collection.
112 *
113 * If already part of the collection, check if the packages are same, if so
114 * then return true, to denote it is already in the import collection, and
115 * it can be accessed without qualified access. If the packages do not
116 * match, then do not add to the import collection, and return false to
117 * denote, it is not added to import collection and needs to be accessed in
118 * a qualified manner.
119 *
Vinod Kumar S38046502016-03-23 15:30:27 +0530120 * @param newImportInfo class/interface info being imported
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530121 * @param className name of the call being generated
122 * @param classPkg generated class package
123 * @return qualified access status of the import node being added
Vinod Kumar S38046502016-03-23 15:30:27 +0530124 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530125 public boolean addImportInfo(JavaQualifiedTypeInfo newImportInfo,
126 String className, String classPkg) {
127
128 if (newImportInfo.getClassInfo().contentEquals(className)) {
129 /*
130 * if the current class name is same as the attribute class name,
131 * then the attribute must be accessed in a qualified manner.
132 */
133 return true;
134 } else if (newImportInfo.getPkgInfo() == null) {
135 /*
136 * If the package info is null, then it is not a candidate for import / qualified access
137 */
138 return false;
139 }
140
141 /*
142 * If the attribute type is having the package info, it is contender
143 * for import list and also need to check if it needs to be a
144 * qualified access.
145 */
146 if (newImportInfo.getPkgInfo().contentEquals(classPkg)) {
147 /**
148 * Package of the referred attribute and the generated class is same, so no need import
149 * or qualified access.
150 */
151 return false;
152 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530153
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530154 for (JavaQualifiedTypeInfo curImportInfo : getImportSet()) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530155 if (curImportInfo.getClassInfo()
156 .contentEquals(newImportInfo.getClassInfo())) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530157 return !curImportInfo.getPkgInfo()
Vinod Kumar S38046502016-03-23 15:30:27 +0530158 .contentEquals(newImportInfo.getPkgInfo());
159 }
160 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530161
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530162 /*
163 * import is added, so it is a member for non qualified access
164 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530165 getImportSet().add(newImportInfo);
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530166 return false;
Vinod Kumar S38046502016-03-23 15:30:27 +0530167 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530168
169 /**
170 * Returns import for class.
171 *
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530172 * @return imports for class
173 */
174 public List<String> getImports() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530175
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530176 String importString;
177 List<String> imports = new ArrayList<>();
178
179 for (JavaQualifiedTypeInfo importInfo : getImportSet()) {
180 if (!importInfo.getPkgInfo().equals(EMPTY_STRING) && importInfo.getClassInfo() != null
181 && !importInfo.getPkgInfo().equals(JAVA_LANG)) {
182 importString = IMPORT + importInfo.getPkgInfo() + PERIOD + importInfo.getClassInfo() + SEMI_COLAN
183 + NEW_LINE;
184
185 imports.add(importString);
186 }
187 }
188
Bharat saraswalc0e04842016-05-12 13:16:57 +0530189 if (getIfListImported()) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530190 imports.add(getImportForList());
191 }
192
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530193 sort(imports);
194 return imports;
195 }
196
197 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530198 * Returns import for hash and equals method.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530199 *
200 * @return import for hash and equals method
201 */
202 public String getImportForHashAndEquals() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530203 return IMPORT + JAVA_UTIL_OBJECTS_IMPORT_PKG + PERIOD + JAVA_UTIL_OBJECTS_IMPORT_CLASS;
204 }
205
206 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530207 * Returns import for to string method.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530208 *
209 * @return import for to string method
210 */
211 public String getImportForToString() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530212 return IMPORT + GOOGLE_MORE_OBJECT_IMPORT_PKG + PERIOD + GOOGLE_MORE_OBJECT_IMPORT_CLASS;
213 }
214
215 /**
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530216 * Returns import for to bitset method.
217 *
218 * @return import for to bitset method
219 */
220 public String getImportForToBitSet() {
221 return IMPORT + JAVA_UTIL_OBJECTS_IMPORT_PKG + PERIOD + BITSET + SEMI_COLAN + NEW_LINE;
222 }
223
224 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530225 * Returns import for list attribute.
226 *
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530227 * @return import for list attribute
Bharat saraswale2d51d62016-03-23 19:40:35 +0530228 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530229 public String getImportForList() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530230 return IMPORT + COLLECTION_IMPORTS + PERIOD + LIST + SEMI_COLAN + NEW_LINE;
231 }
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530232
233 /**
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530234 * Returns import string for ListenerService class.
235 *
236 * @return import string for ListenerService class
237 */
238 public String getListenerServiceImport() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530239 return IMPORT + ONOS_EVENT_PKG + PERIOD + LISTENER_SERVICE + SEMI_COLAN + NEW_LINE;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530240 }
241
242 /**
243 * Returns import string for ListenerRegistry class.
244 *
245 * @return import string for ListenerRegistry class
246 */
247 public String getListenerRegistryImport() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530248 return IMPORT + ONOS_EVENT_PKG + PERIOD + LISTENER_REG + SEMI_COLAN + NEW_LINE;
249 }
250
251 /**
252 * Returns import string for AbstractEvent class.
253 *
254 * @return import string for AbstractEvent class
255 */
256 public String getAbstractEventsImport() {
257 return IMPORT + ONOS_EVENT_PKG + PERIOD + ABSTRACT_EVENT + SEMI_COLAN + NEW_LINE;
258 }
259
260 /**
261 * Returns import string for EventListener class.
262 *
263 * @return import string for EventListener class
264 */
265 public String getEventListenerImport() {
266 return IMPORT + ONOS_EVENT_PKG + PERIOD + EVENT_LISTENER + SEMI_COLAN + NEW_LINE;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530267 }
Bharat saraswalb551aae2016-07-14 15:18:20 +0530268
269 /**
270 * Returns import string for map class.
271 *
272 * @return import string for map class
273 */
274 public String getMapImport() {
275 return IMPORT + COLLECTION_IMPORTS + PERIOD + MAP + SEMI_COLAN + NEW_LINE;
276 }
277
278 /**
279 * Returns import string for hash map class.
280 *
281 * @return import string for hash map class
282 */
283 public String getHashMapImport() {
284 return IMPORT + COLLECTION_IMPORTS + PERIOD + HASH_MAP + SEMI_COLAN + NEW_LINE;
285 }
286
287 /**
288 * Returns import string for hash map class.
289 *
290 * @return import string for hash map class
291 */
292 public String getYangAugmentedInfoImport() {
293 return IMPORT + YANG_AUGMENTED_INFO_CLASS_IMPORT_PKG + PERIOD + YANG_AUGMENTED_INFO_CLASS_IMPORT_CLASS;
294 }
Bharat saraswald50c6382016-07-14 21:57:13 +0530295
296 /**
297 * Returns import string for YangAugmentedOpParamInfo class.
298 *
299 * @return import string for YangAugmentedOpParamInfo class
300 */
301 public String getYangAugmentedOpParamInfoImport() {
302 return IMPORT + YANG_AUGMENTED_INFO_CLASS_IMPORT_PKG + PERIOD +
303 YANG_AUGMENTED_OP_PARAM_INFO_CLASS;
304 }
Bharat saraswale707f032016-07-14 23:33:55 +0530305
306 /**
307 * Returns import for big integer.
308 *
309 * @return import for big integer
310 */
311 public String getBigIntegerImport() {
312 return IMPORT + JAVA_MATH + PERIOD +
313 BIG_INTEGER + SEMI_COLAN + NEW_LINE;
314 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530315}