blob: 2fffe6292d7dab5f8e13816a1299e7109a784f17 [file] [log] [blame]
Vinod Kumar S9f26ae52016-03-23 15:30:27 +05301/*
2 * Copyright 2016 Open Networking Laboratory
3 *
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
18import java.util.SortedSet;
19import java.util.TreeSet;
20
21import org.onosproject.yangutils.datamodel.YangNode;
22
23/**
24 * Generated Java file can contain imports.
25 */
26public class JavaImportData {
27
28 /**
29 * Flag to denote if any list in imported.
30 */
31 private boolean isListToImport;
32
33 /**
34 * Sorted set of import info, to be used to maintain the set of classes to
35 * be imported in the generated class.
36 */
37 private SortedSet<JavaQualifiedTypeInfo> importSet;
38
39 /**
40 * Default constructor.
41 */
42 public JavaImportData() {
43 setImportSet(new TreeSet<JavaQualifiedTypeInfo>());
44 }
45
46 /**
47 * Get if the list needs to be imported.
48 *
49 * @return true if any of the attribute needs to be maintained as a list.
50 */
51 public boolean getIfListImported() {
52 return isListToImport;
53 }
54
55 /**
56 * Set the status of importing list.
57 *
58 * @param isList status to mention list is bing imported.
59 */
60 public void setIfListImported(boolean isList) {
61 isListToImport = isList;
62 }
63
64 /**
65 * Get the set containing the imported class/interface info.
66 *
67 * @return the set containing the imported class/interface info
68 */
69 public SortedSet<JavaQualifiedTypeInfo> getImportSet() {
70 return importSet;
71 }
72
73 /**
74 * Assign the set containing the imported class/interface info.
75 *
76 * @param importSet the set containing the imported class/interface info
77 */
78 private void setImportSet(SortedSet<JavaQualifiedTypeInfo> importSet) {
79 this.importSet = importSet;
80 }
81
82 /**
83 * Add an imported class/interface info if it is not already part of the
84 * collection.
85 *
86 * If already part of the collection, check if the packages are same, if so
87 * then return true, to denote it is already in the import collection, and
88 * it can be accessed without qualified access. If the packages do not
89 * match, then do not add to the import collection, and return false to
90 * denote, it is not added to import collection and needs to be accessed in
91 * a qualified manner.
92 *
93 * @param curNode current data model node
94 * @param newImportInfo class/interface info being imported
95 * @return status of new addition of class/interface to the import set
96 */
97 public boolean addImportInfo(YangNode curNode, JavaQualifiedTypeInfo newImportInfo) {
98 if (!(curNode instanceof HasJavaImportData)) {
99 throw new RuntimeException("missing import info in data model node");
100 }
101 for (JavaQualifiedTypeInfo curImportInfo : ((HasJavaImportData) curNode).getJavaImportData().getImportSet()) {
102 if (curImportInfo.getClassInfo()
103 .contentEquals(newImportInfo.getClassInfo())) {
104 return curImportInfo.getPkgInfo()
105 .contentEquals(newImportInfo.getPkgInfo());
106 }
107 }
108 ((HasJavaImportData) curNode).getJavaImportData().getImportSet().add(newImportInfo);
109 return true;
110 }
111}