blob: 2477d65d2afc0153d821128aaca6be1dee127cf2 [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 saraswal9fab16b2016-09-23 23:27:24 +053023import static java.util.Collections.sort;
24import static java.util.Collections.unmodifiableSortedSet;
25import static org.onosproject.yangutils.translator.tojava.utils.StringGenerator.getImportString;
Bharat saraswal715d3fc2016-05-17 19:59:16 +053026import static org.onosproject.yangutils.utils.UtilConstants.ABSTRACT_EVENT;
Bharat saraswal51c5d672016-11-13 09:03:48 +053027import static org.onosproject.yangutils.utils.UtilConstants.ARRAY_LIST_IMPORT;
Bharat saraswal64e7e232016-07-14 23:33:55 +053028import static org.onosproject.yangutils.utils.UtilConstants.BIG_INTEGER;
Shankara-Huaweib7564772016-08-02 18:13:13 +053029import static org.onosproject.yangutils.utils.UtilConstants.BITSET;
Bharat saraswal84366c52016-03-23 19:40:35 +053030import static org.onosproject.yangutils.utils.UtilConstants.COLLECTION_IMPORTS;
31import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
Bharat saraswal715d3fc2016-05-17 19:59:16 +053032import static org.onosproject.yangutils.utils.UtilConstants.EVENT_LISTENER;
Bharat saraswal84366c52016-03-23 19:40:35 +053033import 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;
Bharat saraswal54e4bab2016-10-05 23:32:14 +053036import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_PKG;
37import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_REGEX_PKG;
Bharat saraswal84366c52016-03-23 19:40:35 +053038import static org.onosproject.yangutils.utils.UtilConstants.LIST;
Bharat saraswal4aaab4d2016-05-17 14:19:38 +053039import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_SERVICE;
Bharat saraswalaf413b82016-07-14 15:18:20 +053040import static org.onosproject.yangutils.utils.UtilConstants.MAP;
Bharat saraswal715d3fc2016-05-17 19:59:16 +053041import static org.onosproject.yangutils.utils.UtilConstants.ONOS_EVENT_PKG;
Bharat saraswal54e4bab2016-10-05 23:32:14 +053042import static org.onosproject.yangutils.utils.UtilConstants.PATTERN;
Vidyashree Ramab3670472016-08-06 15:49:56 +053043import static org.onosproject.yangutils.utils.UtilConstants.QUEUE;
Vidyashree Ramab3670472016-08-06 15:49:56 +053044import static org.onosproject.yangutils.utils.UtilConstants.SET;
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +053045
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053046/**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053047 * Represents that generated Java file can contain imports.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053048 */
49public class JavaImportData {
50
51 /**
52 * Flag to denote if any list in imported.
53 */
54 private boolean isListToImport;
55
56 /**
Vidyashree Ramab3670472016-08-06 15:49:56 +053057 * Flag to denote if any queue is imported due to compiler annotation.
58 */
59 private boolean isQueueToImport;
60
61 /**
62 * Flag to denote if any set is imported due to compiler annotation.
63 */
64 private boolean isSetToImport;
65
66 /**
Bharat saraswala5c28512016-11-10 21:09:23 +053067 * Flag to denote if any map is imported due to compiler annotation.
68 */
69 private boolean isMapToImport;
70
71 /**
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053072 * Sorted set of import info, to be used to maintain the set of classes to
73 * be imported in the generated class.
74 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +053075 private final SortedSet<JavaQualifiedTypeInfoTranslator> importSet;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053076
77 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053078 * Creates java import data object.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053079 */
80 public JavaImportData() {
Bharat saraswal9fab16b2016-09-23 23:27:24 +053081 importSet = new TreeSet<>();
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053082 }
83
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053084
85 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053086 * Sets the status of importing list.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053087 *
Vidyashree Rama02f115f2016-04-18 12:29:39 +053088 * @param isList status to mention list is bing imported
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053089 */
Shankara-Huaweib7564772016-08-02 18:13:13 +053090 void setIfListImported(boolean isList) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053091 isListToImport = isList;
92 }
93
Vidyashree Ramab3670472016-08-06 15:49:56 +053094
95 /**
96 * Sets the status of the queue to be imported due to compiler annotations.
97 *
98 * @param queueToImport status of queue to import
99 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530100 void setQueueToImport(boolean queueToImport) {
Vidyashree Ramab3670472016-08-06 15:49:56 +0530101 isQueueToImport = queueToImport;
102 }
103
104 /**
105 * Sets the status of the set to be imported due to compiler annotations.
106 *
107 * @param setToImport status of set to import
108 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530109 void setSetToImport(boolean setToImport) {
Vidyashree Ramab3670472016-08-06 15:49:56 +0530110 isSetToImport = setToImport;
111 }
112
113 /**
Bharat saraswala5c28512016-11-10 21:09:23 +0530114 * Sets true if map is imported due to compiler annotations.
115 *
116 * @param mapToImport true if map is imported due to compiler annotations
117 */
118 void setMapToImport(boolean mapToImport) {
119 isMapToImport = mapToImport;
120 }
121
122 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530123 * Returns the set containing the imported class/interface info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530124 *
125 * @return the set containing the imported class/interface info
126 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530127 public SortedSet<JavaQualifiedTypeInfoTranslator> getImportSet() {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530128 return unmodifiableSortedSet(importSet);
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530129 }
130
131 /**
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530132 * Adds an imported class/interface info if it is not already part of the
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530133 * collection.
Shankara-Huaweib7564772016-08-02 18:13:13 +0530134 * <p>
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530135 * If already part of the collection, check if the packages are same, if so
136 * then return true, to denote it is already in the import collection, and
137 * it can be accessed without qualified access. If the packages do not
138 * match, then do not add to the import collection, and return false to
139 * denote, it is not added to import collection and needs to be accessed in
140 * a qualified manner.
141 *
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530142 * @param newImportInfo class/interface info being imported
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530143 * @param className name of the call being generated
144 * @param classPkg generated class package
145 * @return qualified access status of the import node being added
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530146 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530147 public boolean addImportInfo(JavaQualifiedTypeInfoTranslator newImportInfo,
148 String className, String classPkg) {
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530149
150 if (newImportInfo.getClassInfo().contentEquals(className)) {
151 /*
Vidyashree Ramab3670472016-08-06 15:49:56 +0530152 * If the current class name is same as the attribute class name,
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530153 * then the attribute must be accessed in a qualified manner.
154 */
155 return true;
156 } else if (newImportInfo.getPkgInfo() == null) {
157 /*
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530158 * If the package info is null, then it is not a candidate for import
159 * / qualified access
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530160 */
161 return false;
162 }
163
164 /*
165 * If the attribute type is having the package info, it is contender
166 * for import list and also need to check if it needs to be a
167 * qualified access.
168 */
169 if (newImportInfo.getPkgInfo().contentEquals(classPkg)) {
Shankara-Huaweib7564772016-08-02 18:13:13 +0530170 /*
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530171 * Package of the referred attribute and the generated class is same,
172 * so no need import
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530173 * or qualified access.
174 */
175 return false;
176 }
Bharat saraswal84366c52016-03-23 19:40:35 +0530177
Shankara-Huaweib7564772016-08-02 18:13:13 +0530178 for (JavaQualifiedTypeInfoTranslator curImportInfo : getImportSet()) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530179 if (curImportInfo.getClassInfo()
180 .contentEquals(newImportInfo.getClassInfo())) {
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530181 return !curImportInfo.getPkgInfo()
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530182 .contentEquals(newImportInfo.getPkgInfo());
183 }
184 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530185
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530186 /*
Vidyashree Ramab3670472016-08-06 15:49:56 +0530187 * Import is added, so it is a member for non qualified access
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530188 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530189 importSet.add(newImportInfo);
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530190 return false;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530191 }
Bharat saraswal84366c52016-03-23 19:40:35 +0530192
193 /**
194 * Returns import for class.
195 *
Bharat saraswal51c5d672016-11-13 09:03:48 +0530196 * @param isForInterface if needs to check for interface
Vidyashree Rama13960652016-04-26 15:06:06 +0530197 * @return imports for class
198 */
Bharat saraswal51c5d672016-11-13 09:03:48 +0530199 public List<String> getImports(boolean isForInterface) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530200
Vidyashree Rama13960652016-04-26 15:06:06 +0530201 String importString;
202 List<String> imports = new ArrayList<>();
Bharat saraswal51c5d672016-11-13 09:03:48 +0530203 boolean check;
Shankara-Huaweib7564772016-08-02 18:13:13 +0530204 for (JavaQualifiedTypeInfoTranslator importInfo : getImportSet()) {
Bharat saraswal51c5d672016-11-13 09:03:48 +0530205 check = importInfo.isForInterface();
206 if (!isForInterface) {
207 check = true;
208 }
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530209 if (!importInfo.getPkgInfo().equals(EMPTY_STRING) &&
210 importInfo.getClassInfo() != null &&
Bharat saraswal51c5d672016-11-13 09:03:48 +0530211 !importInfo.getPkgInfo().equals(JAVA_LANG) &&
212 check) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530213 importString = getImportString(importInfo.getPkgInfo(), importInfo
214 .getClassInfo());
Vidyashree Rama13960652016-04-26 15:06:06 +0530215 imports.add(importString);
216 }
217 }
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530218 if (isListToImport) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530219 imports.add(getImportForList());
Bharat saraswal51c5d672016-11-13 09:03:48 +0530220 if (!isForInterface) {
221 imports.add(ARRAY_LIST_IMPORT);
222 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530223 }
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530224 if (isQueueToImport) {
Vidyashree Ramab3670472016-08-06 15:49:56 +0530225 imports.add(getImportForQueue());
226 }
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530227 if (isSetToImport) {
Vidyashree Ramab3670472016-08-06 15:49:56 +0530228 imports.add(getImportForSet());
229 }
Bharat saraswala5c28512016-11-10 21:09:23 +0530230 if (isMapToImport) {
231 imports.add(getImportForMap());
232 }
Vidyashree Ramab3670472016-08-06 15:49:56 +0530233
Vidyashree Rama13960652016-04-26 15:06:06 +0530234 sort(imports);
235 return imports;
236 }
237
238 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530239 * Returns import for hash and equals method.
Bharat saraswal84366c52016-03-23 19:40:35 +0530240 *
241 * @return import for hash and equals method
242 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530243 String getImportForHashAndEquals() {
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530244 return getImportString(JAVA_UTIL_PKG,
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530245 JAVA_UTIL_OBJECTS_IMPORT_CLASS);
Bharat saraswal84366c52016-03-23 19:40:35 +0530246 }
247
248 /**
Shankara-Huaweia1039e52016-07-14 16:53:09 +0530249 * Returns import for to bitset method.
250 *
251 * @return import for to bitset method
252 */
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530253 public String getImportForToBitSet() {
254 return getImportString(JAVA_UTIL_PKG, BITSET);
255 }
256
257 /**
258 * Returns import for to bitset method.
259 *
260 * @return import for to bitset method
261 */
262 public String getImportForPattern() {
263 return getImportString(JAVA_UTIL_REGEX_PKG, PATTERN);
Shankara-Huaweia1039e52016-07-14 16:53:09 +0530264 }
265
266 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530267 * Returns import for list attribute.
268 *
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530269 * @return import for list attribute
Bharat saraswal84366c52016-03-23 19:40:35 +0530270 */
Bharat saraswal51c5d672016-11-13 09:03:48 +0530271 private String getImportForList() {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530272 return getImportString(COLLECTION_IMPORTS, LIST);
Bharat saraswal84366c52016-03-23 19:40:35 +0530273 }
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530274
275 /**
Bharat saraswala5c28512016-11-10 21:09:23 +0530276 * Returns import for map attribute.
277 *
278 * @return import for map attribute
279 */
280 private String getImportForMap() {
281 return getImportString(COLLECTION_IMPORTS, MAP);
282 }
283
284 /**
Vidyashree Ramab3670472016-08-06 15:49:56 +0530285 * Returns import for queue attribute.
286 *
287 * @return import for queue attribute
288 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530289 private String getImportForQueue() {
290 return getImportString(COLLECTION_IMPORTS, QUEUE);
Vidyashree Ramab3670472016-08-06 15:49:56 +0530291 }
292
293 /**
294 * Returns import for set attribute.
295 *
296 * @return import for set attribute
297 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530298 private String getImportForSet() {
299 return getImportString(COLLECTION_IMPORTS, SET);
Vidyashree Ramab3670472016-08-06 15:49:56 +0530300 }
301
302 /**
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530303 * Returns import string for ListenerService class.
304 *
305 * @return import string for ListenerService class
306 */
307 public String getListenerServiceImport() {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530308 return getImportString(ONOS_EVENT_PKG, LISTENER_SERVICE);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530309 }
310
311 /**
312 * Returns import string for AbstractEvent class.
313 *
314 * @return import string for AbstractEvent class
315 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530316 String getAbstractEventsImport() {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530317 return getImportString(ONOS_EVENT_PKG, ABSTRACT_EVENT);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530318 }
319
320 /**
321 * Returns import string for EventListener class.
322 *
323 * @return import string for EventListener class
324 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530325 String getEventListenerImport() {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530326 return getImportString(ONOS_EVENT_PKG, EVENT_LISTENER);
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530327 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530328
329 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530330 * Returns import for big integer.
331 *
332 * @return import for big integer
333 */
334 public String getBigIntegerImport() {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530335 return getImportString(JAVA_MATH, BIG_INTEGER);
Bharat saraswal64e7e232016-07-14 23:33:55 +0530336 }
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530337
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530338}