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 | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 24 | import static org.onosproject.yangutils.utils.UtilConstants.COLLECTION_IMPORTS; |
| 25 | import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING; |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 26 | import static org.onosproject.yangutils.utils.UtilConstants.EVENT_LISTENER; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 27 | import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_CLASS; |
| 28 | import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_PKG; |
| 29 | import static org.onosproject.yangutils.utils.UtilConstants.IMPORT; |
| 30 | import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG; |
| 31 | import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_CLASS; |
| 32 | import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_PKG; |
| 33 | import static org.onosproject.yangutils.utils.UtilConstants.LIST; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 34 | import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_REG; |
| 35 | import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_SERVICE; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 36 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 37 | import static org.onosproject.yangutils.utils.UtilConstants.ONOS_EVENT_PKG; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 38 | import static org.onosproject.yangutils.utils.UtilConstants.PERIOD; |
| 39 | import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN; |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 40 | import static java.util.Collections.sort; |
| 41 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 42 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 43 | * Represents that generated Java file can contain imports. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 44 | */ |
| 45 | public class JavaImportData { |
| 46 | |
| 47 | /** |
| 48 | * Flag to denote if any list in imported. |
| 49 | */ |
| 50 | private boolean isListToImport; |
| 51 | |
| 52 | /** |
| 53 | * Sorted set of import info, to be used to maintain the set of classes to |
| 54 | * be imported in the generated class. |
| 55 | */ |
| 56 | private SortedSet<JavaQualifiedTypeInfo> importSet; |
| 57 | |
| 58 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 59 | * Creates java import data object. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 60 | */ |
| 61 | public JavaImportData() { |
| 62 | setImportSet(new TreeSet<JavaQualifiedTypeInfo>()); |
| 63 | } |
| 64 | |
| 65 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 66 | * Returns if the list needs to be imported. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 67 | * |
Vidyashree Rama | 02f115f | 2016-04-18 12:29:39 +0530 | [diff] [blame] | 68 | * @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] | 69 | */ |
| 70 | public boolean getIfListImported() { |
| 71 | return isListToImport; |
| 72 | } |
| 73 | |
| 74 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 75 | * Sets the status of importing list. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 76 | * |
Vidyashree Rama | 02f115f | 2016-04-18 12:29:39 +0530 | [diff] [blame] | 77 | * @param isList status to mention list is bing imported |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 78 | */ |
| 79 | public void setIfListImported(boolean isList) { |
| 80 | isListToImport = isList; |
| 81 | } |
| 82 | |
| 83 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 84 | * Returns the set containing the imported class/interface info. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 85 | * |
| 86 | * @return the set containing the imported class/interface info |
| 87 | */ |
| 88 | public SortedSet<JavaQualifiedTypeInfo> getImportSet() { |
| 89 | return importSet; |
| 90 | } |
| 91 | |
| 92 | /** |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 93 | * Assigns the set containing the imported class/interface info. |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 94 | * |
| 95 | * @param importSet the set containing the imported class/interface info |
| 96 | */ |
| 97 | private void setImportSet(SortedSet<JavaQualifiedTypeInfo> importSet) { |
| 98 | this.importSet = importSet; |
| 99 | } |
| 100 | |
| 101 | /** |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 102 | * 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] | 103 | * collection. |
| 104 | * |
| 105 | * If already part of the collection, check if the packages are same, if so |
| 106 | * then return true, to denote it is already in the import collection, and |
| 107 | * it can be accessed without qualified access. If the packages do not |
| 108 | * match, then do not add to the import collection, and return false to |
| 109 | * denote, it is not added to import collection and needs to be accessed in |
| 110 | * a qualified manner. |
| 111 | * |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 112 | * @param newImportInfo class/interface info being imported |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 113 | * @param className name of the call being generated |
| 114 | * @param classPkg generated class package |
| 115 | * @return qualified access status of the import node being added |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 116 | */ |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 117 | public boolean addImportInfo(JavaQualifiedTypeInfo newImportInfo, |
| 118 | String className, String classPkg) { |
| 119 | |
| 120 | if (newImportInfo.getClassInfo().contentEquals(className)) { |
| 121 | /* |
| 122 | * if the current class name is same as the attribute class name, |
| 123 | * then the attribute must be accessed in a qualified manner. |
| 124 | */ |
| 125 | return true; |
| 126 | } else if (newImportInfo.getPkgInfo() == null) { |
| 127 | /* |
| 128 | * If the package info is null, then it is not a candidate for import / qualified access |
| 129 | */ |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * If the attribute type is having the package info, it is contender |
| 135 | * for import list and also need to check if it needs to be a |
| 136 | * qualified access. |
| 137 | */ |
| 138 | if (newImportInfo.getPkgInfo().contentEquals(classPkg)) { |
| 139 | /** |
| 140 | * Package of the referred attribute and the generated class is same, so no need import |
| 141 | * or qualified access. |
| 142 | */ |
| 143 | return false; |
| 144 | } |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 145 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 146 | for (JavaQualifiedTypeInfo curImportInfo : getImportSet()) { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 147 | if (curImportInfo.getClassInfo() |
| 148 | .contentEquals(newImportInfo.getClassInfo())) { |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 149 | return !curImportInfo.getPkgInfo() |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 150 | .contentEquals(newImportInfo.getPkgInfo()); |
| 151 | } |
| 152 | } |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 153 | |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 154 | /* |
| 155 | * import is added, so it is a member for non qualified access |
| 156 | */ |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 157 | getImportSet().add(newImportInfo); |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 158 | return false; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 159 | } |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 160 | |
| 161 | /** |
| 162 | * Returns import for class. |
| 163 | * |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 164 | * @return imports for class |
| 165 | */ |
| 166 | public List<String> getImports() { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 167 | |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 168 | String importString; |
| 169 | List<String> imports = new ArrayList<>(); |
| 170 | |
| 171 | for (JavaQualifiedTypeInfo importInfo : getImportSet()) { |
| 172 | if (!importInfo.getPkgInfo().equals(EMPTY_STRING) && importInfo.getClassInfo() != null |
| 173 | && !importInfo.getPkgInfo().equals(JAVA_LANG)) { |
| 174 | importString = IMPORT + importInfo.getPkgInfo() + PERIOD + importInfo.getClassInfo() + SEMI_COLAN |
| 175 | + NEW_LINE; |
| 176 | |
| 177 | imports.add(importString); |
| 178 | } |
| 179 | } |
| 180 | |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 181 | if (getIfListImported()) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 182 | imports.add(getImportForList()); |
| 183 | } |
| 184 | |
Vidyashree Rama | 1396065 | 2016-04-26 15:06:06 +0530 | [diff] [blame] | 185 | sort(imports); |
| 186 | return imports; |
| 187 | } |
| 188 | |
| 189 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 190 | * Returns import for hash and equals method. |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 191 | * |
| 192 | * @return import for hash and equals method |
| 193 | */ |
| 194 | public String getImportForHashAndEquals() { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 195 | return IMPORT + JAVA_UTIL_OBJECTS_IMPORT_PKG + PERIOD + JAVA_UTIL_OBJECTS_IMPORT_CLASS; |
| 196 | } |
| 197 | |
| 198 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 199 | * Returns import for to string method. |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 200 | * |
| 201 | * @return import for to string method |
| 202 | */ |
| 203 | public String getImportForToString() { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 204 | return IMPORT + GOOGLE_MORE_OBJECT_IMPORT_PKG + PERIOD + GOOGLE_MORE_OBJECT_IMPORT_CLASS; |
| 205 | } |
| 206 | |
| 207 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 208 | * Returns import for list attribute. |
| 209 | * |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 210 | * @return import for list attribute |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 211 | */ |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 212 | public String getImportForList() { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 213 | return IMPORT + COLLECTION_IMPORTS + PERIOD + LIST + SEMI_COLAN + NEW_LINE; |
| 214 | } |
Bharat saraswal | e2bc60d | 2016-04-16 02:28:25 +0530 | [diff] [blame] | 215 | |
| 216 | /** |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 217 | * Returns import string for ListenerService class. |
| 218 | * |
| 219 | * @return import string for ListenerService class |
| 220 | */ |
| 221 | public String getListenerServiceImport() { |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 222 | return IMPORT + ONOS_EVENT_PKG + PERIOD + LISTENER_SERVICE + SEMI_COLAN + NEW_LINE; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Returns import string for ListenerRegistry class. |
| 227 | * |
| 228 | * @return import string for ListenerRegistry class |
| 229 | */ |
| 230 | public String getListenerRegistryImport() { |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 231 | return IMPORT + ONOS_EVENT_PKG + PERIOD + LISTENER_REG + SEMI_COLAN + NEW_LINE; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Returns import string for AbstractEvent class. |
| 236 | * |
| 237 | * @return import string for AbstractEvent class |
| 238 | */ |
| 239 | public String getAbstractEventsImport() { |
| 240 | return IMPORT + ONOS_EVENT_PKG + PERIOD + ABSTRACT_EVENT + SEMI_COLAN + NEW_LINE; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Returns import string for EventListener class. |
| 245 | * |
| 246 | * @return import string for EventListener class |
| 247 | */ |
| 248 | public String getEventListenerImport() { |
| 249 | return IMPORT + ONOS_EVENT_PKG + PERIOD + EVENT_LISTENER + SEMI_COLAN + NEW_LINE; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 250 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 251 | } |