blob: 723b937190723f58851d5b4f59842f67c8eb6cc4 [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;
Vidyashree Rama74453712016-04-18 12:29:39 +053022import static java.util.Collections.sort;
Vinod Kumar S38046502016-03-23 15:30:27 +053023
24import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053025import org.onosproject.yangutils.translator.exception.TranslatorException;
Vinod Kumar S38046502016-03-23 15:30:27 +053026
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053027import static org.onosproject.yangutils.utils.UtilConstants.ARRAY_LIST;
28import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO_CLASS_IMPORT_CLASS;
29import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO_CLASS_IMPORT_PKG;
Bharat saraswale2d51d62016-03-23 19:40:35 +053030import static org.onosproject.yangutils.utils.UtilConstants.COLLECTION_IMPORTS;
31import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
32import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_CLASS;
33import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_PKG;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053034import static org.onosproject.yangutils.utils.UtilConstants.HAS_AUGMENTATION_CLASS_IMPORT_CLASS;
35import static org.onosproject.yangutils.utils.UtilConstants.HAS_AUGMENTATION_CLASS_IMPORT_PKG;
Bharat saraswale2d51d62016-03-23 19:40:35 +053036import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
37import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG;
38import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_CLASS;
39import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_PKG;
40import static org.onosproject.yangutils.utils.UtilConstants.LIST;
41import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
42import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
43import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
44
Vinod Kumar S38046502016-03-23 15:30:27 +053045/**
Bharat saraswald9822e92016-04-05 15:13:44 +053046 * Represents that generated Java file can contain imports.
Vinod Kumar S38046502016-03-23 15:30:27 +053047 */
48public class JavaImportData {
49
50 /**
51 * Flag to denote if any list in imported.
52 */
53 private boolean isListToImport;
54
55 /**
56 * Sorted set of import info, to be used to maintain the set of classes to
57 * be imported in the generated class.
58 */
59 private SortedSet<JavaQualifiedTypeInfo> importSet;
60
61 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053062 * Creates java import data object.
Vinod Kumar S38046502016-03-23 15:30:27 +053063 */
64 public JavaImportData() {
65 setImportSet(new TreeSet<JavaQualifiedTypeInfo>());
66 }
67
68 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053069 * Returns if the list needs to be imported.
Vinod Kumar S38046502016-03-23 15:30:27 +053070 *
Vidyashree Rama74453712016-04-18 12:29:39 +053071 * @return true if any of the attribute needs to be maintained as a list
Vinod Kumar S38046502016-03-23 15:30:27 +053072 */
73 public boolean getIfListImported() {
74 return isListToImport;
75 }
76
77 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053078 * Sets the status of importing list.
Vinod Kumar S38046502016-03-23 15:30:27 +053079 *
Vidyashree Rama74453712016-04-18 12:29:39 +053080 * @param isList status to mention list is bing imported
Vinod Kumar S38046502016-03-23 15:30:27 +053081 */
82 public void setIfListImported(boolean isList) {
83 isListToImport = isList;
84 }
85
86 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053087 * Returns the set containing the imported class/interface info.
Vinod Kumar S38046502016-03-23 15:30:27 +053088 *
89 * @return the set containing the imported class/interface info
90 */
91 public SortedSet<JavaQualifiedTypeInfo> getImportSet() {
92 return importSet;
93 }
94
95 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053096 * Assigns the set containing the imported class/interface info.
Vinod Kumar S38046502016-03-23 15:30:27 +053097 *
98 * @param importSet the set containing the imported class/interface info
99 */
100 private void setImportSet(SortedSet<JavaQualifiedTypeInfo> importSet) {
101 this.importSet = importSet;
102 }
103
104 /**
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530105 * Adds an imported class/interface info if it is not already part of the
Vinod Kumar S38046502016-03-23 15:30:27 +0530106 * collection.
107 *
108 * If already part of the collection, check if the packages are same, if so
109 * then return true, to denote it is already in the import collection, and
110 * it can be accessed without qualified access. If the packages do not
111 * match, then do not add to the import collection, and return false to
112 * denote, it is not added to import collection and needs to be accessed in
113 * a qualified manner.
114 *
115 * @param curNode current data model node
116 * @param newImportInfo class/interface info being imported
117 * @return status of new addition of class/interface to the import set
118 */
119 public boolean addImportInfo(YangNode curNode, JavaQualifiedTypeInfo newImportInfo) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530120
Vinod Kumar S38046502016-03-23 15:30:27 +0530121 if (!(curNode instanceof HasJavaImportData)) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530122 throw new TranslatorException("missing import info in data model node");
Vinod Kumar S38046502016-03-23 15:30:27 +0530123 }
124 for (JavaQualifiedTypeInfo curImportInfo : ((HasJavaImportData) curNode).getJavaImportData().getImportSet()) {
125 if (curImportInfo.getClassInfo()
126 .contentEquals(newImportInfo.getClassInfo())) {
127 return curImportInfo.getPkgInfo()
128 .contentEquals(newImportInfo.getPkgInfo());
129 }
130 }
131 ((HasJavaImportData) curNode).getJavaImportData().getImportSet().add(newImportInfo);
132 return true;
133 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530134
135 /**
136 * Returns import for class.
137 *
138 * @param attr java attribute info
139 * @return imports for class
140 */
141 public List<String> getImports(JavaAttributeInfo attr) {
142
143 String importString;
144 List<String> imports = new ArrayList<>();
145
146 for (JavaQualifiedTypeInfo importInfo : getImportSet()) {
Vidyashree Rama74453712016-04-18 12:29:39 +0530147 if (!importInfo.getPkgInfo().equals(EMPTY_STRING) && importInfo.getClassInfo() != null
148 && !importInfo.getPkgInfo().equals(JAVA_LANG)) {
149 importString = IMPORT + importInfo.getPkgInfo() + PERIOD + importInfo.getClassInfo() + SEMI_COLAN
Bharat saraswale2d51d62016-03-23 19:40:35 +0530150 + NEW_LINE;
151
152 imports.add(importString);
153 }
154 }
155
156 if (attr.isListAttr()) {
Bharat saraswald9822e92016-04-05 15:13:44 +0530157 imports.add(getImportForList());
Bharat saraswale2d51d62016-03-23 19:40:35 +0530158 }
159
Vidyashree Rama74453712016-04-18 12:29:39 +0530160 sort(imports);
Bharat saraswale2d51d62016-03-23 19:40:35 +0530161 return imports;
162 }
163
164 /**
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530165 * Returns import for class.
166 *
167 * @return imports for class
168 */
169 public List<String> getImports() {
170 String importString;
171 List<String> imports = new ArrayList<>();
172
173 for (JavaQualifiedTypeInfo importInfo : getImportSet()) {
174 if (!importInfo.getPkgInfo().equals(EMPTY_STRING) && importInfo.getClassInfo() != null
175 && !importInfo.getPkgInfo().equals(JAVA_LANG)) {
176 importString = IMPORT + importInfo.getPkgInfo() + PERIOD + importInfo.getClassInfo() + SEMI_COLAN
177 + NEW_LINE;
178
179 imports.add(importString);
180 }
181 }
182
183 sort(imports);
184 return imports;
185 }
186
187 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530188 * Returns import for hash and equals method.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530189 *
190 * @return import for hash and equals method
191 */
192 public String getImportForHashAndEquals() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530193 return IMPORT + JAVA_UTIL_OBJECTS_IMPORT_PKG + PERIOD + JAVA_UTIL_OBJECTS_IMPORT_CLASS;
194 }
195
196 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530197 * Returns import for to string method.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530198 *
199 * @return import for to string method
200 */
201 public String getImportForToString() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530202 return IMPORT + GOOGLE_MORE_OBJECT_IMPORT_PKG + PERIOD + GOOGLE_MORE_OBJECT_IMPORT_CLASS;
203 }
204
205 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530206 * Returns import for list attribute.
207 *
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530208 * @return import for list attribute
Bharat saraswale2d51d62016-03-23 19:40:35 +0530209 */
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530210 public static String getImportForList() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530211 return IMPORT + COLLECTION_IMPORTS + PERIOD + LIST + SEMI_COLAN + NEW_LINE;
212 }
Bharat saraswalcc1cdab2016-04-16 02:28:25 +0530213
214 /**
215 * Returns import for array list attribute.
216 *
217 * @return import for array list attribute
218 */
219 public static String getImportForArrayList() {
220 return IMPORT + COLLECTION_IMPORTS + PERIOD + ARRAY_LIST + SEMI_COLAN + NEW_LINE;
221 }
222
223 /**
224 * Returns import string for HasAugmentation class.
225 *
226 * @return import string for HasAugmentation class
227 */
228 public static String getHasAugmentationImport() {
229 return IMPORT + HAS_AUGMENTATION_CLASS_IMPORT_PKG + PERIOD + HAS_AUGMENTATION_CLASS_IMPORT_CLASS;
230 }
231
232 /**
233 * Returns import string for AugmentedInfo class.
234 *
235 * @return import string for AugmentedInfo class
236 */
237 public static String getAugmentedInfoImport() {
238 return IMPORT + AUGMENTED_INFO_CLASS_IMPORT_PKG + PERIOD + AUGMENTED_INFO_CLASS_IMPORT_CLASS;
239 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530240}