blob: 2c2ce05b8fac4fcb67ca31d484777e12873f17f3 [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 saraswale2d51d62016-03-23 19:40:35 +053027import static org.onosproject.yangutils.utils.UtilConstants.COLLECTION_IMPORTS;
28import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
29import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_CLASS;
30import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_PKG;
31import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
32import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG;
33import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_CLASS;
34import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_PKG;
35import static org.onosproject.yangutils.utils.UtilConstants.LIST;
36import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
37import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
38import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
39
Vinod Kumar S38046502016-03-23 15:30:27 +053040/**
Bharat saraswald9822e92016-04-05 15:13:44 +053041 * Represents that generated Java file can contain imports.
Vinod Kumar S38046502016-03-23 15:30:27 +053042 */
43public class JavaImportData {
44
45 /**
46 * Flag to denote if any list in imported.
47 */
48 private boolean isListToImport;
49
50 /**
51 * Sorted set of import info, to be used to maintain the set of classes to
52 * be imported in the generated class.
53 */
54 private SortedSet<JavaQualifiedTypeInfo> importSet;
55
56 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053057 * Creates java import data object.
Vinod Kumar S38046502016-03-23 15:30:27 +053058 */
59 public JavaImportData() {
60 setImportSet(new TreeSet<JavaQualifiedTypeInfo>());
61 }
62
63 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053064 * Returns if the list needs to be imported.
Vinod Kumar S38046502016-03-23 15:30:27 +053065 *
Vidyashree Rama74453712016-04-18 12:29:39 +053066 * @return true if any of the attribute needs to be maintained as a list
Vinod Kumar S38046502016-03-23 15:30:27 +053067 */
68 public boolean getIfListImported() {
69 return isListToImport;
70 }
71
72 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053073 * Sets the status of importing list.
Vinod Kumar S38046502016-03-23 15:30:27 +053074 *
Vidyashree Rama74453712016-04-18 12:29:39 +053075 * @param isList status to mention list is bing imported
Vinod Kumar S38046502016-03-23 15:30:27 +053076 */
77 public void setIfListImported(boolean isList) {
78 isListToImport = isList;
79 }
80
81 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053082 * Returns the set containing the imported class/interface info.
Vinod Kumar S38046502016-03-23 15:30:27 +053083 *
84 * @return the set containing the imported class/interface info
85 */
86 public SortedSet<JavaQualifiedTypeInfo> getImportSet() {
87 return importSet;
88 }
89
90 /**
91 * Assign the set containing the imported class/interface info.
92 *
93 * @param importSet the set containing the imported class/interface info
94 */
95 private void setImportSet(SortedSet<JavaQualifiedTypeInfo> importSet) {
96 this.importSet = importSet;
97 }
98
99 /**
100 * Add an imported class/interface info if it is not already part of the
101 * collection.
102 *
103 * If already part of the collection, check if the packages are same, if so
104 * then return true, to denote it is already in the import collection, and
105 * it can be accessed without qualified access. If the packages do not
106 * match, then do not add to the import collection, and return false to
107 * denote, it is not added to import collection and needs to be accessed in
108 * a qualified manner.
109 *
110 * @param curNode current data model node
111 * @param newImportInfo class/interface info being imported
112 * @return status of new addition of class/interface to the import set
113 */
114 public boolean addImportInfo(YangNode curNode, JavaQualifiedTypeInfo newImportInfo) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530115
Vinod Kumar S38046502016-03-23 15:30:27 +0530116 if (!(curNode instanceof HasJavaImportData)) {
Bharat saraswal6ef0b762016-04-05 12:45:45 +0530117 throw new TranslatorException("missing import info in data model node");
Vinod Kumar S38046502016-03-23 15:30:27 +0530118 }
119 for (JavaQualifiedTypeInfo curImportInfo : ((HasJavaImportData) curNode).getJavaImportData().getImportSet()) {
120 if (curImportInfo.getClassInfo()
121 .contentEquals(newImportInfo.getClassInfo())) {
122 return curImportInfo.getPkgInfo()
123 .contentEquals(newImportInfo.getPkgInfo());
124 }
125 }
126 ((HasJavaImportData) curNode).getJavaImportData().getImportSet().add(newImportInfo);
127 return true;
128 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530129
130 /**
131 * Returns import for class.
132 *
133 * @param attr java attribute info
134 * @return imports for class
135 */
136 public List<String> getImports(JavaAttributeInfo attr) {
137
138 String importString;
139 List<String> imports = new ArrayList<>();
140
141 for (JavaQualifiedTypeInfo importInfo : getImportSet()) {
Vidyashree Rama74453712016-04-18 12:29:39 +0530142 if (!importInfo.getPkgInfo().equals(EMPTY_STRING) && importInfo.getClassInfo() != null
143 && !importInfo.getPkgInfo().equals(JAVA_LANG)) {
144 importString = IMPORT + importInfo.getPkgInfo() + PERIOD + importInfo.getClassInfo() + SEMI_COLAN
Bharat saraswale2d51d62016-03-23 19:40:35 +0530145 + 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
Vidyashree Rama74453712016-04-18 12:29:39 +0530155 sort(imports);
Bharat saraswale2d51d62016-03-23 19:40:35 +0530156 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}