blob: 958201ba28a3b352b03403de2b96aa371b435a21 [file] [log] [blame]
Vinod Kumar S9f26ae52016-03-23 15:30:27 +05301/*
Brian O'Connor0f7908b2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vinod Kumar S9f26ae52016-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 saraswal84366c52016-03-23 19:40:35 +053018import java.util.ArrayList;
19import java.util.List;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053020import java.util.SortedSet;
21import java.util.TreeSet;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053022
Bharat saraswale2bc60d2016-04-16 02:28:25 +053023import static org.onosproject.yangutils.utils.UtilConstants.ARRAY_LIST;
24import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO_CLASS_IMPORT_CLASS;
25import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO_CLASS_IMPORT_PKG;
Bharat saraswal84366c52016-03-23 19:40:35 +053026import static org.onosproject.yangutils.utils.UtilConstants.COLLECTION_IMPORTS;
27import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
28import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_CLASS;
29import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_PKG;
Bharat saraswale2bc60d2016-04-16 02:28:25 +053030import static org.onosproject.yangutils.utils.UtilConstants.HAS_AUGMENTATION_CLASS_IMPORT_CLASS;
31import static org.onosproject.yangutils.utils.UtilConstants.HAS_AUGMENTATION_CLASS_IMPORT_PKG;
Bharat saraswal84366c52016-03-23 19:40:35 +053032import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
33import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG;
34import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_CLASS;
35import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_PKG;
36import static org.onosproject.yangutils.utils.UtilConstants.LIST;
37import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
38import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
39import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
40
Vinod Kumar S79a374b2016-04-30 21:09:15 +053041import static java.util.Collections.sort;
42
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053043/**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053044 * Represents that generated Java file can contain imports.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053045 */
46public class JavaImportData {
47
48 /**
49 * Flag to denote if any list in imported.
50 */
51 private boolean isListToImport;
52
53 /**
54 * Sorted set of import info, to be used to maintain the set of classes to
55 * be imported in the generated class.
56 */
57 private SortedSet<JavaQualifiedTypeInfo> importSet;
58
59 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053060 * Creates java import data object.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053061 */
62 public JavaImportData() {
63 setImportSet(new TreeSet<JavaQualifiedTypeInfo>());
64 }
65
66 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053067 * Returns if the list needs to be imported.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053068 *
Vidyashree Rama02f115f2016-04-18 12:29:39 +053069 * @return true if any of the attribute needs to be maintained as a list
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053070 */
71 public boolean getIfListImported() {
72 return isListToImport;
73 }
74
75 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053076 * Sets the status of importing list.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053077 *
Vidyashree Rama02f115f2016-04-18 12:29:39 +053078 * @param isList status to mention list is bing imported
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053079 */
80 public void setIfListImported(boolean isList) {
81 isListToImport = isList;
82 }
83
84 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053085 * Returns the set containing the imported class/interface info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053086 *
87 * @return the set containing the imported class/interface info
88 */
89 public SortedSet<JavaQualifiedTypeInfo> getImportSet() {
90 return importSet;
91 }
92
93 /**
Bharat saraswale2bc60d2016-04-16 02:28:25 +053094 * Assigns the set containing the imported class/interface info.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053095 *
96 * @param importSet the set containing the imported class/interface info
97 */
98 private void setImportSet(SortedSet<JavaQualifiedTypeInfo> importSet) {
99 this.importSet = importSet;
100 }
101
102 /**
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530103 * Adds an imported class/interface info if it is not already part of the
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530104 * collection.
105 *
106 * If already part of the collection, check if the packages are same, if so
107 * then return true, to denote it is already in the import collection, and
108 * it can be accessed without qualified access. If the packages do not
109 * match, then do not add to the import collection, and return false to
110 * denote, it is not added to import collection and needs to be accessed in
111 * a qualified manner.
112 *
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530113 * @param newImportInfo class/interface info being imported
114 * @return status of new addition of class/interface to the import set
115 */
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530116 public boolean addImportInfo(JavaQualifiedTypeInfo newImportInfo) {
Bharat saraswal84366c52016-03-23 19:40:35 +0530117
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530118 for (JavaQualifiedTypeInfo curImportInfo : getImportSet()) {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530119 if (curImportInfo.getClassInfo()
120 .contentEquals(newImportInfo.getClassInfo())) {
121 return curImportInfo.getPkgInfo()
122 .contentEquals(newImportInfo.getPkgInfo());
123 }
124 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530125
126 getImportSet().add(newImportInfo);
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530127 return true;
128 }
Bharat saraswal84366c52016-03-23 19:40:35 +0530129
130 /**
131 * Returns import for class.
132 *
Vidyashree Rama13960652016-04-26 15:06:06 +0530133 * @return imports for class
134 */
135 public List<String> getImports() {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530136
Vidyashree Rama13960652016-04-26 15:06:06 +0530137 String importString;
138 List<String> imports = new ArrayList<>();
139
140 for (JavaQualifiedTypeInfo importInfo : getImportSet()) {
141 if (!importInfo.getPkgInfo().equals(EMPTY_STRING) && importInfo.getClassInfo() != null
142 && !importInfo.getPkgInfo().equals(JAVA_LANG)) {
143 importString = IMPORT + importInfo.getPkgInfo() + PERIOD + importInfo.getClassInfo() + SEMI_COLAN
144 + NEW_LINE;
145
146 imports.add(importString);
147 }
148 }
149
Bharat saraswal250a7472016-05-12 13:16:57 +0530150 if (getIfListImported()) {
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530151 imports.add(getImportForList());
152 }
153
Vidyashree Rama13960652016-04-26 15:06:06 +0530154 sort(imports);
155 return imports;
156 }
157
158 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530159 * Returns import for hash and equals method.
Bharat saraswal84366c52016-03-23 19:40:35 +0530160 *
161 * @return import for hash and equals method
162 */
163 public String getImportForHashAndEquals() {
Bharat saraswal84366c52016-03-23 19:40:35 +0530164 return IMPORT + JAVA_UTIL_OBJECTS_IMPORT_PKG + PERIOD + JAVA_UTIL_OBJECTS_IMPORT_CLASS;
165 }
166
167 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530168 * Returns import for to string method.
Bharat saraswal84366c52016-03-23 19:40:35 +0530169 *
170 * @return import for to string method
171 */
172 public String getImportForToString() {
Bharat saraswal84366c52016-03-23 19:40:35 +0530173 return IMPORT + GOOGLE_MORE_OBJECT_IMPORT_PKG + PERIOD + GOOGLE_MORE_OBJECT_IMPORT_CLASS;
174 }
175
176 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530177 * Returns import for list attribute.
178 *
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530179 * @return import for list attribute
Bharat saraswal84366c52016-03-23 19:40:35 +0530180 */
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530181 public static String getImportForList() {
Bharat saraswal84366c52016-03-23 19:40:35 +0530182 return IMPORT + COLLECTION_IMPORTS + PERIOD + LIST + SEMI_COLAN + NEW_LINE;
183 }
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530184
185 /**
186 * Returns import for array list attribute.
187 *
188 * @return import for array list attribute
189 */
190 public static String getImportForArrayList() {
191 return IMPORT + COLLECTION_IMPORTS + PERIOD + ARRAY_LIST + SEMI_COLAN + NEW_LINE;
192 }
193
194 /**
195 * Returns import string for HasAugmentation class.
196 *
197 * @return import string for HasAugmentation class
198 */
199 public static String getHasAugmentationImport() {
200 return IMPORT + HAS_AUGMENTATION_CLASS_IMPORT_PKG + PERIOD + HAS_AUGMENTATION_CLASS_IMPORT_CLASS;
201 }
202
203 /**
204 * Returns import string for AugmentedInfo class.
205 *
206 * @return import string for AugmentedInfo class
207 */
208 public static String getAugmentedInfoImport() {
209 return IMPORT + AUGMENTED_INFO_CLASS_IMPORT_PKG + PERIOD + AUGMENTED_INFO_CLASS_IMPORT_CLASS;
210 }
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530211}