blob: 7661172987f2a85e8a35978a9084a02c955ee8e8 [file] [log] [blame]
Vinod Kumar S38046502016-03-23 15:30:27 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S38046502016-03-23 15:30:27 +05303 *
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 */
16package org.onosproject.yangutils.translator.tojava;
17
Bharat saraswale2d51d62016-03-23 19:40:35 +053018import java.util.ArrayList;
19import java.util.List;
Vinod Kumar S38046502016-03-23 15:30:27 +053020import java.util.SortedSet;
21import java.util.TreeSet;
Vinod Kumar S38046502016-03-23 15:30:27 +053022
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053023import static java.util.Collections.sort;
24
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053025import static org.onosproject.yangutils.utils.UtilConstants.ARRAY_LIST;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053026import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTATION_HOLDER_CLASS_IMPORT_CLASS;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053027import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO_CLASS_IMPORT_CLASS;
28import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO_CLASS_IMPORT_PKG;
Bharat saraswale2d51d62016-03-23 19:40:35 +053029import static org.onosproject.yangutils.utils.UtilConstants.COLLECTION_IMPORTS;
30import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
31import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_CLASS;
32import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_PKG;
33import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
34import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG;
35import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_CLASS;
36import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_PKG;
37import static org.onosproject.yangutils.utils.UtilConstants.LIST;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053038import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_PKG;
39import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_REG;
40import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_SERVICE;
Bharat saraswale2d51d62016-03-23 19:40:35 +053041import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
42import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053043import static org.onosproject.yangutils.utils.UtilConstants.PROVIDED_AUGMENTATION_CLASS_IMPORT_PKG;
Bharat saraswale2d51d62016-03-23 19:40:35 +053044import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
45
Vinod Kumar S38046502016-03-23 15:30:27 +053046/**
Bharat saraswald9822e92016-04-05 15:13:44 +053047 * Represents that generated Java file can contain imports.
Vinod Kumar S38046502016-03-23 15:30:27 +053048 */
49public 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 saraswald9822e92016-04-05 15:13:44 +053063 * Creates java import data object.
Vinod Kumar S38046502016-03-23 15:30:27 +053064 */
65 public JavaImportData() {
66 setImportSet(new TreeSet<JavaQualifiedTypeInfo>());
67 }
68
69 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053070 * Returns if the list needs to be imported.
Vinod Kumar S38046502016-03-23 15:30:27 +053071 *
Vidyashree Rama74453712016-04-18 12:29:39 +053072 * @return true if any of the attribute needs to be maintained as a list
Vinod Kumar S38046502016-03-23 15:30:27 +053073 */
74 public boolean getIfListImported() {
75 return isListToImport;
76 }
77
78 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053079 * Sets the status of importing list.
Vinod Kumar S38046502016-03-23 15:30:27 +053080 *
Vidyashree Rama74453712016-04-18 12:29:39 +053081 * @param isList status to mention list is bing imported
Vinod Kumar S38046502016-03-23 15:30:27 +053082 */
83 public void setIfListImported(boolean isList) {
84 isListToImport = isList;
85 }
86
87 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053088 * Returns the set containing the imported class/interface info.
Vinod Kumar S38046502016-03-23 15:30:27 +053089 *
90 * @return the set containing the imported class/interface info
91 */
92 public SortedSet<JavaQualifiedTypeInfo> getImportSet() {
93 return importSet;
94 }
95
96 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053097 * Assigns the set containing the imported class/interface info.
Vinod Kumar S38046502016-03-23 15:30:27 +053098 *
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 saraswalcc1cdab2016-04-16 02:28:25 +0530106 * Adds an imported class/interface info if it is not already part of the
Vinod Kumar S38046502016-03-23 15:30:27 +0530107 * 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 S38046502016-03-23 15:30:27 +0530116 * @param newImportInfo class/interface info being imported
117 * @return status of new addition of class/interface to the import set
118 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530119 public boolean addImportInfo(JavaQualifiedTypeInfo newImportInfo) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530120
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530121 for (JavaQualifiedTypeInfo curImportInfo : getImportSet()) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530122 if (curImportInfo.getClassInfo()
123 .contentEquals(newImportInfo.getClassInfo())) {
124 return curImportInfo.getPkgInfo()
125 .contentEquals(newImportInfo.getPkgInfo());
126 }
127 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530128
129 getImportSet().add(newImportInfo);
Vinod Kumar S38046502016-03-23 15:30:27 +0530130 return true;
131 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530132
133 /**
134 * Returns import for class.
135 *
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530136 * @return imports for class
137 */
138 public List<String> getImports() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530139
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530140 String importString;
141 List<String> imports = new ArrayList<>();
142
143 for (JavaQualifiedTypeInfo importInfo : getImportSet()) {
144 if (!importInfo.getPkgInfo().equals(EMPTY_STRING) && importInfo.getClassInfo() != null
145 && !importInfo.getPkgInfo().equals(JAVA_LANG)) {
146 importString = IMPORT + importInfo.getPkgInfo() + PERIOD + importInfo.getClassInfo() + SEMI_COLAN
147 + NEW_LINE;
148
149 imports.add(importString);
150 }
151 }
152
Bharat saraswalc0e04842016-05-12 13:16:57 +0530153 if (getIfListImported()) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530154 imports.add(getImportForList());
155 }
156
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530157 sort(imports);
158 return imports;
159 }
160
161 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530162 * Returns import for hash and equals method.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530163 *
164 * @return import for hash and equals method
165 */
166 public String getImportForHashAndEquals() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530167 return IMPORT + JAVA_UTIL_OBJECTS_IMPORT_PKG + PERIOD + JAVA_UTIL_OBJECTS_IMPORT_CLASS;
168 }
169
170 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530171 * Returns import for to string method.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530172 *
173 * @return import for to string method
174 */
175 public String getImportForToString() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530176 return IMPORT + GOOGLE_MORE_OBJECT_IMPORT_PKG + PERIOD + GOOGLE_MORE_OBJECT_IMPORT_CLASS;
177 }
178
179 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530180 * Returns import for list attribute.
181 *
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530182 * @return import for list attribute
Bharat saraswale2d51d62016-03-23 19:40:35 +0530183 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530184 public String getImportForList() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530185 return IMPORT + COLLECTION_IMPORTS + PERIOD + LIST + SEMI_COLAN + NEW_LINE;
186 }
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530187
188 /**
189 * Returns import for array list attribute.
190 *
191 * @return import for array list attribute
192 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530193 public String getImportForArrayList() {
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530194 return IMPORT + COLLECTION_IMPORTS + PERIOD + ARRAY_LIST + SEMI_COLAN + NEW_LINE;
195 }
196
197 /**
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530198 * Returns import string for AugmentationHolder class.
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530199 *
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530200 * @return import string for AugmentationHolder class
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530201 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530202 public String getAugmentationHolderImport() {
203 return IMPORT + PROVIDED_AUGMENTATION_CLASS_IMPORT_PKG + PERIOD + AUGMENTATION_HOLDER_CLASS_IMPORT_CLASS;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530204 }
205
206 /**
207 * Returns import string for AugmentedInfo class.
208 *
209 * @return import string for AugmentedInfo class
210 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530211 public String getAugmentedInfoImport() {
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530212 return IMPORT + AUGMENTED_INFO_CLASS_IMPORT_PKG + PERIOD + AUGMENTED_INFO_CLASS_IMPORT_CLASS;
213 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530214
215 /**
216 * Returns import string for ListenerService class.
217 *
218 * @return import string for ListenerService class
219 */
220 public String getListenerServiceImport() {
221 return IMPORT + LISTENER_PKG + PERIOD + LISTENER_SERVICE + SEMI_COLAN + NEW_LINE;
222 }
223
224 /**
225 * Returns import string for ListenerRegistry class.
226 *
227 * @return import string for ListenerRegistry class
228 */
229 public String getListenerRegistryImport() {
230 return IMPORT + LISTENER_PKG + PERIOD + LISTENER_REG + SEMI_COLAN + NEW_LINE;
231 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530232}