blob: 7c821754a8a3aa917a382987623204a7d5f67fd1 [file] [log] [blame]
Vinod Kumar S9f26ae52016-03-23 15:30:27 +05301/*
Brian O'Connor0f7908b2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S9f26ae52016-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 saraswal84366c52016-03-23 19:40:35 +053018import java.util.ArrayList;
19import java.util.List;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053020import java.util.SortedSet;
21import java.util.TreeSet;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053022
Bharat saraswal715d3fc2016-05-17 19:59:16 +053023import static org.onosproject.yangutils.utils.UtilConstants.ABSTRACT_EVENT;
Bharat saraswal64e7e232016-07-14 23:33:55 +053024import static org.onosproject.yangutils.utils.UtilConstants.BIG_INTEGER;
Shankara-Huaweib7564772016-08-02 18:13:13 +053025import static org.onosproject.yangutils.utils.UtilConstants.BITSET;
Bharat saraswal84366c52016-03-23 19:40:35 +053026import static org.onosproject.yangutils.utils.UtilConstants.COLLECTION_IMPORTS;
27import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
Bharat saraswal715d3fc2016-05-17 19:59:16 +053028import static org.onosproject.yangutils.utils.UtilConstants.EVENT_LISTENER;
Bharat saraswal84366c52016-03-23 19:40:35 +053029import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_CLASS;
30import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_PKG;
Bharat saraswalaf413b82016-07-14 15:18:20 +053031import static org.onosproject.yangutils.utils.UtilConstants.HASH_MAP;
Bharat saraswal84366c52016-03-23 19:40:35 +053032import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
33import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG;
Bharat saraswal64e7e232016-07-14 23:33:55 +053034import static org.onosproject.yangutils.utils.UtilConstants.JAVA_MATH;
Bharat saraswal84366c52016-03-23 19:40:35 +053035import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_CLASS;
36import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_PKG;
37import static org.onosproject.yangutils.utils.UtilConstants.LIST;
Bharat saraswal4aaab4d2016-05-17 14:19:38 +053038import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_REG;
39import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_SERVICE;
Bharat saraswalaf413b82016-07-14 15:18:20 +053040import static org.onosproject.yangutils.utils.UtilConstants.MAP;
Bharat saraswal84366c52016-03-23 19:40:35 +053041import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswal715d3fc2016-05-17 19:59:16 +053042import static org.onosproject.yangutils.utils.UtilConstants.ONOS_EVENT_PKG;
Bharat saraswal84366c52016-03-23 19:40:35 +053043import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
Vidyashree Ramab3670472016-08-06 15:49:56 +053044import static org.onosproject.yangutils.utils.UtilConstants.QUEUE;
Bharat saraswal84366c52016-03-23 19:40:35 +053045import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
Vidyashree Ramab3670472016-08-06 15:49:56 +053046import static org.onosproject.yangutils.utils.UtilConstants.SET;
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +053047import static java.util.Collections.sort;
48
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053049/**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053050 * Represents that generated Java file can contain imports.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053051 */
52public class JavaImportData {
53
54 /**
55 * Flag to denote if any list in imported.
56 */
57 private boolean isListToImport;
58
59 /**
Vidyashree Ramab3670472016-08-06 15:49:56 +053060 * Flag to denote if any queue is imported due to compiler annotation.
61 */
62 private boolean isQueueToImport;
63
64 /**
65 * Flag to denote if any set is imported due to compiler annotation.
66 */
67 private boolean isSetToImport;
68
69 /**
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053070 * Sorted set of import info, to be used to maintain the set of classes to
71 * be imported in the generated class.
72 */
Shankara-Huaweib7564772016-08-02 18:13:13 +053073 private SortedSet<JavaQualifiedTypeInfoTranslator> importSet;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053074
75 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053076 * Creates java import data object.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053077 */
78 public JavaImportData() {
Shankara-Huaweib7564772016-08-02 18:13:13 +053079 setImportSet(new TreeSet<>());
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053080 }
81
82 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053083 * Returns if the list needs to be imported.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053084 *
Vidyashree Rama02f115f2016-04-18 12:29:39 +053085 * @return true if any of the attribute needs to be maintained as a list
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053086 */
Shankara-Huaweib7564772016-08-02 18:13:13 +053087 private boolean getIfListImported() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053088 return isListToImport;
89 }
90
91 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053092 * Sets the status of importing list.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053093 *
Vidyashree Rama02f115f2016-04-18 12:29:39 +053094 * @param isList status to mention list is bing imported
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053095 */
Shankara-Huaweib7564772016-08-02 18:13:13 +053096 void setIfListImported(boolean isList) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053097 isListToImport = isList;
98 }
99
100 /**
Vidyashree Ramab3670472016-08-06 15:49:56 +0530101 * Is Queue to be imported due to compiler annotations.
102 *
103 * @return status of queue import
104 */
105 public boolean isQueueToImport() {
106 return isQueueToImport;
107 }
108
109 /**
110 * Is Set to be imported due to compiler annotations.
111 *
112 * @return status of set import
113 */
114 public boolean isSetToImport() {
115 return isSetToImport;
116 }
117
118 /**
119 * Sets the status of the queue to be imported due to compiler annotations.
120 *
121 * @param queueToImport status of queue to import
122 */
123 public void setQueueToImport(boolean queueToImport) {
124 isQueueToImport = queueToImport;
125 }
126
127 /**
128 * Sets the status of the set to be imported due to compiler annotations.
129 *
130 * @param setToImport status of set to import
131 */
132 public void setSetToImport(boolean setToImport) {
133 isSetToImport = setToImport;
134 }
135
136 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530137 * Returns the set containing the imported class/interface info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530138 *
139 * @return the set containing the imported class/interface info
140 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530141 public SortedSet<JavaQualifiedTypeInfoTranslator> getImportSet() {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530142 return importSet;
143 }
144
145 /**
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530146 * Assigns the set containing the imported class/interface info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530147 *
148 * @param importSet the set containing the imported class/interface info
149 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530150 private void setImportSet(SortedSet<JavaQualifiedTypeInfoTranslator> importSet) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530151 this.importSet = importSet;
152 }
153
154 /**
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530155 * Adds an imported class/interface info if it is not already part of the
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530156 * collection.
Shankara-Huaweib7564772016-08-02 18:13:13 +0530157 * <p>
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530158 * If already part of the collection, check if the packages are same, if so
159 * then return true, to denote it is already in the import collection, and
160 * it can be accessed without qualified access. If the packages do not
161 * match, then do not add to the import collection, and return false to
162 * denote, it is not added to import collection and needs to be accessed in
163 * a qualified manner.
164 *
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530165 * @param newImportInfo class/interface info being imported
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530166 * @param className name of the call being generated
167 * @param classPkg generated class package
168 * @return qualified access status of the import node being added
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530169 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530170 public boolean addImportInfo(JavaQualifiedTypeInfoTranslator newImportInfo,
171 String className, String classPkg) {
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530172
173 if (newImportInfo.getClassInfo().contentEquals(className)) {
174 /*
Vidyashree Ramab3670472016-08-06 15:49:56 +0530175 * If the current class name is same as the attribute class name,
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530176 * then the attribute must be accessed in a qualified manner.
177 */
178 return true;
179 } else if (newImportInfo.getPkgInfo() == null) {
180 /*
181 * If the package info is null, then it is not a candidate for import / qualified access
182 */
183 return false;
184 }
185
186 /*
187 * If the attribute type is having the package info, it is contender
188 * for import list and also need to check if it needs to be a
189 * qualified access.
190 */
191 if (newImportInfo.getPkgInfo().contentEquals(classPkg)) {
Shankara-Huaweib7564772016-08-02 18:13:13 +0530192 /*
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530193 * Package of the referred attribute and the generated class is same, so no need import
194 * or qualified access.
195 */
196 return false;
197 }
Bharat saraswal84366c52016-03-23 19:40:35 +0530198
Shankara-Huaweib7564772016-08-02 18:13:13 +0530199 for (JavaQualifiedTypeInfoTranslator curImportInfo : getImportSet()) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530200 if (curImportInfo.getClassInfo()
201 .contentEquals(newImportInfo.getClassInfo())) {
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530202 return !curImportInfo.getPkgInfo()
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530203 .contentEquals(newImportInfo.getPkgInfo());
204 }
205 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530206
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530207 /*
Vidyashree Ramab3670472016-08-06 15:49:56 +0530208 * Import is added, so it is a member for non qualified access
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530209 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530210 getImportSet().add(newImportInfo);
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530211 return false;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530212 }
Bharat saraswal84366c52016-03-23 19:40:35 +0530213
214 /**
215 * Returns import for class.
216 *
Vidyashree Rama13960652016-04-26 15:06:06 +0530217 * @return imports for class
218 */
219 public List<String> getImports() {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530220
Vidyashree Rama13960652016-04-26 15:06:06 +0530221 String importString;
222 List<String> imports = new ArrayList<>();
223
Shankara-Huaweib7564772016-08-02 18:13:13 +0530224 for (JavaQualifiedTypeInfoTranslator importInfo : getImportSet()) {
Vidyashree Rama13960652016-04-26 15:06:06 +0530225 if (!importInfo.getPkgInfo().equals(EMPTY_STRING) && importInfo.getClassInfo() != null
226 && !importInfo.getPkgInfo().equals(JAVA_LANG)) {
227 importString = IMPORT + importInfo.getPkgInfo() + PERIOD + importInfo.getClassInfo() + SEMI_COLAN
228 + NEW_LINE;
229
230 imports.add(importString);
231 }
232 }
233
Bharat saraswal250a7472016-05-12 13:16:57 +0530234 if (getIfListImported()) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530235 imports.add(getImportForList());
236 }
237
Vidyashree Ramab3670472016-08-06 15:49:56 +0530238 if (isQueueToImport()) {
239 imports.add(getImportForQueue());
240 }
241
242 if (isSetToImport()) {
243 imports.add(getImportForSet());
244 }
245
Vidyashree Rama13960652016-04-26 15:06:06 +0530246 sort(imports);
247 return imports;
248 }
249
250 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530251 * Returns import for hash and equals method.
Bharat saraswal84366c52016-03-23 19:40:35 +0530252 *
253 * @return import for hash and equals method
254 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530255 String getImportForHashAndEquals() {
Bharat saraswal84366c52016-03-23 19:40:35 +0530256 return IMPORT + JAVA_UTIL_OBJECTS_IMPORT_PKG + PERIOD + JAVA_UTIL_OBJECTS_IMPORT_CLASS;
257 }
258
259 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530260 * Returns import for to string method.
Bharat saraswal84366c52016-03-23 19:40:35 +0530261 *
262 * @return import for to string method
263 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530264 String getImportForToString() {
Bharat saraswal84366c52016-03-23 19:40:35 +0530265 return IMPORT + GOOGLE_MORE_OBJECT_IMPORT_PKG + PERIOD + GOOGLE_MORE_OBJECT_IMPORT_CLASS;
266 }
267
268 /**
Shankara-Huaweia1039e52016-07-14 16:53:09 +0530269 * Returns import for to bitset method.
270 *
271 * @return import for to bitset method
272 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530273 String getImportForToBitSet() {
Shankara-Huaweia1039e52016-07-14 16:53:09 +0530274 return IMPORT + JAVA_UTIL_OBJECTS_IMPORT_PKG + PERIOD + BITSET + SEMI_COLAN + NEW_LINE;
275 }
276
277 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530278 * Returns import for list attribute.
279 *
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530280 * @return import for list attribute
Bharat saraswal84366c52016-03-23 19:40:35 +0530281 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530282 String getImportForList() {
Bharat saraswal84366c52016-03-23 19:40:35 +0530283 return IMPORT + COLLECTION_IMPORTS + PERIOD + LIST + SEMI_COLAN + NEW_LINE;
284 }
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530285
286 /**
Vidyashree Ramab3670472016-08-06 15:49:56 +0530287 * Returns import for queue attribute.
288 *
289 * @return import for queue attribute
290 */
291 public String getImportForQueue() {
292 return IMPORT + COLLECTION_IMPORTS + PERIOD + QUEUE + SEMI_COLAN + NEW_LINE;
293 }
294
295 /**
296 * Returns import for set attribute.
297 *
298 * @return import for set attribute
299 */
300 public String getImportForSet() {
301 return IMPORT + COLLECTION_IMPORTS + PERIOD + SET + SEMI_COLAN + NEW_LINE;
302 }
303
304 /**
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530305 * Returns import string for ListenerService class.
306 *
307 * @return import string for ListenerService class
308 */
309 public String getListenerServiceImport() {
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530310 return IMPORT + ONOS_EVENT_PKG + PERIOD + LISTENER_SERVICE + SEMI_COLAN + NEW_LINE;
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530311 }
312
313 /**
314 * Returns import string for ListenerRegistry class.
315 *
316 * @return import string for ListenerRegistry class
317 */
318 public String getListenerRegistryImport() {
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530319 return IMPORT + ONOS_EVENT_PKG + PERIOD + LISTENER_REG + SEMI_COLAN + NEW_LINE;
320 }
321
322 /**
323 * Returns import string for AbstractEvent class.
324 *
325 * @return import string for AbstractEvent class
326 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530327 String getAbstractEventsImport() {
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530328 return IMPORT + ONOS_EVENT_PKG + PERIOD + ABSTRACT_EVENT + SEMI_COLAN + NEW_LINE;
329 }
330
331 /**
332 * Returns import string for EventListener class.
333 *
334 * @return import string for EventListener class
335 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530336 String getEventListenerImport() {
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530337 return IMPORT + ONOS_EVENT_PKG + PERIOD + EVENT_LISTENER + SEMI_COLAN + NEW_LINE;
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530338 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530339
340 /**
341 * Returns import string for map class.
342 *
343 * @return import string for map class
344 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530345 String getMapImport() {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530346 return IMPORT + COLLECTION_IMPORTS + PERIOD + MAP + SEMI_COLAN + NEW_LINE;
347 }
348
349 /**
350 * Returns import string for hash map class.
351 *
352 * @return import string for hash map class
353 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530354 String getHashMapImport() {
Bharat saraswalaf413b82016-07-14 15:18:20 +0530355 return IMPORT + COLLECTION_IMPORTS + PERIOD + HASH_MAP + SEMI_COLAN + NEW_LINE;
356 }
357
358 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530359 * Returns import for big integer.
360 *
361 * @return import for big integer
362 */
363 public String getBigIntegerImport() {
364 return IMPORT + JAVA_MATH + PERIOD +
365 BIG_INTEGER + SEMI_COLAN + NEW_LINE;
366 }
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530367}