Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.yangutils.translator.tojava; |
| 17 | |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 18 | import java.util.ArrayList; |
| 19 | import java.util.List; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 20 | import java.util.SortedSet; |
| 21 | import java.util.TreeSet; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 22 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 23 | import static org.onosproject.yangutils.utils.UtilConstants.ABSTRACT_EVENT; |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame^] | 24 | import static org.onosproject.yangutils.utils.UtilConstants.YANG_AUGMENTED_INFO_CLASS_IMPORT_CLASS; |
| 25 | import static org.onosproject.yangutils.utils.UtilConstants.YANG_AUGMENTED_INFO_CLASS_IMPORT_PKG; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 26 | import static org.onosproject.yangutils.utils.UtilConstants.COLLECTION_IMPORTS; |
| 27 | import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING; |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 28 | import static org.onosproject.yangutils.utils.UtilConstants.EVENT_LISTENER; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 29 | import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_CLASS; |
| 30 | import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_PKG; |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame^] | 31 | import static org.onosproject.yangutils.utils.UtilConstants.HASH_MAP; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 32 | import static org.onosproject.yangutils.utils.UtilConstants.IMPORT; |
| 33 | import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG; |
| 34 | import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_CLASS; |
| 35 | import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_PKG; |
| 36 | import static org.onosproject.yangutils.utils.UtilConstants.LIST; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 37 | import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_REG; |
| 38 | import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_SERVICE; |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame^] | 39 | import static org.onosproject.yangutils.utils.UtilConstants.MAP; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 40 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 41 | import static org.onosproject.yangutils.utils.UtilConstants.ONOS_EVENT_PKG; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 42 | import static org.onosproject.yangutils.utils.UtilConstants.PERIOD; |
| 43 | import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN; |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 44 | import static java.util.Collections.sort; |
| 45 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 46 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 47 | * Represents that generated Java file can contain imports. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 48 | */ |
| 49 | public 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 saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 63 | * Creates java import data object. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 64 | */ |
| 65 | public JavaImportData() { |
| 66 | setImportSet(new TreeSet<JavaQualifiedTypeInfo>()); |
| 67 | } |
| 68 | |
| 69 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 70 | * Returns if the list needs to be imported. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 71 | * |
Vidyashree Rama | 02f115f | 2016-04-18 12:29:39 +0530 | [diff] [blame] | 72 | * @return true if any of the attribute needs to be maintained as a list |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 73 | */ |
| 74 | public boolean getIfListImported() { |
| 75 | return isListToImport; |
| 76 | } |
| 77 | |
| 78 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 79 | * Sets the status of importing list. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 80 | * |
Vidyashree Rama | 02f115f | 2016-04-18 12:29:39 +0530 | [diff] [blame] | 81 | * @param isList status to mention list is bing imported |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 82 | */ |
| 83 | public void setIfListImported(boolean isList) { |
| 84 | isListToImport = isList; |
| 85 | } |
| 86 | |
| 87 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 88 | * Returns the set containing the imported class/interface info. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 89 | * |
| 90 | * @return the set containing the imported class/interface info |
| 91 | */ |
| 92 | public SortedSet<JavaQualifiedTypeInfo> getImportSet() { |
| 93 | return importSet; |
| 94 | } |
| 95 | |
| 96 | /** |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 97 | * Assigns the set containing the imported class/interface info. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 98 | * |
| 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 saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 106 | * Adds an imported class/interface info if it is not already part of the |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 107 | * 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 S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 116 | * @param newImportInfo class/interface info being imported |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 117 | * @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 S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 120 | */ |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 121 | 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 saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 149 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 150 | for (JavaQualifiedTypeInfo curImportInfo : getImportSet()) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 151 | if (curImportInfo.getClassInfo() |
| 152 | .contentEquals(newImportInfo.getClassInfo())) { |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 153 | return !curImportInfo.getPkgInfo() |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 154 | .contentEquals(newImportInfo.getPkgInfo()); |
| 155 | } |
| 156 | } |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 157 | |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 158 | /* |
| 159 | * import is added, so it is a member for non qualified access |
| 160 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 161 | getImportSet().add(newImportInfo); |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 162 | return false; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 163 | } |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 164 | |
| 165 | /** |
| 166 | * Returns import for class. |
| 167 | * |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 168 | * @return imports for class |
| 169 | */ |
| 170 | public List<String> getImports() { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 171 | |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 172 | 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 saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 185 | if (getIfListImported()) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 186 | imports.add(getImportForList()); |
| 187 | } |
| 188 | |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 189 | sort(imports); |
| 190 | return imports; |
| 191 | } |
| 192 | |
| 193 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 194 | * Returns import for hash and equals method. |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 195 | * |
| 196 | * @return import for hash and equals method |
| 197 | */ |
| 198 | public String getImportForHashAndEquals() { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 199 | return IMPORT + JAVA_UTIL_OBJECTS_IMPORT_PKG + PERIOD + JAVA_UTIL_OBJECTS_IMPORT_CLASS; |
| 200 | } |
| 201 | |
| 202 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 203 | * Returns import for to string method. |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 204 | * |
| 205 | * @return import for to string method |
| 206 | */ |
| 207 | public String getImportForToString() { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 208 | return IMPORT + GOOGLE_MORE_OBJECT_IMPORT_PKG + PERIOD + GOOGLE_MORE_OBJECT_IMPORT_CLASS; |
| 209 | } |
| 210 | |
| 211 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 212 | * Returns import for list attribute. |
| 213 | * |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 214 | * @return import for list attribute |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 215 | */ |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 216 | public String getImportForList() { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 217 | return IMPORT + COLLECTION_IMPORTS + PERIOD + LIST + SEMI_COLAN + NEW_LINE; |
| 218 | } |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 219 | |
| 220 | /** |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 221 | * Returns import string for ListenerService class. |
| 222 | * |
| 223 | * @return import string for ListenerService class |
| 224 | */ |
| 225 | public String getListenerServiceImport() { |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 226 | return IMPORT + ONOS_EVENT_PKG + PERIOD + LISTENER_SERVICE + SEMI_COLAN + NEW_LINE; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Returns import string for ListenerRegistry class. |
| 231 | * |
| 232 | * @return import string for ListenerRegistry class |
| 233 | */ |
| 234 | public String getListenerRegistryImport() { |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 235 | 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 saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 254 | } |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame^] | 255 | |
| 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 S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 282 | } |