blob: 33896596df7eed5494c4d743cd4a7815c2916e79 [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 saraswal33dfa012016-05-17 19:59:16 +053023import static org.onosproject.yangutils.utils.UtilConstants.ABSTRACT_EVENT;
Bharat saraswale2d51d62016-03-23 19:40:35 +053024import static org.onosproject.yangutils.utils.UtilConstants.COLLECTION_IMPORTS;
25import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
Bharat saraswal33dfa012016-05-17 19:59:16 +053026import static org.onosproject.yangutils.utils.UtilConstants.EVENT_LISTENER;
Bharat saraswale2d51d62016-03-23 19:40:35 +053027import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_CLASS;
28import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_PKG;
29import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
30import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG;
31import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_CLASS;
32import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_PKG;
33import static org.onosproject.yangutils.utils.UtilConstants.LIST;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053034import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_REG;
35import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_SERVICE;
Bharat saraswale2d51d62016-03-23 19:40:35 +053036import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswal33dfa012016-05-17 19:59:16 +053037import static org.onosproject.yangutils.utils.UtilConstants.ONOS_EVENT_PKG;
Bharat saraswale2d51d62016-03-23 19:40:35 +053038import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
39import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053040import static java.util.Collections.sort;
41
Vinod Kumar S38046502016-03-23 15:30:27 +053042/**
Bharat saraswald9822e92016-04-05 15:13:44 +053043 * Represents that generated Java file can contain imports.
Vinod Kumar S38046502016-03-23 15:30:27 +053044 */
45public 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 saraswald9822e92016-04-05 15:13:44 +053059 * Creates java import data object.
Vinod Kumar S38046502016-03-23 15:30:27 +053060 */
61 public JavaImportData() {
62 setImportSet(new TreeSet<JavaQualifiedTypeInfo>());
63 }
64
65 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053066 * Returns if the list needs to be imported.
Vinod Kumar S38046502016-03-23 15:30:27 +053067 *
Vidyashree Rama74453712016-04-18 12:29:39 +053068 * @return true if any of the attribute needs to be maintained as a list
Vinod Kumar S38046502016-03-23 15:30:27 +053069 */
70 public boolean getIfListImported() {
71 return isListToImport;
72 }
73
74 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053075 * Sets the status of importing list.
Vinod Kumar S38046502016-03-23 15:30:27 +053076 *
Vidyashree Rama74453712016-04-18 12:29:39 +053077 * @param isList status to mention list is bing imported
Vinod Kumar S38046502016-03-23 15:30:27 +053078 */
79 public void setIfListImported(boolean isList) {
80 isListToImport = isList;
81 }
82
83 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053084 * Returns the set containing the imported class/interface info.
Vinod Kumar S38046502016-03-23 15:30:27 +053085 *
86 * @return the set containing the imported class/interface info
87 */
88 public SortedSet<JavaQualifiedTypeInfo> getImportSet() {
89 return importSet;
90 }
91
92 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053093 * Assigns the set containing the imported class/interface info.
Vinod Kumar S38046502016-03-23 15:30:27 +053094 *
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 saraswalcc1cdab2016-04-16 02:28:25 +0530102 * Adds an imported class/interface info if it is not already part of the
Vinod Kumar S38046502016-03-23 15:30:27 +0530103 * 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 S38046502016-03-23 15:30:27 +0530112 * @param newImportInfo class/interface info being imported
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530113 * @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 S38046502016-03-23 15:30:27 +0530116 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530117 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 saraswale2d51d62016-03-23 19:40:35 +0530145
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530146 for (JavaQualifiedTypeInfo curImportInfo : getImportSet()) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530147 if (curImportInfo.getClassInfo()
148 .contentEquals(newImportInfo.getClassInfo())) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530149 return !curImportInfo.getPkgInfo()
Vinod Kumar S38046502016-03-23 15:30:27 +0530150 .contentEquals(newImportInfo.getPkgInfo());
151 }
152 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530153
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530154 /*
155 * import is added, so it is a member for non qualified access
156 */
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530157 getImportSet().add(newImportInfo);
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530158 return false;
Vinod Kumar S38046502016-03-23 15:30:27 +0530159 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530160
161 /**
162 * Returns import for class.
163 *
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530164 * @return imports for class
165 */
166 public List<String> getImports() {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530167
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530168 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 saraswalc0e04842016-05-12 13:16:57 +0530181 if (getIfListImported()) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530182 imports.add(getImportForList());
183 }
184
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530185 sort(imports);
186 return imports;
187 }
188
189 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530190 * Returns import for hash and equals method.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530191 *
192 * @return import for hash and equals method
193 */
194 public String getImportForHashAndEquals() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530195 return IMPORT + JAVA_UTIL_OBJECTS_IMPORT_PKG + PERIOD + JAVA_UTIL_OBJECTS_IMPORT_CLASS;
196 }
197
198 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530199 * Returns import for to string method.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530200 *
201 * @return import for to string method
202 */
203 public String getImportForToString() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530204 return IMPORT + GOOGLE_MORE_OBJECT_IMPORT_PKG + PERIOD + GOOGLE_MORE_OBJECT_IMPORT_CLASS;
205 }
206
207 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530208 * Returns import for list attribute.
209 *
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530210 * @return import for list attribute
Bharat saraswale2d51d62016-03-23 19:40:35 +0530211 */
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530212 public String getImportForList() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530213 return IMPORT + COLLECTION_IMPORTS + PERIOD + LIST + SEMI_COLAN + NEW_LINE;
214 }
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530215
216 /**
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530217 * Returns import string for ListenerService class.
218 *
219 * @return import string for ListenerService class
220 */
221 public String getListenerServiceImport() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530222 return IMPORT + ONOS_EVENT_PKG + PERIOD + LISTENER_SERVICE + SEMI_COLAN + NEW_LINE;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530223 }
224
225 /**
226 * Returns import string for ListenerRegistry class.
227 *
228 * @return import string for ListenerRegistry class
229 */
230 public String getListenerRegistryImport() {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530231 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 saraswalab4c6ba2016-05-17 14:19:38 +0530250 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530251}