blob: 7d8f4e053465e8fb50bb6e06f5c40dec992a506f [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;
Vidyashree Rama02f115f2016-04-18 12:29:39 +053022import static java.util.Collections.sort;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053023
24import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswal780eca32016-04-05 12:45:45 +053025import org.onosproject.yangutils.translator.exception.TranslatorException;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053026
Bharat saraswale2bc60d2016-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 saraswal84366c52016-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 saraswale2bc60d2016-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 saraswal84366c52016-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 S9f26ae52016-03-23 15:30:27 +053045/**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053046 * Represents that generated Java file can contain imports.
Vinod Kumar S9f26ae52016-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 saraswal63f26fb2016-04-05 15:13:44 +053062 * Creates java import data object.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053063 */
64 public JavaImportData() {
65 setImportSet(new TreeSet<JavaQualifiedTypeInfo>());
66 }
67
68 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053069 * Returns if the list needs to be imported.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053070 *
Vidyashree Rama02f115f2016-04-18 12:29:39 +053071 * @return true if any of the attribute needs to be maintained as a list
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053072 */
73 public boolean getIfListImported() {
74 return isListToImport;
75 }
76
77 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053078 * Sets the status of importing list.
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053079 *
Vidyashree Rama02f115f2016-04-18 12:29:39 +053080 * @param isList status to mention list is bing imported
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053081 */
82 public void setIfListImported(boolean isList) {
83 isListToImport = isList;
84 }
85
86 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053087 * Returns the set containing the imported class/interface info.
Vinod Kumar S9f26ae52016-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 saraswale2bc60d2016-04-16 02:28:25 +053096 * Assigns the set containing the imported class/interface info.
Vinod Kumar S9f26ae52016-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 saraswale2bc60d2016-04-16 02:28:25 +0530105 * Adds an imported class/interface info if it is not already part of the
Vinod Kumar S9f26ae52016-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 saraswal84366c52016-03-23 19:40:35 +0530120
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530121 if (!(curNode instanceof HasJavaImportData)) {
Bharat saraswal780eca32016-04-05 12:45:45 +0530122 throw new TranslatorException("missing import info in data model node");
Vinod Kumar S9f26ae52016-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 saraswal84366c52016-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 Rama02f115f2016-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 saraswal84366c52016-03-23 19:40:35 +0530150 + NEW_LINE;
151
152 imports.add(importString);
153 }
154 }
155
156 if (attr.isListAttr()) {
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530157 imports.add(getImportForList());
Bharat saraswal84366c52016-03-23 19:40:35 +0530158 }
159
Vidyashree Rama02f115f2016-04-18 12:29:39 +0530160 sort(imports);
Bharat saraswal84366c52016-03-23 19:40:35 +0530161 return imports;
162 }
163
164 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530165 * Returns import for hash and equals method.
Bharat saraswal84366c52016-03-23 19:40:35 +0530166 *
167 * @return import for hash and equals method
168 */
169 public String getImportForHashAndEquals() {
Bharat saraswal84366c52016-03-23 19:40:35 +0530170 return IMPORT + JAVA_UTIL_OBJECTS_IMPORT_PKG + PERIOD + JAVA_UTIL_OBJECTS_IMPORT_CLASS;
171 }
172
173 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530174 * Returns import for to string method.
Bharat saraswal84366c52016-03-23 19:40:35 +0530175 *
176 * @return import for to string method
177 */
178 public String getImportForToString() {
Bharat saraswal84366c52016-03-23 19:40:35 +0530179 return IMPORT + GOOGLE_MORE_OBJECT_IMPORT_PKG + PERIOD + GOOGLE_MORE_OBJECT_IMPORT_CLASS;
180 }
181
182 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530183 * Returns import for list attribute.
184 *
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530185 * @return import for list attribute
Bharat saraswal84366c52016-03-23 19:40:35 +0530186 */
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530187 public static String getImportForList() {
Bharat saraswal84366c52016-03-23 19:40:35 +0530188 return IMPORT + COLLECTION_IMPORTS + PERIOD + LIST + SEMI_COLAN + NEW_LINE;
189 }
Bharat saraswale2bc60d2016-04-16 02:28:25 +0530190
191 /**
192 * Returns import for array list attribute.
193 *
194 * @return import for array list attribute
195 */
196 public static String getImportForArrayList() {
197 return IMPORT + COLLECTION_IMPORTS + PERIOD + ARRAY_LIST + SEMI_COLAN + NEW_LINE;
198 }
199
200 /**
201 * Returns import string for HasAugmentation class.
202 *
203 * @return import string for HasAugmentation class
204 */
205 public static String getHasAugmentationImport() {
206 return IMPORT + HAS_AUGMENTATION_CLASS_IMPORT_PKG + PERIOD + HAS_AUGMENTATION_CLASS_IMPORT_CLASS;
207 }
208
209 /**
210 * Returns import string for AugmentedInfo class.
211 *
212 * @return import string for AugmentedInfo class
213 */
214 public static String getAugmentedInfoImport() {
215 return IMPORT + AUGMENTED_INFO_CLASS_IMPORT_PKG + PERIOD + AUGMENTED_INFO_CLASS_IMPORT_CLASS;
216 }
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530217}