blob: 469e4168f1b0760f1655d48d06ba3897a638eab2 [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;
22
23import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswal6ef0b762016-04-05 12:45:45 +053024import org.onosproject.yangutils.translator.exception.TranslatorException;
Vinod Kumar S38046502016-03-23 15:30:27 +053025
Bharat saraswale2d51d62016-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;
30import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
31import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG;
32import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_CLASS;
33import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_PKG;
34import static org.onosproject.yangutils.utils.UtilConstants.LIST;
35import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
36import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
37import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
38
Vinod Kumar S38046502016-03-23 15:30:27 +053039/**
Bharat saraswald9822e92016-04-05 15:13:44 +053040 * Represents that generated Java file can contain imports.
Vinod Kumar S38046502016-03-23 15:30:27 +053041 */
42public class JavaImportData {
43
44 /**
45 * Flag to denote if any list in imported.
46 */
47 private boolean isListToImport;
48
49 /**
50 * Sorted set of import info, to be used to maintain the set of classes to
51 * be imported in the generated class.
52 */
53 private SortedSet<JavaQualifiedTypeInfo> importSet;
54
55 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053056 * Creates java import data object.
Vinod Kumar S38046502016-03-23 15:30:27 +053057 */
58 public JavaImportData() {
59 setImportSet(new TreeSet<JavaQualifiedTypeInfo>());
60 }
61
62 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053063 * Returns if the list needs to be imported.
Vinod Kumar S38046502016-03-23 15:30:27 +053064 *
65 * @return true if any of the attribute needs to be maintained as a list.
66 */
67 public boolean getIfListImported() {
68 return isListToImport;
69 }
70
71 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053072 * Sets the status of importing list.
Vinod Kumar S38046502016-03-23 15:30:27 +053073 *
74 * @param isList status to mention list is bing imported.
75 */
76 public void setIfListImported(boolean isList) {
77 isListToImport = isList;
78 }
79
80 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053081 * Returns the set containing the imported class/interface info.
Vinod Kumar S38046502016-03-23 15:30:27 +053082 *
83 * @return the set containing the imported class/interface info
84 */
85 public SortedSet<JavaQualifiedTypeInfo> getImportSet() {
86 return importSet;
87 }
88
89 /**
90 * Assign the set containing the imported class/interface info.
91 *
92 * @param importSet the set containing the imported class/interface info
93 */
94 private void setImportSet(SortedSet<JavaQualifiedTypeInfo> importSet) {
95 this.importSet = importSet;
96 }
97
98 /**
99 * Add an imported class/interface info if it is not already part of the
100 * collection.
101 *
102 * If already part of the collection, check if the packages are same, if so
103 * then return true, to denote it is already in the import collection, and
104 * it can be accessed without qualified access. If the packages do not
105 * match, then do not add to the import collection, and return false to
106 * denote, it is not added to import collection and needs to be accessed in
107 * a qualified manner.
108 *
109 * @param curNode current data model node
110 * @param newImportInfo class/interface info being imported
111 * @return status of new addition of class/interface to the import set
112 */
113 public boolean addImportInfo(YangNode curNode, JavaQualifiedTypeInfo newImportInfo) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530114
Vinod Kumar S38046502016-03-23 15:30:27 +0530115 if (!(curNode instanceof HasJavaImportData)) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530116 throw new TranslatorException("missing import info in data model node");
Vinod Kumar S38046502016-03-23 15:30:27 +0530117 }
118 for (JavaQualifiedTypeInfo curImportInfo : ((HasJavaImportData) curNode).getJavaImportData().getImportSet()) {
119 if (curImportInfo.getClassInfo()
120 .contentEquals(newImportInfo.getClassInfo())) {
121 return curImportInfo.getPkgInfo()
122 .contentEquals(newImportInfo.getPkgInfo());
123 }
124 }
125 ((HasJavaImportData) curNode).getJavaImportData().getImportSet().add(newImportInfo);
126 return true;
127 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530128
129 /**
130 * Returns import for class.
131 *
132 * @param attr java attribute info
133 * @return imports for class
134 */
135 public List<String> getImports(JavaAttributeInfo attr) {
136
137 String importString;
138 List<String> imports = new ArrayList<>();
139
140 for (JavaQualifiedTypeInfo importInfo : getImportSet()) {
141 importString = IMPORT;
142 if (importInfo.getPkgInfo() != EMPTY_STRING && importInfo.getClassInfo() != null
143 && importInfo.getPkgInfo() != JAVA_LANG) {
144 importString = importString + importInfo.getPkgInfo() + PERIOD + importInfo.getClassInfo() + SEMI_COLAN
145 + NEW_LINE;
146
147 imports.add(importString);
148 }
149 }
150
151 if (attr.isListAttr()) {
Bharat saraswald9822e92016-04-05 15:13:44 +0530152 imports.add(getImportForList());
Bharat saraswale2d51d62016-03-23 19:40:35 +0530153 }
154
155 java.util.Collections.sort(imports);
156 return imports;
157 }
158
159 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530160 * Returns import for hash and equals method.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530161 *
162 * @return import for hash and equals method
163 */
164 public String getImportForHashAndEquals() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530165 return IMPORT + JAVA_UTIL_OBJECTS_IMPORT_PKG + PERIOD + JAVA_UTIL_OBJECTS_IMPORT_CLASS;
166 }
167
168 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530169 * Returns import for to string method.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530170 *
171 * @return import for to string method
172 */
173 public String getImportForToString() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530174 return IMPORT + GOOGLE_MORE_OBJECT_IMPORT_PKG + PERIOD + GOOGLE_MORE_OBJECT_IMPORT_CLASS;
175 }
176
177 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530178 * Returns import for list attribute.
179 *
180 * @return import for for list attribute
Bharat saraswale2d51d62016-03-23 19:40:35 +0530181 */
Bharat saraswald9822e92016-04-05 15:13:44 +0530182
183 private static String getImportForList() {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530184 return IMPORT + COLLECTION_IMPORTS + PERIOD + LIST + SEMI_COLAN + NEW_LINE;
185 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530186}