blob: f2e70199f7b5e98f28bf8b68327308053605ee33 [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 saraswal64e7e232016-07-14 23:33:55 +053027import static org.onosproject.yangutils.utils.UtilConstants.BIG_INTEGER;
Shankara-Huaweib7564772016-08-02 18:13:13 +053028import static org.onosproject.yangutils.utils.UtilConstants.BITSET;
Bharat saraswal84366c52016-03-23 19:40:35 +053029import static org.onosproject.yangutils.utils.UtilConstants.COLLECTION_IMPORTS;
30import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
Bharat saraswal715d3fc2016-05-17 19:59:16 +053031import static org.onosproject.yangutils.utils.UtilConstants.EVENT_LISTENER;
Bharat saraswal84366c52016-03-23 19:40:35 +053032import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_CLASS;
33import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_PKG;
Bharat saraswalaf413b82016-07-14 15:18:20 +053034import static org.onosproject.yangutils.utils.UtilConstants.HASH_MAP;
Bharat saraswal84366c52016-03-23 19:40:35 +053035import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG;
Bharat saraswal64e7e232016-07-14 23:33:55 +053036import static org.onosproject.yangutils.utils.UtilConstants.JAVA_MATH;
Bharat saraswal84366c52016-03-23 19:40:35 +053037import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_CLASS;
38import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_PKG;
39import static org.onosproject.yangutils.utils.UtilConstants.LIST;
Bharat saraswal4aaab4d2016-05-17 14:19:38 +053040import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_SERVICE;
Bharat saraswalaf413b82016-07-14 15:18:20 +053041import static org.onosproject.yangutils.utils.UtilConstants.MAP;
Bharat saraswal715d3fc2016-05-17 19:59:16 +053042import static org.onosproject.yangutils.utils.UtilConstants.ONOS_EVENT_PKG;
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 /**
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053067 * Sorted set of import info, to be used to maintain the set of classes to
68 * be imported in the generated class.
69 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +053070 private final SortedSet<JavaQualifiedTypeInfoTranslator> importSet;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053071
72 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053073 * Creates java import data object.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053074 */
75 public JavaImportData() {
Bharat saraswal9fab16b2016-09-23 23:27:24 +053076 importSet = new TreeSet<>();
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053077 }
78
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053079
80 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053081 * Sets the status of importing list.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053082 *
Vidyashree Rama02f115f2016-04-18 12:29:39 +053083 * @param isList status to mention list is bing imported
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053084 */
Shankara-Huaweib7564772016-08-02 18:13:13 +053085 void setIfListImported(boolean isList) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053086 isListToImport = isList;
87 }
88
Vidyashree Ramab3670472016-08-06 15:49:56 +053089
90 /**
91 * Sets the status of the queue to be imported due to compiler annotations.
92 *
93 * @param queueToImport status of queue to import
94 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +053095 void setQueueToImport(boolean queueToImport) {
Vidyashree Ramab3670472016-08-06 15:49:56 +053096 isQueueToImport = queueToImport;
97 }
98
99 /**
100 * Sets the status of the set to be imported due to compiler annotations.
101 *
102 * @param setToImport status of set to import
103 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530104 void setSetToImport(boolean setToImport) {
Vidyashree Ramab3670472016-08-06 15:49:56 +0530105 isSetToImport = setToImport;
106 }
107
108 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530109 * Returns the set containing the imported class/interface info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530110 *
111 * @return the set containing the imported class/interface info
112 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530113 public SortedSet<JavaQualifiedTypeInfoTranslator> getImportSet() {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530114 return unmodifiableSortedSet(importSet);
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530115 }
116
117 /**
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530118 * Adds an imported class/interface info if it is not already part of the
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530119 * collection.
Shankara-Huaweib7564772016-08-02 18:13:13 +0530120 * <p>
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530121 * If already part of the collection, check if the packages are same, if so
122 * then return true, to denote it is already in the import collection, and
123 * it can be accessed without qualified access. If the packages do not
124 * match, then do not add to the import collection, and return false to
125 * denote, it is not added to import collection and needs to be accessed in
126 * a qualified manner.
127 *
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530128 * @param newImportInfo class/interface info being imported
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530129 * @param className name of the call being generated
130 * @param classPkg generated class package
131 * @return qualified access status of the import node being added
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530132 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530133 public boolean addImportInfo(JavaQualifiedTypeInfoTranslator newImportInfo,
134 String className, String classPkg) {
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530135
136 if (newImportInfo.getClassInfo().contentEquals(className)) {
137 /*
Vidyashree Ramab3670472016-08-06 15:49:56 +0530138 * If the current class name is same as the attribute class name,
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530139 * then the attribute must be accessed in a qualified manner.
140 */
141 return true;
142 } else if (newImportInfo.getPkgInfo() == null) {
143 /*
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530144 * If the package info is null, then it is not a candidate for import
145 * / qualified access
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530146 */
147 return false;
148 }
149
150 /*
151 * If the attribute type is having the package info, it is contender
152 * for import list and also need to check if it needs to be a
153 * qualified access.
154 */
155 if (newImportInfo.getPkgInfo().contentEquals(classPkg)) {
Shankara-Huaweib7564772016-08-02 18:13:13 +0530156 /*
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530157 * Package of the referred attribute and the generated class is same,
158 * so no need import
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530159 * or qualified access.
160 */
161 return false;
162 }
Bharat saraswal84366c52016-03-23 19:40:35 +0530163
Shankara-Huaweib7564772016-08-02 18:13:13 +0530164 for (JavaQualifiedTypeInfoTranslator curImportInfo : getImportSet()) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530165 if (curImportInfo.getClassInfo()
166 .contentEquals(newImportInfo.getClassInfo())) {
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530167 return !curImportInfo.getPkgInfo()
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530168 .contentEquals(newImportInfo.getPkgInfo());
169 }
170 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530171
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530172 /*
Vidyashree Ramab3670472016-08-06 15:49:56 +0530173 * Import is added, so it is a member for non qualified access
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530174 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530175 importSet.add(newImportInfo);
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530176 return false;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530177 }
Bharat saraswal84366c52016-03-23 19:40:35 +0530178
179 /**
180 * Returns import for class.
181 *
Vidyashree Rama13960652016-04-26 15:06:06 +0530182 * @return imports for class
183 */
184 public List<String> getImports() {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530185
Vidyashree Rama13960652016-04-26 15:06:06 +0530186 String importString;
187 List<String> imports = new ArrayList<>();
188
Shankara-Huaweib7564772016-08-02 18:13:13 +0530189 for (JavaQualifiedTypeInfoTranslator importInfo : getImportSet()) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530190 if (!importInfo.getPkgInfo().equals(EMPTY_STRING) &&
191 importInfo.getClassInfo() != null &&
192 !importInfo.getPkgInfo().equals(JAVA_LANG)) {
193 importString = getImportString(importInfo.getPkgInfo(), importInfo
194 .getClassInfo());
Vidyashree Rama13960652016-04-26 15:06:06 +0530195 imports.add(importString);
196 }
197 }
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530198 if (isListToImport) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530199 imports.add(getImportForList());
200 }
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530201 if (isQueueToImport) {
Vidyashree Ramab3670472016-08-06 15:49:56 +0530202 imports.add(getImportForQueue());
203 }
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530204 if (isSetToImport) {
Vidyashree Ramab3670472016-08-06 15:49:56 +0530205 imports.add(getImportForSet());
206 }
207
Vidyashree Rama13960652016-04-26 15:06:06 +0530208 sort(imports);
209 return imports;
210 }
211
212 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530213 * Returns import for hash and equals method.
Bharat saraswal84366c52016-03-23 19:40:35 +0530214 *
215 * @return import for hash and equals method
216 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530217 String getImportForHashAndEquals() {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530218 return getImportString(JAVA_UTIL_OBJECTS_IMPORT_PKG,
219 JAVA_UTIL_OBJECTS_IMPORT_CLASS);
Bharat saraswal84366c52016-03-23 19:40:35 +0530220 }
221
222 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530223 * Returns import for to string method.
Bharat saraswal84366c52016-03-23 19:40:35 +0530224 *
225 * @return import for to string method
226 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530227 String getImportForToString() {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530228 return getImportString(GOOGLE_MORE_OBJECT_IMPORT_PKG,
229 GOOGLE_MORE_OBJECT_IMPORT_CLASS);
Bharat saraswal84366c52016-03-23 19:40:35 +0530230 }
231
232 /**
Shankara-Huaweia1039e52016-07-14 16:53:09 +0530233 * Returns import for to bitset method.
234 *
235 * @return import for to bitset method
236 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530237 String getImportForToBitSet() {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530238 return getImportString(JAVA_UTIL_OBJECTS_IMPORT_PKG, BITSET);
Shankara-Huaweia1039e52016-07-14 16:53:09 +0530239 }
240
241 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530242 * Returns import for list attribute.
243 *
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530244 * @return import for list attribute
Bharat saraswal84366c52016-03-23 19:40:35 +0530245 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530246 String getImportForList() {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530247 return getImportString(COLLECTION_IMPORTS, LIST);
Bharat saraswal84366c52016-03-23 19:40:35 +0530248 }
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530249
250 /**
Vidyashree Ramab3670472016-08-06 15:49:56 +0530251 * Returns import for queue attribute.
252 *
253 * @return import for queue attribute
254 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530255 private String getImportForQueue() {
256 return getImportString(COLLECTION_IMPORTS, QUEUE);
Vidyashree Ramab3670472016-08-06 15:49:56 +0530257 }
258
259 /**
260 * Returns import for set attribute.
261 *
262 * @return import for set attribute
263 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530264 private String getImportForSet() {
265 return getImportString(COLLECTION_IMPORTS, SET);
Vidyashree Ramab3670472016-08-06 15:49:56 +0530266 }
267
268 /**
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530269 * Returns import string for ListenerService class.
270 *
271 * @return import string for ListenerService class
272 */
273 public String getListenerServiceImport() {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530274 return getImportString(ONOS_EVENT_PKG, LISTENER_SERVICE);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530275 }
276
277 /**
278 * Returns import string for AbstractEvent class.
279 *
280 * @return import string for AbstractEvent class
281 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530282 String getAbstractEventsImport() {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530283 return getImportString(ONOS_EVENT_PKG, ABSTRACT_EVENT);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530284 }
285
286 /**
287 * Returns import string for EventListener class.
288 *
289 * @return import string for EventListener class
290 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530291 String getEventListenerImport() {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530292 return getImportString(ONOS_EVENT_PKG, EVENT_LISTENER);
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530293 }
Bharat saraswalaf413b82016-07-14 15:18:20 +0530294
295 /**
296 * Returns import string for map class.
297 *
298 * @return import string for map class
299 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530300 String getMapImport() {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530301 return getImportString(COLLECTION_IMPORTS, MAP);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530302 }
303
304 /**
305 * Returns import string for hash map class.
306 *
307 * @return import string for hash map class
308 */
Shankara-Huaweib7564772016-08-02 18:13:13 +0530309 String getHashMapImport() {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530310 return getImportString(COLLECTION_IMPORTS, HASH_MAP);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530311 }
312
313 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530314 * Returns import for big integer.
315 *
316 * @return import for big integer
317 */
318 public String getBigIntegerImport() {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530319 return getImportString(JAVA_MATH, BIG_INTEGER);
Bharat saraswal64e7e232016-07-14 23:33:55 +0530320 }
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530321
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530322}