1 /*
2 * Themis: Java Project Framework
3 * Copyright 2012-2026. Tony Washer
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6 * use this file except in compliance with the License. You may obtain a copy
7 * of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 */
17 package io.github.tonywasher.joceanus.themis.lethe.ui;
18
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.Iterator;
22 import java.util.List;
23
24 import io.github.tonywasher.joceanus.themis.lethe.analysis.ThemisAnalysisElement;
25
26 /**
27 * Source Panel Tree Entry.
28 */
29 public class ThemisSourceEntry {
30 /**
31 * Entry name prefix.
32 */
33 private static final String ENTRY_PREFIX = "TreeItem";
34
35 /**
36 * The Parent.
37 */
38 private final ThemisSourceEntry theParent;
39
40 /**
41 * The id of the entry.
42 */
43 private final int theId;
44
45 /**
46 * The Header List.
47 */
48 private List<ThemisSourceEntry> theHeaderList;
49
50 /**
51 * The Child List.
52 */
53 private List<ThemisSourceEntry> theChildList;
54
55 /**
56 * The Trailer List.
57 */
58 private List<ThemisSourceEntry> theTrailerList;
59
60 /**
61 * The unique name of the entry.
62 */
63 private final String theUniqueName;
64
65 /**
66 * The name of the entry.
67 */
68 private final String theDisplayName;
69
70 /**
71 * The object for the entry.
72 */
73 private final ThemisAnalysisElement theObject;
74
75 /**
76 * Constructor.
77 *
78 * @param pElement the sourceElement
79 */
80 ThemisSourceEntry(final ThemisAnalysisElement pElement) {
81 this(null, pElement);
82 }
83
84 /**
85 * Constructor.
86 *
87 * @param pParent the parent entry
88 * @param pElement the sourceElement
89 */
90 ThemisSourceEntry(final ThemisSourceEntry pParent,
91 final ThemisAnalysisElement pElement) {
92 /* Store parameters */
93 theParent = pParent;
94 theObject = pElement;
95
96 /* Allocate id and unique name */
97 theId = ThemisSourcePanel.getNextId();
98 theUniqueName = ENTRY_PREFIX + theId;
99 theDisplayName = pElement.toString();
100
101 /* If we have a parent */
102 if (pParent != null) {
103 /* Add the entry to the child list */
104 pParent.addChild(this);
105 }
106 }
107
108 /**
109 * Get parent.
110 *
111 * @return the parent
112 */
113 ThemisSourceEntry getParent() {
114 return theParent;
115 }
116
117 /**
118 * Get unique name.
119 *
120 * @return the name
121 */
122 String getUniqueName() {
123 return theUniqueName;
124 }
125
126 /**
127 * Get object.
128 *
129 * @return the object
130 */
131 ThemisAnalysisElement getObject() {
132 return theObject;
133 }
134
135 @Override
136 public String toString() {
137 return theDisplayName;
138 }
139
140 /**
141 * Get child iterator.
142 *
143 * @return the iterator
144 */
145 Iterator<ThemisSourceEntry> childIterator() {
146 return theChildList == null
147 ? Collections.emptyIterator()
148 : theChildList.iterator();
149 }
150
151 /**
152 * Add header.
153 *
154 * @param pHeader the header to add
155 */
156 private void addHeader(final ThemisSourceEntry pHeader) {
157 if (theHeaderList == null) {
158 theHeaderList = new ArrayList<>();
159 }
160 theHeaderList.add(pHeader);
161 }
162
163 /**
164 * Add child.
165 *
166 * @param pChild the child to add
167 */
168 private void addChild(final ThemisSourceEntry pChild) {
169 if (theChildList == null) {
170 theChildList = new ArrayList<>();
171 }
172 theChildList.add(pChild);
173 }
174
175 /**
176 * Add trailer.
177 *
178 * @param pTrailer the trailer to add
179 */
180 private void addTrailer(final ThemisSourceEntry pTrailer) {
181 if (theTrailerList == null) {
182 theTrailerList = new ArrayList<>();
183 }
184 theTrailerList.add(pTrailer);
185 }
186
187 @Override
188 public boolean equals(final Object pThat) {
189 /* Handle trivial cases */
190 if (this == pThat) {
191 return true;
192 }
193 if (pThat == null) {
194 return false;
195 }
196
197 /* Check class */
198 if (!(pThat instanceof ThemisSourceEntry)) {
199 return false;
200 }
201
202 /* Access as ThemisSourceEntry */
203 final ThemisSourceEntry myThat = (ThemisSourceEntry) pThat;
204
205 /* Must have same id */
206 return theId == myThat.theId;
207 }
208
209 @Override
210 public int hashCode() {
211 return theId;
212 }
213 }