blob: b1c36ecb70228cfdd2c867d31c9975c3860a9f38 [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 saraswalcc1cdab2016-04-16 02:28:25 +053024import static org.onosproject.yangutils.utils.UtilConstants.ARRAY_LIST;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053025import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTATION_HOLDER_CLASS_IMPORT_CLASS;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053026import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO_CLASS_IMPORT_CLASS;
27import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO_CLASS_IMPORT_PKG;
Bharat saraswale2d51d62016-03-23 19:40:35 +053028import static org.onosproject.yangutils.utils.UtilConstants.COLLECTION_IMPORTS;
29import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
Bharat saraswal33dfa012016-05-17 19:59:16 +053030import static org.onosproject.yangutils.utils.UtilConstants.EVENT_LISTENER;
Bharat saraswale2d51d62016-03-23 19:40:35 +053031import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_CLASS;
32import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_PKG;
33import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
34import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG;
35import 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 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;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053043import static org.onosproject.yangutils.utils.UtilConstants.PROVIDED_AUGMENTATION_CLASS_IMPORT_PKG;
Bharat saraswale2d51d62016-03-23 19:40:35 +053044import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
45
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053046import static java.util.Collections.sort;
47
Vinod Kumar S38046502016-03-23 15:30:27 +053048/**
Bharat saraswald9822e92016-04-05 15:13:44 +053049 * Represents that generated Java file can contain imports.
Vinod Kumar S38046502016-03-23 15:30:27 +053050 */
51public class JavaImportData {
52
53 /**
54 * Flag to denote if any list in imported.
55 */
56 private boolean isListToImport;
57
58 /**
59 * Sorted set of import info, to be used to maintain the set of classes to
60 * be imported in the generated class.
61 */
62 private SortedSet<JavaQualifiedTypeInfo> importSet;
63
64 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053065 * Creates java import data object.
Vinod Kumar S38046502016-03-23 15:30:27 +053066 */
67 public JavaImportData() {
68 setImportSet(new TreeSet<JavaQualifiedTypeInfo>());
69 }
70
71 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053072 * Returns if the list needs to be imported.
Vinod Kumar S38046502016-03-23 15:30:27 +053073 *
Vidyashree Rama74453712016-04-18 12:29:39 +053074 * @return true if any of the attribute needs to be maintained as a list
Vinod Kumar S38046502016-03-23 15:30:27 +053075 */
76 public boolean getIfListImported() {
77 return isListToImport;
78 }
79
80 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053081 * Sets the status of importing list.
Vinod Kumar S38046502016-03-23 15:30:27 +053082 *
Vidyashree Rama74453712016-04-18 12:29:39 +053083 * @param isList status to mention list is bing imported
Vinod Kumar S38046502016-03-23 15:30:27 +053084 */
85 public void setIfListImported(boolean isList) {
86 isListToImport = isList;
87 }
88
89 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053090 * Returns the set containing the imported class/interface info.
Vinod Kumar S38046502016-03-23 15:30:27 +053091 *
92 * @return the set containing the imported class/interface info
93 */
94 public SortedSet<JavaQualifiedTypeInfo> getImportSet() {
95 return importSet;
96 }
97
98 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053099 * Assigns the set containing the imported class/interface info.
Vinod Kumar S38046502016-03-23 15:30:27 +0530100 *
101 * @param importSet the set containing the imported class/interface info
102 */
103 private void setImportSet(SortedSet<JavaQualifiedTypeInfo> importSet) {
104 this.importSet = importSet;
105 }
106
107 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530108 * Adds an imported class/interface info if it is not already part of the
Vinod Kumar S38046502016-03-23 15:30:27 +0530109 * collection.
110 *
111 * If already part of the collection, check if the packages are same, if so
112 * then return true, to denote it is already in the import collection, and
113 * it can be accessed without qualified access. If the packages do not
114 * match, then do not add to the import collection, and return false to
115 * denote, it is not added to import collection and needs to be accessed in
116 * a qualified manner.
117 *
Vinod Kumar S38046502016-03-23 15:30:27 +0530118 * @param newImportInfo class/interface info being imported
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530119 * @param className name of the call being generated
120 * @param classPkg generated class package
121 * @return qualified access status of the import node being added
Vinod Kumar S38046502016-03-23 15:30:27 +0530122 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530123 public boolean addImportInfo(JavaQualifiedTypeInfo newImportInfo,
124 String className, String classPkg) {
125
126 if (newImportInfo.getClassInfo().contentEquals(className)) {
127 /*
128 * if the current class name is same as the attribute class name,
129 * then the attribute must be accessed in a qualified manner.
130 */
131 return true;
132 } else if (newImportInfo.getPkgInfo() == null) {
133 /*
134 * If the package info is null, then it is not a candidate for import / qualified access
135 */
136 return false;
137 }
138
139 /*
140 * If the attribute type is having the package info, it is contender
141 * for import list and also need to check if it needs to be a
142 * qualified access.
143 */
144 if (newImportInfo.getPkgInfo().contentEquals(classPkg)) {
145 /**
146 * Package of the referred attribute and the generated class is same, so no need import
147 * or qualified access.
148 */
149 return false;
150 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530151
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530152 for (JavaQualifiedTypeInfo curImportInfo : getImportSet()) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530153 if (curImportInfo.getClassInfo()
154 .contentEquals(newImportInfo.getClassInfo())) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530155 return !curImportInfo.getPkgInfo()
Vinod Kumar S38046502016-03-23 15:30:27 +0530156 .contentEquals(newImportInfo.getPkgInfo());
157 }
158 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530159
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530160 /*
161 * import is added, so it is a member for non qualified access
162 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530163 getImportSet().add(newImportInfo);
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530164 return false;
Vinod Kumar S38046502016-03-23 15:30:27 +0530165 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530166
167 /**
168 * Returns import for class.
169 *
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530170 * @return imports for class
171 */
172 public List<String> getImports() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530173
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530174 String importString;
175 List<String> imports = new ArrayList<>();
176
177 for (JavaQualifiedTypeInfo importInfo : getImportSet()) {
178 if (!importInfo.getPkgInfo().equals(EMPTY_STRING) && importInfo.getClassInfo() != null
179 && !importInfo.getPkgInfo().equals(JAVA_LANG)) {
180 importString = IMPORT + importInfo.getPkgInfo() + PERIOD + importInfo.getClassInfo() + SEMI_COLAN
181 + NEW_LINE;
182
183 imports.add(importString);
184 }
185 }
186
Bharat saraswalc0e04842016-05-12 13:16:57 +0530187 if (getIfListImported()) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530188 imports.add(getImportForList());
189 }
190
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530191 sort(imports);
192 return imports;
193 }
194
195 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530196 * Returns import for hash and equals method.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530197 *
198 * @return import for hash and equals method
199 */
200 public String getImportForHashAndEquals() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530201 return IMPORT + JAVA_UTIL_OBJECTS_IMPORT_PKG + PERIOD + JAVA_UTIL_OBJECTS_IMPORT_CLASS;
202 }
203
204 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530205 * Returns import for to string method.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530206 *
207 * @return import for to string method
208 */
209 public String getImportForToString() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530210 return IMPORT + GOOGLE_MORE_OBJECT_IMPORT_PKG + PERIOD + GOOGLE_MORE_OBJECT_IMPORT_CLASS;
211 }
212
213 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530214 * Returns import for list attribute.
215 *
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530216 * @return import for list attribute
Bharat saraswale2d51d62016-03-23 19:40:35 +0530217 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530218 public String getImportForList() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530219 return IMPORT + COLLECTION_IMPORTS + PERIOD + LIST + SEMI_COLAN + NEW_LINE;
220 }
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530221
222 /**
223 * Returns import for array list attribute.
224 *
225 * @return import for array list attribute
226 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530227 public String getImportForArrayList() {
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530228 return IMPORT + COLLECTION_IMPORTS + PERIOD + ARRAY_LIST + SEMI_COLAN + NEW_LINE;
229 }
230
231 /**
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530232 * Returns import string for AugmentationHolder class.
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530233 *
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530234 * @return import string for AugmentationHolder class
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530235 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530236 public String getAugmentationHolderImport() {
237 return IMPORT + PROVIDED_AUGMENTATION_CLASS_IMPORT_PKG + PERIOD + AUGMENTATION_HOLDER_CLASS_IMPORT_CLASS;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530238 }
239
240 /**
241 * Returns import string for AugmentedInfo class.
242 *
243 * @return import string for AugmentedInfo class
244 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530245 public String getAugmentedInfoImport() {
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530246 return IMPORT + AUGMENTED_INFO_CLASS_IMPORT_PKG + PERIOD + AUGMENTED_INFO_CLASS_IMPORT_CLASS;
247 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530248
249 /**
250 * Returns import string for ListenerService class.
251 *
252 * @return import string for ListenerService class
253 */
254 public String getListenerServiceImport() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530255 return IMPORT + ONOS_EVENT_PKG + PERIOD + LISTENER_SERVICE + SEMI_COLAN + NEW_LINE;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530256 }
257
258 /**
259 * Returns import string for ListenerRegistry class.
260 *
261 * @return import string for ListenerRegistry class
262 */
263 public String getListenerRegistryImport() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530264 return IMPORT + ONOS_EVENT_PKG + PERIOD + LISTENER_REG + SEMI_COLAN + NEW_LINE;
265 }
266
267 /**
268 * Returns import string for AbstractEvent class.
269 *
270 * @return import string for AbstractEvent class
271 */
272 public String getAbstractEventsImport() {
273 return IMPORT + ONOS_EVENT_PKG + PERIOD + ABSTRACT_EVENT + SEMI_COLAN + NEW_LINE;
274 }
275
276 /**
277 * Returns import string for EventListener class.
278 *
279 * @return import string for EventListener class
280 */
281 public String getEventListenerImport() {
282 return IMPORT + ONOS_EVENT_PKG + PERIOD + EVENT_LISTENER + SEMI_COLAN + NEW_LINE;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530283 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530284}