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