1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.custom.tree.model;
20
21 import java.io.Serializable;
22
23
24
25
26
27
28 public final class TreePath
29 extends Object
30 implements Serializable
31 {
32 private static final long serialVersionUID = 8972939732002296134L;
33 private Object[] elements;
34
35
36
37
38
39
40
41 public TreePath(Object[] pathElements)
42 {
43 if (pathElements == null || pathElements.length == 0)
44 {
45 throw new IllegalArgumentException("pathElements must be non null and not empty");
46 }
47
48 elements = pathElements;
49 }
50
51
52
53
54
55
56 protected TreePath(TreePath parent, Object lastElement)
57 {
58 elements = new Object[parent.elements.length + 1];
59
60 System.arraycopy(parent.elements, 0, elements, 0, elements.length - 1);
61 elements[elements.length - 1] = lastElement;
62 }
63
64
65
66
67
68
69
70
71 protected TreePath(Object[] pathElements, int length)
72 {
73 Object[] elements = new Object[length];
74
75 System.arraycopy(pathElements, 0, elements, 0, length);
76 }
77
78
79
80
81
82
83
84
85 public Object[] getPath()
86 {
87 Object[] answer = new Object[elements.length];
88 System.arraycopy(elements, 0, answer, 0, elements.length);
89 return answer;
90 }
91
92
93
94
95
96
97
98 public Object getLastPathComponent()
99 {
100 return elements[elements.length - 1];
101 }
102
103
104
105
106
107
108
109 public int getPathCount()
110 {
111 return elements.length;
112 }
113
114
115
116
117
118
119
120
121
122
123 public Object getPathComponent(int index)
124 {
125 if (index < 0 || index >= elements.length)
126 {
127 throw new IllegalArgumentException("Index " + index + " is out of range");
128 }
129
130 return elements[index];
131 }
132
133
134
135
136
137
138
139
140
141 public boolean equals(Object o)
142 {
143 if (o == this)
144 {
145 return true;
146 }
147
148 if (!(o instanceof TreePath))
149 {
150 return false;
151 }
152 TreePath other = (TreePath)o;
153
154 if (elements.length != other.elements.length)
155 {
156 return false;
157 }
158
159 for (int i = 0; i < elements.length; i++)
160 {
161 Object thisElement = elements[i];
162 Object otherElement = other.elements[i];
163
164 if (!thisElement.equals(otherElement))
165 {
166 return false;
167 }
168 }
169 return true;
170 }
171
172
173
174
175
176
177
178
179 public int hashCode()
180 {
181 return elements[elements.length - 1].hashCode();
182 }
183
184
185
186
187
188
189
190
191
192
193
194
195 public boolean isDescendant(TreePath path)
196 {
197 if (path == null)
198 {
199 return false;
200 }
201
202 if (elements.length < path.elements.length)
203 {
204
205 return false;
206 }
207
208 for (int i = 0; i < elements.length; i++)
209 {
210 Object thisElement = elements[i];
211 Object otherElement = path.elements[i];
212
213 if (!thisElement.equals(otherElement))
214 {
215 return false;
216 }
217 }
218 return true;
219 }
220
221
222
223
224
225
226
227
228
229 public TreePath pathByAddingChild(Object child)
230 {
231 if (child == null)
232 {
233 throw new NullPointerException("Null child not allowed");
234 }
235
236 return new TreePath(this, child);
237 }
238
239
240
241
242
243
244 public TreePath getParentPath()
245 {
246 return new TreePath(elements, elements.length - 1);
247 }
248
249
250
251
252
253
254
255
256 public String toString()
257 {
258 StringBuffer buffer = new StringBuffer("[");
259
260 for (int i = 0; i < elements.length; i++)
261 {
262 if (i > 0)
263 {
264 buffer.append(", ");
265 }
266 buffer.append(elements[i]);
267
268 }
269
270 buffer.append("]");
271 return buffer.toString();
272 }
273 }
274