Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 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; |
| 22 | |
| 23 | import org.onosproject.yangutils.datamodel.YangNode; |
| 24 | |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 25 | import static org.onosproject.yangutils.utils.UtilConstants.COLLECTION_IMPORTS; |
| 26 | import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING; |
| 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; |
| 34 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
| 35 | import static org.onosproject.yangutils.utils.UtilConstants.PERIOD; |
| 36 | import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN; |
| 37 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 38 | /** |
| 39 | * Generated Java file can contain imports. |
| 40 | */ |
| 41 | public class JavaImportData { |
| 42 | |
| 43 | /** |
| 44 | * Flag to denote if any list in imported. |
| 45 | */ |
| 46 | private boolean isListToImport; |
| 47 | |
| 48 | /** |
| 49 | * Sorted set of import info, to be used to maintain the set of classes to |
| 50 | * be imported in the generated class. |
| 51 | */ |
| 52 | private SortedSet<JavaQualifiedTypeInfo> importSet; |
| 53 | |
| 54 | /** |
| 55 | * Default constructor. |
| 56 | */ |
| 57 | public JavaImportData() { |
| 58 | setImportSet(new TreeSet<JavaQualifiedTypeInfo>()); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Get if the list needs to be imported. |
| 63 | * |
| 64 | * @return true if any of the attribute needs to be maintained as a list. |
| 65 | */ |
| 66 | public boolean getIfListImported() { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 67 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 68 | return isListToImport; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Set the status of importing list. |
| 73 | * |
| 74 | * @param isList status to mention list is bing imported. |
| 75 | */ |
| 76 | public void setIfListImported(boolean isList) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 77 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 78 | isListToImport = isList; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Get the set containing the imported class/interface info. |
| 83 | * |
| 84 | * @return the set containing the imported class/interface info |
| 85 | */ |
| 86 | public SortedSet<JavaQualifiedTypeInfo> getImportSet() { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 87 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 88 | return importSet; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Assign the set containing the imported class/interface info. |
| 93 | * |
| 94 | * @param importSet the set containing the imported class/interface info |
| 95 | */ |
| 96 | private void setImportSet(SortedSet<JavaQualifiedTypeInfo> importSet) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 97 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 98 | this.importSet = importSet; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Add an imported class/interface info if it is not already part of the |
| 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 | * |
| 112 | * @param curNode current data model node |
| 113 | * @param newImportInfo class/interface info being imported |
| 114 | * @return status of new addition of class/interface to the import set |
| 115 | */ |
| 116 | public boolean addImportInfo(YangNode curNode, JavaQualifiedTypeInfo newImportInfo) { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 117 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 118 | if (!(curNode instanceof HasJavaImportData)) { |
| 119 | throw new RuntimeException("missing import info in data model node"); |
| 120 | } |
| 121 | for (JavaQualifiedTypeInfo curImportInfo : ((HasJavaImportData) curNode).getJavaImportData().getImportSet()) { |
| 122 | if (curImportInfo.getClassInfo() |
| 123 | .contentEquals(newImportInfo.getClassInfo())) { |
| 124 | return curImportInfo.getPkgInfo() |
| 125 | .contentEquals(newImportInfo.getPkgInfo()); |
| 126 | } |
| 127 | } |
| 128 | ((HasJavaImportData) curNode).getJavaImportData().getImportSet().add(newImportInfo); |
| 129 | return true; |
| 130 | } |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * Returns import for class. |
| 134 | * |
| 135 | * @param attr java attribute info |
| 136 | * @return imports for class |
| 137 | */ |
| 138 | public List<String> getImports(JavaAttributeInfo attr) { |
| 139 | |
| 140 | String importString; |
| 141 | List<String> imports = new ArrayList<>(); |
| 142 | |
| 143 | for (JavaQualifiedTypeInfo importInfo : getImportSet()) { |
| 144 | importString = IMPORT; |
| 145 | if (importInfo.getPkgInfo() != EMPTY_STRING && importInfo.getClassInfo() != null |
| 146 | && importInfo.getPkgInfo() != JAVA_LANG) { |
| 147 | importString = importString + importInfo.getPkgInfo() + PERIOD + importInfo.getClassInfo() + SEMI_COLAN |
| 148 | + NEW_LINE; |
| 149 | |
| 150 | imports.add(importString); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | if (attr.isListAttr()) { |
| 155 | imports.add(setImportForList()); |
| 156 | } |
| 157 | |
| 158 | java.util.Collections.sort(imports); |
| 159 | return imports; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Gets import for hash and equals method. |
| 164 | * |
| 165 | * @return import for hash and equals method |
| 166 | */ |
| 167 | public String getImportForHashAndEquals() { |
| 168 | |
| 169 | return IMPORT + JAVA_UTIL_OBJECTS_IMPORT_PKG + PERIOD + JAVA_UTIL_OBJECTS_IMPORT_CLASS; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Gets import for to string method. |
| 174 | * |
| 175 | * @return import for to string method |
| 176 | */ |
| 177 | public String getImportForToString() { |
| 178 | |
| 179 | return IMPORT + GOOGLE_MORE_OBJECT_IMPORT_PKG + PERIOD + GOOGLE_MORE_OBJECT_IMPORT_CLASS; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Sets import for to list. |
| 184 | */ |
| 185 | private static String setImportForList() { |
| 186 | |
| 187 | return IMPORT + COLLECTION_IMPORTS + PERIOD + LIST + SEMI_COLAN + NEW_LINE; |
| 188 | } |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 189 | } |