1 | |
package org.apache.onami.configuration.variables; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.util.ArrayList; |
23 | |
import java.util.List; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
final class Tree<T> |
31 | |
{ |
32 | |
|
33 | |
|
34 | |
private T data; |
35 | |
|
36 | |
|
37 | 89372 | private Tree<T> parent = null; |
38 | |
|
39 | |
|
40 | 89372 | private List<Tree<T>> children = new ArrayList<Tree<T>>(); |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
public Tree( T data ) |
48 | 89372 | { |
49 | 89372 | this.data = data; |
50 | 89372 | } |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
public boolean isRoot() |
56 | |
{ |
57 | 313382 | return parent == null; |
58 | |
} |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
public boolean isLeaf() |
64 | |
{ |
65 | 0 | return children.isEmpty(); |
66 | |
} |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
public Tree<T> addLeaf( T child ) |
75 | |
{ |
76 | 73566 | Tree<T> leaf = new Tree<T>( child ); |
77 | 73566 | leaf.parent = this; |
78 | 73566 | children.add( leaf ); |
79 | 73566 | return leaf; |
80 | |
} |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
public Tree<T> getParent() |
86 | |
{ |
87 | 269238 | return parent; |
88 | |
} |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
public void removeFromParent() |
94 | |
{ |
95 | 0 | if ( !isRoot() ) |
96 | |
{ |
97 | 0 | getParent().removeSubtree( this ); |
98 | |
} |
99 | 0 | } |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
public void removeSubtree( Tree<T> subtree ) |
107 | |
{ |
108 | 0 | if ( children.remove( subtree ) ) |
109 | |
{ |
110 | 0 | subtree.parent = null; |
111 | |
} |
112 | 0 | } |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
public T getData() |
118 | |
{ |
119 | 126 | return data; |
120 | |
} |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
public int getDepth() |
127 | |
{ |
128 | 0 | int depth = 0; |
129 | 0 | Tree<T> curr = parent; |
130 | 0 | while ( curr != null ) |
131 | |
{ |
132 | 0 | curr = curr.parent; |
133 | 0 | depth++; |
134 | |
} |
135 | 0 | return depth; |
136 | |
} |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
public List<Tree<T>> getChildren() |
142 | |
{ |
143 | 126 | return children; |
144 | |
} |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
public Tree<T> addSubtree( Tree<T> subtree ) |
153 | |
{ |
154 | 0 | Tree<T> copy = addLeaf( subtree.data ); |
155 | 0 | copy.children = new ArrayList<Tree<T>>( subtree.children ); |
156 | 0 | return copy; |
157 | |
} |
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
public boolean inSubtrees( T element ) |
164 | |
{ |
165 | 0 | for ( Tree<T> child : getChildren() ) |
166 | |
{ |
167 | 0 | if ( child.isElement( element ) || child.inSubtrees( element ) ) |
168 | |
{ |
169 | 0 | return true; |
170 | |
} |
171 | |
} |
172 | 0 | return false; |
173 | |
} |
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
public boolean isElement( T element ) |
180 | |
{ |
181 | 134600 | return ( data.equals( element ) ); |
182 | |
} |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
public boolean inAncestors( T element ) |
190 | |
{ |
191 | 223954 | if ( !isRoot() ) |
192 | |
{ |
193 | 134600 | return getParent().isElement( element ) || getParent().inAncestors( element ); |
194 | |
} |
195 | 89354 | return false; |
196 | |
} |
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
public Tree<T> getRoot() |
202 | |
{ |
203 | 74 | if ( isRoot() ) |
204 | |
{ |
205 | 18 | return this; |
206 | |
} |
207 | 56 | return getParent().getRoot(); |
208 | |
} |
209 | |
|
210 | |
@Override |
211 | |
public String toString() |
212 | |
{ |
213 | 18 | StringBuilder buffer = new StringBuilder(); |
214 | 18 | toString( buffer, 0 ); |
215 | 18 | return buffer.toString(); |
216 | |
} |
217 | |
|
218 | |
private void toString( StringBuilder buffer, int level ) |
219 | |
{ |
220 | |
|
221 | |
|
222 | 126 | StringBuilder indent = new StringBuilder(); |
223 | |
Tree<T> prev; |
224 | 126 | Tree<T> curr = this; |
225 | 378 | for ( int i = level - 1; i >= 0; i-- ) |
226 | |
{ |
227 | 252 | prev = curr; |
228 | 252 | curr = prev.parent; |
229 | 252 | if ( i == level - 1 ) |
230 | |
{ |
231 | 108 | indent.append( " _|" ); |
232 | |
} |
233 | |
else |
234 | |
{ |
235 | 144 | indent.append( " " ); |
236 | |
|
237 | 144 | if ( i < level && curr.children.indexOf( prev ) < curr.children.size() - 1 ) |
238 | |
{ |
239 | 4 | indent.append( "|" ); |
240 | |
} |
241 | |
else |
242 | |
{ |
243 | 140 | indent.append( " " ); |
244 | |
} |
245 | |
} |
246 | |
} |
247 | 126 | buffer.append( indent.reverse() ); |
248 | |
|
249 | |
|
250 | 126 | buffer.append( getData() ).append( "\n" ); |
251 | |
|
252 | |
|
253 | 126 | for ( Tree<T> child : getChildren() ) |
254 | |
{ |
255 | 108 | child.toString( buffer, level + 1 ); |
256 | |
} |
257 | 126 | } |
258 | |
|
259 | |
} |