blob: e9eafbf6314e68e0b426c7137e00cf409ab8ca97 [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 saraswalb551aae2016-07-14 15:18:20 +053024import static org.onosproject.yangutils.utils.UtilConstants.YANG_AUGMENTED_INFO_CLASS_IMPORT_CLASS;
25import static org.onosproject.yangutils.utils.UtilConstants.YANG_AUGMENTED_INFO_CLASS_IMPORT_PKG;
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;
34import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_CLASS;
35import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_PKG;
36import static org.onosproject.yangutils.utils.UtilConstants.LIST;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053037import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_REG;
38import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_SERVICE;
Bharat saraswalb551aae2016-07-14 15:18:20 +053039import static org.onosproject.yangutils.utils.UtilConstants.MAP;
Bharat saraswale2d51d62016-03-23 19:40:35 +053040import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswal33dfa012016-05-17 19:59:16 +053041import static org.onosproject.yangutils.utils.UtilConstants.ONOS_EVENT_PKG;
Bharat saraswale2d51d62016-03-23 19:40:35 +053042import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
43import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053044import static java.util.Collections.sort;
45
Vinod Kumar S38046502016-03-23 15:30:27 +053046/**
Bharat saraswald9822e92016-04-05 15:13:44 +053047 * Represents that generated Java file can contain imports.
Vinod Kumar S38046502016-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 /**
57 * Sorted set of import info, to be used to maintain the set of classes to
58 * be imported in the generated class.
59 */
60 private SortedSet<JavaQualifiedTypeInfo> importSet;
61
62 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053063 * Creates java import data object.
Vinod Kumar S38046502016-03-23 15:30:27 +053064 */
65 public JavaImportData() {
66 setImportSet(new TreeSet<JavaQualifiedTypeInfo>());
67 }
68
69 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053070 * Returns if the list needs to be imported.
Vinod Kumar S38046502016-03-23 15:30:27 +053071 *
Vidyashree Rama74453712016-04-18 12:29:39 +053072 * @return true if any of the attribute needs to be maintained as a list
Vinod Kumar S38046502016-03-23 15:30:27 +053073 */
74 public boolean getIfListImported() {
75 return isListToImport;
76 }
77
78 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053079 * Sets the status of importing list.
Vinod Kumar S38046502016-03-23 15:30:27 +053080 *
Vidyashree Rama74453712016-04-18 12:29:39 +053081 * @param isList status to mention list is bing imported
Vinod Kumar S38046502016-03-23 15:30:27 +053082 */
83 public void setIfListImported(boolean isList) {
84 isListToImport = isList;
85 }
86
87 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053088 * Returns the set containing the imported class/interface info.
Vinod Kumar S38046502016-03-23 15:30:27 +053089 *
90 * @return the set containing the imported class/interface info
91 */
92 public SortedSet<JavaQualifiedTypeInfo> getImportSet() {
93 return importSet;
94 }
95
96 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053097 * Assigns the set containing the imported class/interface info.
Vinod Kumar S38046502016-03-23 15:30:27 +053098 *
99 * @param importSet the set containing the imported class/interface info
100 */
101 private void setImportSet(SortedSet<JavaQualifiedTypeInfo> importSet) {
102 this.importSet = importSet;
103 }
104
105 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530106 * Adds an imported class/interface info if it is not already part of the
Vinod Kumar S38046502016-03-23 15:30:27 +0530107 * collection.
108 *
109 * If already part of the collection, check if the packages are same, if so
110 * then return true, to denote it is already in the import collection, and
111 * it can be accessed without qualified access. If the packages do not
112 * match, then do not add to the import collection, and return false to
113 * denote, it is not added to import collection and needs to be accessed in
114 * a qualified manner.
115 *
Vinod Kumar S38046502016-03-23 15:30:27 +0530116 * @param newImportInfo class/interface info being imported
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530117 * @param className name of the call being generated
118 * @param classPkg generated class package
119 * @return qualified access status of the import node being added
Vinod Kumar S38046502016-03-23 15:30:27 +0530120 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530121 public boolean addImportInfo(JavaQualifiedTypeInfo newImportInfo,
122 String className, String classPkg) {
123
124 if (newImportInfo.getClassInfo().contentEquals(className)) {
125 /*
126 * if the current class name is same as the attribute class name,
127 * then the attribute must be accessed in a qualified manner.
128 */
129 return true;
130 } else if (newImportInfo.getPkgInfo() == null) {
131 /*
132 * If the package info is null, then it is not a candidate for import / qualified access
133 */
134 return false;
135 }
136
137 /*
138 * If the attribute type is having the package info, it is contender
139 * for import list and also need to check if it needs to be a
140 * qualified access.
141 */
142 if (newImportInfo.getPkgInfo().contentEquals(classPkg)) {
143 /**
144 * Package of the referred attribute and the generated class is same, so no need import
145 * or qualified access.
146 */
147 return false;
148 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530149
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530150 for (JavaQualifiedTypeInfo curImportInfo : getImportSet()) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530151 if (curImportInfo.getClassInfo()
152 .contentEquals(newImportInfo.getClassInfo())) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530153 return !curImportInfo.getPkgInfo()
Vinod Kumar S38046502016-03-23 15:30:27 +0530154 .contentEquals(newImportInfo.getPkgInfo());
155 }
156 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530157
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530158 /*
159 * import is added, so it is a member for non qualified access
160 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530161 getImportSet().add(newImportInfo);
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530162 return false;
Vinod Kumar S38046502016-03-23 15:30:27 +0530163 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530164
165 /**
166 * Returns import for class.
167 *
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530168 * @return imports for class
169 */
170 public List<String> getImports() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530171
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530172 String importString;
173 List<String> imports = new ArrayList<>();
174
175 for (JavaQualifiedTypeInfo importInfo : getImportSet()) {
176 if (!importInfo.getPkgInfo().equals(EMPTY_STRING) && importInfo.getClassInfo() != null
177 && !importInfo.getPkgInfo().equals(JAVA_LANG)) {
178 importString = IMPORT + importInfo.getPkgInfo() + PERIOD + importInfo.getClassInfo() + SEMI_COLAN
179 + NEW_LINE;
180
181 imports.add(importString);
182 }
183 }
184
Bharat saraswalc0e04842016-05-12 13:16:57 +0530185 if (getIfListImported()) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530186 imports.add(getImportForList());
187 }
188
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530189 sort(imports);
190 return imports;
191 }
192
193 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530194 * Returns import for hash and equals method.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530195 *
196 * @return import for hash and equals method
197 */
198 public String getImportForHashAndEquals() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530199 return IMPORT + JAVA_UTIL_OBJECTS_IMPORT_PKG + PERIOD + JAVA_UTIL_OBJECTS_IMPORT_CLASS;
200 }
201
202 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530203 * Returns import for to string method.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530204 *
205 * @return import for to string method
206 */
207 public String getImportForToString() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530208 return IMPORT + GOOGLE_MORE_OBJECT_IMPORT_PKG + PERIOD + GOOGLE_MORE_OBJECT_IMPORT_CLASS;
209 }
210
211 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530212 * Returns import for list attribute.
213 *
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530214 * @return import for list attribute
Bharat saraswale2d51d62016-03-23 19:40:35 +0530215 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530216 public String getImportForList() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530217 return IMPORT + COLLECTION_IMPORTS + PERIOD + LIST + SEMI_COLAN + NEW_LINE;
218 }
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530219
220 /**
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530221 * Returns import string for ListenerService class.
222 *
223 * @return import string for ListenerService class
224 */
225 public String getListenerServiceImport() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530226 return IMPORT + ONOS_EVENT_PKG + PERIOD + LISTENER_SERVICE + SEMI_COLAN + NEW_LINE;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530227 }
228
229 /**
230 * Returns import string for ListenerRegistry class.
231 *
232 * @return import string for ListenerRegistry class
233 */
234 public String getListenerRegistryImport() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530235 return IMPORT + ONOS_EVENT_PKG + PERIOD + LISTENER_REG + SEMI_COLAN + NEW_LINE;
236 }
237
238 /**
239 * Returns import string for AbstractEvent class.
240 *
241 * @return import string for AbstractEvent class
242 */
243 public String getAbstractEventsImport() {
244 return IMPORT + ONOS_EVENT_PKG + PERIOD + ABSTRACT_EVENT + SEMI_COLAN + NEW_LINE;
245 }
246
247 /**
248 * Returns import string for EventListener class.
249 *
250 * @return import string for EventListener class
251 */
252 public String getEventListenerImport() {
253 return IMPORT + ONOS_EVENT_PKG + PERIOD + EVENT_LISTENER + SEMI_COLAN + NEW_LINE;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530254 }
Bharat saraswalb551aae2016-07-14 15:18:20 +0530255
256 /**
257 * Returns import string for map class.
258 *
259 * @return import string for map class
260 */
261 public String getMapImport() {
262 return IMPORT + COLLECTION_IMPORTS + PERIOD + MAP + SEMI_COLAN + NEW_LINE;
263 }
264
265 /**
266 * Returns import string for hash map class.
267 *
268 * @return import string for hash map class
269 */
270 public String getHashMapImport() {
271 return IMPORT + COLLECTION_IMPORTS + PERIOD + HASH_MAP + SEMI_COLAN + NEW_LINE;
272 }
273
274 /**
275 * Returns import string for hash map class.
276 *
277 * @return import string for hash map class
278 */
279 public String getYangAugmentedInfoImport() {
280 return IMPORT + YANG_AUGMENTED_INFO_CLASS_IMPORT_PKG + PERIOD + YANG_AUGMENTED_INFO_CLASS_IMPORT_CLASS;
281 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530282}