blob: aa8b53f897692af5a16d57a39c13dcb783d36db1 [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;
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053025import static org.onosproject.yangutils.utils.UtilConstants.BITSET;
Bharat saraswale2d51d62016-03-23 19:40:35 +053026import static org.onosproject.yangutils.utils.UtilConstants.COLLECTION_IMPORTS;
27import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
Bharat saraswal33dfa012016-05-17 19:59:16 +053028import static org.onosproject.yangutils.utils.UtilConstants.EVENT_LISTENER;
Bharat saraswale2d51d62016-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 saraswalb551aae2016-07-14 15:18:20 +053031import static org.onosproject.yangutils.utils.UtilConstants.HASH_MAP;
Bharat saraswale2d51d62016-03-23 19:40:35 +053032import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
33import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG;
Bharat saraswale707f032016-07-14 23:33:55 +053034import static org.onosproject.yangutils.utils.UtilConstants.JAVA_MATH;
Bharat saraswale2d51d62016-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 saraswalab4c6ba2016-05-17 14:19:38 +053038import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_REG;
39import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_SERVICE;
Bharat saraswalb551aae2016-07-14 15:18:20 +053040import static org.onosproject.yangutils.utils.UtilConstants.MAP;
Bharat saraswale2d51d62016-03-23 19:40:35 +053041import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswal33dfa012016-05-17 19:59:16 +053042import static org.onosproject.yangutils.utils.UtilConstants.ONOS_EVENT_PKG;
Bharat saraswale2d51d62016-03-23 19:40:35 +053043import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
44import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053045import static java.util.Collections.sort;
46
Vinod Kumar S38046502016-03-23 15:30:27 +053047/**
Bharat saraswald9822e92016-04-05 15:13:44 +053048 * Represents that generated Java file can contain imports.
Vinod Kumar S38046502016-03-23 15:30:27 +053049 */
50public class JavaImportData {
51
52 /**
53 * Flag to denote if any list in imported.
54 */
55 private boolean isListToImport;
56
57 /**
58 * Sorted set of import info, to be used to maintain the set of classes to
59 * be imported in the generated class.
60 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053061 private SortedSet<JavaQualifiedTypeInfoTranslator> importSet;
Vinod Kumar S38046502016-03-23 15:30:27 +053062
63 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053064 * Creates java import data object.
Vinod Kumar S38046502016-03-23 15:30:27 +053065 */
66 public JavaImportData() {
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053067 setImportSet(new TreeSet<>());
Vinod Kumar S38046502016-03-23 15:30:27 +053068 }
69
70 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053071 * Returns if the list needs to be imported.
Vinod Kumar S38046502016-03-23 15:30:27 +053072 *
Vidyashree Rama74453712016-04-18 12:29:39 +053073 * @return true if any of the attribute needs to be maintained as a list
Vinod Kumar S38046502016-03-23 15:30:27 +053074 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053075 private boolean getIfListImported() {
Vinod Kumar S38046502016-03-23 15:30:27 +053076 return isListToImport;
77 }
78
79 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053080 * Sets the status of importing list.
Vinod Kumar S38046502016-03-23 15:30:27 +053081 *
Vidyashree Rama74453712016-04-18 12:29:39 +053082 * @param isList status to mention list is bing imported
Vinod Kumar S38046502016-03-23 15:30:27 +053083 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053084 void setIfListImported(boolean isList) {
Vinod Kumar S38046502016-03-23 15:30:27 +053085 isListToImport = isList;
86 }
87
88 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053089 * Returns the set containing the imported class/interface info.
Vinod Kumar S38046502016-03-23 15:30:27 +053090 *
91 * @return the set containing the imported class/interface info
92 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053093 public SortedSet<JavaQualifiedTypeInfoTranslator> getImportSet() {
Vinod Kumar S38046502016-03-23 15:30:27 +053094 return importSet;
95 }
96
97 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053098 * Assigns the set containing the imported class/interface info.
Vinod Kumar S38046502016-03-23 15:30:27 +053099 *
100 * @param importSet the set containing the imported class/interface info
101 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530102 private void setImportSet(SortedSet<JavaQualifiedTypeInfoTranslator> importSet) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530103 this.importSet = importSet;
104 }
105
106 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530107 * Adds an imported class/interface info if it is not already part of the
Vinod Kumar S38046502016-03-23 15:30:27 +0530108 * collection.
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530109 * <p>
Vinod Kumar S38046502016-03-23 15:30:27 +0530110 * If already part of the collection, check if the packages are same, if so
111 * then return true, to denote it is already in the import collection, and
112 * it can be accessed without qualified access. If the packages do not
113 * match, then do not add to the import collection, and return false to
114 * denote, it is not added to import collection and needs to be accessed in
115 * a qualified manner.
116 *
Vinod Kumar S38046502016-03-23 15:30:27 +0530117 * @param newImportInfo class/interface info being imported
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530118 * @param className name of the call being generated
119 * @param classPkg generated class package
120 * @return qualified access status of the import node being added
Vinod Kumar S38046502016-03-23 15:30:27 +0530121 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530122 public boolean addImportInfo(JavaQualifiedTypeInfoTranslator newImportInfo,
123 String className, String classPkg) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530124
125 if (newImportInfo.getClassInfo().contentEquals(className)) {
126 /*
127 * if the current class name is same as the attribute class name,
128 * then the attribute must be accessed in a qualified manner.
129 */
130 return true;
131 } else if (newImportInfo.getPkgInfo() == null) {
132 /*
133 * If the package info is null, then it is not a candidate for import / qualified access
134 */
135 return false;
136 }
137
138 /*
139 * If the attribute type is having the package info, it is contender
140 * for import list and also need to check if it needs to be a
141 * qualified access.
142 */
143 if (newImportInfo.getPkgInfo().contentEquals(classPkg)) {
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530144 /*
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530145 * Package of the referred attribute and the generated class is same, so no need import
146 * or qualified access.
147 */
148 return false;
149 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530150
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530151 for (JavaQualifiedTypeInfoTranslator curImportInfo : getImportSet()) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530152 if (curImportInfo.getClassInfo()
153 .contentEquals(newImportInfo.getClassInfo())) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530154 return !curImportInfo.getPkgInfo()
Vinod Kumar S38046502016-03-23 15:30:27 +0530155 .contentEquals(newImportInfo.getPkgInfo());
156 }
157 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530158
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530159 /*
160 * import is added, so it is a member for non qualified access
161 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530162 getImportSet().add(newImportInfo);
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530163 return false;
Vinod Kumar S38046502016-03-23 15:30:27 +0530164 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530165
166 /**
167 * Returns import for class.
168 *
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530169 * @return imports for class
170 */
171 public List<String> getImports() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530172
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530173 String importString;
174 List<String> imports = new ArrayList<>();
175
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530176 for (JavaQualifiedTypeInfoTranslator importInfo : getImportSet()) {
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530177 if (!importInfo.getPkgInfo().equals(EMPTY_STRING) && importInfo.getClassInfo() != null
178 && !importInfo.getPkgInfo().equals(JAVA_LANG)) {
179 importString = IMPORT + importInfo.getPkgInfo() + PERIOD + importInfo.getClassInfo() + SEMI_COLAN
180 + NEW_LINE;
181
182 imports.add(importString);
183 }
184 }
185
Bharat saraswalc0e04842016-05-12 13:16:57 +0530186 if (getIfListImported()) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530187 imports.add(getImportForList());
188 }
189
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530190 sort(imports);
191 return imports;
192 }
193
194 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530195 * Returns import for hash and equals method.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530196 *
197 * @return import for hash and equals method
198 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530199 String getImportForHashAndEquals() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530200 return IMPORT + JAVA_UTIL_OBJECTS_IMPORT_PKG + PERIOD + JAVA_UTIL_OBJECTS_IMPORT_CLASS;
201 }
202
203 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530204 * Returns import for to string method.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530205 *
206 * @return import for to string method
207 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530208 String getImportForToString() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530209 return IMPORT + GOOGLE_MORE_OBJECT_IMPORT_PKG + PERIOD + GOOGLE_MORE_OBJECT_IMPORT_CLASS;
210 }
211
212 /**
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530213 * Returns import for to bitset method.
214 *
215 * @return import for to bitset method
216 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530217 String getImportForToBitSet() {
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530218 return IMPORT + JAVA_UTIL_OBJECTS_IMPORT_PKG + PERIOD + BITSET + SEMI_COLAN + NEW_LINE;
219 }
220
221 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530222 * Returns import for list attribute.
223 *
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530224 * @return import for list attribute
Bharat saraswale2d51d62016-03-23 19:40:35 +0530225 */
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530226 String getImportForList() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530227 return IMPORT + COLLECTION_IMPORTS + PERIOD + LIST + SEMI_COLAN + NEW_LINE;
228 }
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530229
230 /**
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530231 * Returns import string for ListenerService class.
232 *
233 * @return import string for ListenerService class
234 */
235 public String getListenerServiceImport() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530236 return IMPORT + ONOS_EVENT_PKG + PERIOD + LISTENER_SERVICE + SEMI_COLAN + NEW_LINE;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530237 }
238
239 /**
240 * Returns import string for ListenerRegistry class.
241 *
242 * @return import string for ListenerRegistry class
243 */
244 public String getListenerRegistryImport() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530245 return IMPORT + ONOS_EVENT_PKG + PERIOD + LISTENER_REG + SEMI_COLAN + NEW_LINE;
246 }
247
248 /**
249 * Returns import string for AbstractEvent class.
250 *
251 * @return import string for AbstractEvent class
252 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530253 String getAbstractEventsImport() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530254 return IMPORT + ONOS_EVENT_PKG + PERIOD + ABSTRACT_EVENT + SEMI_COLAN + NEW_LINE;
255 }
256
257 /**
258 * Returns import string for EventListener class.
259 *
260 * @return import string for EventListener class
261 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530262 String getEventListenerImport() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530263 return IMPORT + ONOS_EVENT_PKG + PERIOD + EVENT_LISTENER + SEMI_COLAN + NEW_LINE;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530264 }
Bharat saraswalb551aae2016-07-14 15:18:20 +0530265
266 /**
267 * Returns import string for map class.
268 *
269 * @return import string for map class
270 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530271 String getMapImport() {
Bharat saraswalb551aae2016-07-14 15:18:20 +0530272 return IMPORT + COLLECTION_IMPORTS + PERIOD + MAP + SEMI_COLAN + NEW_LINE;
273 }
274
275 /**
276 * Returns import string for hash map class.
277 *
278 * @return import string for hash map class
279 */
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530280 String getHashMapImport() {
Bharat saraswalb551aae2016-07-14 15:18:20 +0530281 return IMPORT + COLLECTION_IMPORTS + PERIOD + HASH_MAP + SEMI_COLAN + NEW_LINE;
282 }
283
284 /**
Bharat saraswale707f032016-07-14 23:33:55 +0530285 * Returns import for big integer.
286 *
287 * @return import for big integer
288 */
289 public String getBigIntegerImport() {
290 return IMPORT + JAVA_MATH + PERIOD +
291 BIG_INTEGER + SEMI_COLAN + NEW_LINE;
292 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530293}