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 | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 24 | import static org.onosproject.yangutils.utils.UtilConstants.ARRAY_LIST; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 25 | import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTATION_HOLDER_CLASS_IMPORT_CLASS; |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 26 | import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO_CLASS_IMPORT_CLASS; |
| 27 | import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO_CLASS_IMPORT_PKG; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 28 | import static org.onosproject.yangutils.utils.UtilConstants.COLLECTION_IMPORTS; |
| 29 | import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING; |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 30 | import static org.onosproject.yangutils.utils.UtilConstants.EVENT_LISTENER; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 31 | import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_CLASS; |
| 32 | import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_PKG; |
| 33 | import static org.onosproject.yangutils.utils.UtilConstants.IMPORT; |
| 34 | import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG; |
| 35 | import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_CLASS; |
| 36 | import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_PKG; |
| 37 | import static org.onosproject.yangutils.utils.UtilConstants.LIST; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 38 | import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_REG; |
| 39 | import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_SERVICE; |
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; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 43 | import static org.onosproject.yangutils.utils.UtilConstants.PROVIDED_AUGMENTATION_CLASS_IMPORT_PKG; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 44 | import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN; |
| 45 | |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame^] | 46 | import static java.util.Collections.sort; |
| 47 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 48 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 49 | * Represents that generated Java file can contain imports. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 50 | */ |
| 51 | public 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 saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 65 | * Creates java import data object. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 66 | */ |
| 67 | public JavaImportData() { |
| 68 | setImportSet(new TreeSet<JavaQualifiedTypeInfo>()); |
| 69 | } |
| 70 | |
| 71 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 72 | * Returns if the list needs to be imported. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 73 | * |
Vidyashree Rama | 02f115f | 2016-04-18 12:29:39 +0530 | [diff] [blame] | 74 | * @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] | 75 | */ |
| 76 | public boolean getIfListImported() { |
| 77 | return isListToImport; |
| 78 | } |
| 79 | |
| 80 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 81 | * Sets the status of importing list. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 82 | * |
Vidyashree Rama | 02f115f | 2016-04-18 12:29:39 +0530 | [diff] [blame] | 83 | * @param isList status to mention list is bing imported |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 84 | */ |
| 85 | public void setIfListImported(boolean isList) { |
| 86 | isListToImport = isList; |
| 87 | } |
| 88 | |
| 89 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 90 | * Returns the set containing the imported class/interface info. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 91 | * |
| 92 | * @return the set containing the imported class/interface info |
| 93 | */ |
| 94 | public SortedSet<JavaQualifiedTypeInfo> getImportSet() { |
| 95 | return importSet; |
| 96 | } |
| 97 | |
| 98 | /** |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 99 | * Assigns the set containing the imported class/interface info. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 100 | * |
| 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 saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 108 | * 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] | 109 | * 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 S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 118 | * @param newImportInfo class/interface info being imported |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame^] | 119 | * @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 S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 122 | */ |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame^] | 123 | 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 saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 151 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 152 | for (JavaQualifiedTypeInfo curImportInfo : getImportSet()) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 153 | if (curImportInfo.getClassInfo() |
| 154 | .contentEquals(newImportInfo.getClassInfo())) { |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame^] | 155 | return !curImportInfo.getPkgInfo() |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 156 | .contentEquals(newImportInfo.getPkgInfo()); |
| 157 | } |
| 158 | } |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 159 | |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame^] | 160 | /* |
| 161 | * import is added, so it is a member for non qualified access |
| 162 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 163 | getImportSet().add(newImportInfo); |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame^] | 164 | return false; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 165 | } |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 166 | |
| 167 | /** |
| 168 | * Returns import for class. |
| 169 | * |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 170 | * @return imports for class |
| 171 | */ |
| 172 | public List<String> getImports() { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 173 | |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 174 | 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 saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 187 | if (getIfListImported()) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 188 | imports.add(getImportForList()); |
| 189 | } |
| 190 | |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 191 | sort(imports); |
| 192 | return imports; |
| 193 | } |
| 194 | |
| 195 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 196 | * Returns import for hash and equals method. |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 197 | * |
| 198 | * @return import for hash and equals method |
| 199 | */ |
| 200 | public String getImportForHashAndEquals() { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 201 | return IMPORT + JAVA_UTIL_OBJECTS_IMPORT_PKG + PERIOD + JAVA_UTIL_OBJECTS_IMPORT_CLASS; |
| 202 | } |
| 203 | |
| 204 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 205 | * Returns import for to string method. |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 206 | * |
| 207 | * @return import for to string method |
| 208 | */ |
| 209 | public String getImportForToString() { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 210 | return IMPORT + GOOGLE_MORE_OBJECT_IMPORT_PKG + PERIOD + GOOGLE_MORE_OBJECT_IMPORT_CLASS; |
| 211 | } |
| 212 | |
| 213 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 214 | * Returns import for list attribute. |
| 215 | * |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 216 | * @return import for list attribute |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 217 | */ |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 218 | public String getImportForList() { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 219 | return IMPORT + COLLECTION_IMPORTS + PERIOD + LIST + SEMI_COLAN + NEW_LINE; |
| 220 | } |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 221 | |
| 222 | /** |
| 223 | * Returns import for array list attribute. |
| 224 | * |
| 225 | * @return import for array list attribute |
| 226 | */ |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 227 | public String getImportForArrayList() { |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 228 | return IMPORT + COLLECTION_IMPORTS + PERIOD + ARRAY_LIST + SEMI_COLAN + NEW_LINE; |
| 229 | } |
| 230 | |
| 231 | /** |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 232 | * Returns import string for AugmentationHolder class. |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 233 | * |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 234 | * @return import string for AugmentationHolder class |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 235 | */ |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 236 | public String getAugmentationHolderImport() { |
| 237 | return IMPORT + PROVIDED_AUGMENTATION_CLASS_IMPORT_PKG + PERIOD + AUGMENTATION_HOLDER_CLASS_IMPORT_CLASS; |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Returns import string for AugmentedInfo class. |
| 242 | * |
| 243 | * @return import string for AugmentedInfo class |
| 244 | */ |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 245 | public String getAugmentedInfoImport() { |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 246 | return IMPORT + AUGMENTED_INFO_CLASS_IMPORT_PKG + PERIOD + AUGMENTED_INFO_CLASS_IMPORT_CLASS; |
| 247 | } |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 248 | |
| 249 | /** |
| 250 | * Returns import string for ListenerService class. |
| 251 | * |
| 252 | * @return import string for ListenerService class |
| 253 | */ |
| 254 | public String getListenerServiceImport() { |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 255 | return IMPORT + ONOS_EVENT_PKG + PERIOD + LISTENER_SERVICE + SEMI_COLAN + NEW_LINE; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Returns import string for ListenerRegistry class. |
| 260 | * |
| 261 | * @return import string for ListenerRegistry class |
| 262 | */ |
| 263 | public String getListenerRegistryImport() { |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 264 | 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 saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 283 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 284 | } |