blob: 5ccf0b3c27642e8f69e9b2dff81b21d2dcbdb7c4 [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 saraswalab4c6ba2016-05-17 14:19:38 +053023import static java.util.Collections.sort;
24
Bharat saraswal33dfa012016-05-17 19:59:16 +053025import static org.onosproject.yangutils.utils.UtilConstants.ABSTRACT_EVENT;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053026import static org.onosproject.yangutils.utils.UtilConstants.ARRAY_LIST;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053027import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTATION_HOLDER_CLASS_IMPORT_CLASS;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053028import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO_CLASS_IMPORT_CLASS;
29import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO_CLASS_IMPORT_PKG;
Bharat saraswale2d51d62016-03-23 19:40:35 +053030import static org.onosproject.yangutils.utils.UtilConstants.COLLECTION_IMPORTS;
31import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
Bharat saraswal33dfa012016-05-17 19:59:16 +053032import static org.onosproject.yangutils.utils.UtilConstants.EVENT_LISTENER;
Bharat saraswale2d51d62016-03-23 19:40:35 +053033import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_CLASS;
34import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_PKG;
35import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
36import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG;
37import 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 saraswalab4c6ba2016-05-17 14:19:38 +053040import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_REG;
41import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_SERVICE;
Bharat saraswale2d51d62016-03-23 19:40:35 +053042import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswal33dfa012016-05-17 19:59:16 +053043import static org.onosproject.yangutils.utils.UtilConstants.ONOS_EVENT_PKG;
Bharat saraswale2d51d62016-03-23 19:40:35 +053044import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053045import static org.onosproject.yangutils.utils.UtilConstants.PROVIDED_AUGMENTATION_CLASS_IMPORT_PKG;
Bharat saraswale2d51d62016-03-23 19:40:35 +053046import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
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
119 * @return status of new addition of class/interface to the import set
120 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530121 public boolean addImportInfo(JavaQualifiedTypeInfo newImportInfo) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530122
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530123 for (JavaQualifiedTypeInfo curImportInfo : getImportSet()) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530124 if (curImportInfo.getClassInfo()
125 .contentEquals(newImportInfo.getClassInfo())) {
126 return curImportInfo.getPkgInfo()
127 .contentEquals(newImportInfo.getPkgInfo());
128 }
129 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530130
131 getImportSet().add(newImportInfo);
Vinod Kumar S38046502016-03-23 15:30:27 +0530132 return true;
133 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530134
135 /**
136 * Returns import for class.
137 *
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530138 * @return imports for class
139 */
140 public List<String> getImports() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530141
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530142 String importString;
143 List<String> imports = new ArrayList<>();
144
145 for (JavaQualifiedTypeInfo importInfo : getImportSet()) {
146 if (!importInfo.getPkgInfo().equals(EMPTY_STRING) && importInfo.getClassInfo() != null
147 && !importInfo.getPkgInfo().equals(JAVA_LANG)) {
148 importString = IMPORT + importInfo.getPkgInfo() + PERIOD + importInfo.getClassInfo() + SEMI_COLAN
149 + NEW_LINE;
150
151 imports.add(importString);
152 }
153 }
154
Bharat saraswalc0e04842016-05-12 13:16:57 +0530155 if (getIfListImported()) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530156 imports.add(getImportForList());
157 }
158
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530159 sort(imports);
160 return imports;
161 }
162
163 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530164 * Returns import for hash and equals method.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530165 *
166 * @return import for hash and equals method
167 */
168 public String getImportForHashAndEquals() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530169 return IMPORT + JAVA_UTIL_OBJECTS_IMPORT_PKG + PERIOD + JAVA_UTIL_OBJECTS_IMPORT_CLASS;
170 }
171
172 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530173 * Returns import for to string method.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530174 *
175 * @return import for to string method
176 */
177 public String getImportForToString() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530178 return IMPORT + GOOGLE_MORE_OBJECT_IMPORT_PKG + PERIOD + GOOGLE_MORE_OBJECT_IMPORT_CLASS;
179 }
180
181 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530182 * Returns import for list attribute.
183 *
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530184 * @return import for list attribute
Bharat saraswale2d51d62016-03-23 19:40:35 +0530185 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530186 public String getImportForList() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530187 return IMPORT + COLLECTION_IMPORTS + PERIOD + LIST + SEMI_COLAN + NEW_LINE;
188 }
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530189
190 /**
191 * Returns import for array list attribute.
192 *
193 * @return import for array list attribute
194 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530195 public String getImportForArrayList() {
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530196 return IMPORT + COLLECTION_IMPORTS + PERIOD + ARRAY_LIST + SEMI_COLAN + NEW_LINE;
197 }
198
199 /**
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530200 * Returns import string for AugmentationHolder class.
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530201 *
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530202 * @return import string for AugmentationHolder class
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530203 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530204 public String getAugmentationHolderImport() {
205 return IMPORT + PROVIDED_AUGMENTATION_CLASS_IMPORT_PKG + PERIOD + AUGMENTATION_HOLDER_CLASS_IMPORT_CLASS;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530206 }
207
208 /**
209 * Returns import string for AugmentedInfo class.
210 *
211 * @return import string for AugmentedInfo class
212 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530213 public String getAugmentedInfoImport() {
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530214 return IMPORT + AUGMENTED_INFO_CLASS_IMPORT_PKG + PERIOD + AUGMENTED_INFO_CLASS_IMPORT_CLASS;
215 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530216
217 /**
218 * Returns import string for ListenerService class.
219 *
220 * @return import string for ListenerService class
221 */
222 public String getListenerServiceImport() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530223 return IMPORT + ONOS_EVENT_PKG + PERIOD + LISTENER_SERVICE + SEMI_COLAN + NEW_LINE;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530224 }
225
226 /**
227 * Returns import string for ListenerRegistry class.
228 *
229 * @return import string for ListenerRegistry class
230 */
231 public String getListenerRegistryImport() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530232 return IMPORT + ONOS_EVENT_PKG + PERIOD + LISTENER_REG + SEMI_COLAN + NEW_LINE;
233 }
234
235 /**
236 * Returns import string for AbstractEvent class.
237 *
238 * @return import string for AbstractEvent class
239 */
240 public String getAbstractEventsImport() {
241 return IMPORT + ONOS_EVENT_PKG + PERIOD + ABSTRACT_EVENT + SEMI_COLAN + NEW_LINE;
242 }
243
244 /**
245 * Returns import string for EventListener class.
246 *
247 * @return import string for EventListener class
248 */
249 public String getEventListenerImport() {
250 return IMPORT + ONOS_EVENT_PKG + PERIOD + EVENT_LISTENER + SEMI_COLAN + NEW_LINE;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530251 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530252}