KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > tree > model > TreePath


1 /*
2  * Copyright 2004 The Apache Software Foundation.
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  */

16 package org.apache.myfaces.custom.tree.model;
17
18 import java.io.Serializable JavaDoc;
19
20
21 /**
22  * @author <a HREF="mailto:oliver@rossmueller.com">Oliver Rossmueller</a>
23  * @version $Revision: 1.3 $ $Date: 2004/10/13 11:50:58 $
24  * $Log: TreePath.java,v $
25  * Revision 1.3 2004/10/13 11:50:58 matze
26  * renamed packages to org.apache
27  *
28  * Revision 1.2 2004/07/01 21:53:04 mwessendorf
29  * ASF switch
30  *
31  * Revision 1.1 2004/04/22 10:20:24 manolito
32  * tree component
33  *
34  */

35 public final class TreePath
36         extends Object JavaDoc
37         implements Serializable JavaDoc
38 {
39
40
41     private Object JavaDoc[] elements;
42
43
44     /**
45      * Construct a pathElements from an array of Objects
46      *
47      * @param pathElements an array of Objects representing the pathElements to a node
48      */

49     public TreePath(Object JavaDoc[] pathElements)
50     {
51         if (pathElements == null || pathElements.length == 0)
52         {
53             throw new IllegalArgumentException JavaDoc("pathElements must be non null and not empty");
54         }
55
56         elements = pathElements;
57     }
58
59
60     /**
61      * Construct a new TreePath, which is the path identified by
62      * parent ending in lastElement.
63      */

64     protected TreePath(TreePath parent, Object JavaDoc lastElement)
65     {
66         elements = new Object JavaDoc[parent.elements.length + 1];
67
68         System.arraycopy(parent.elements, 0, elements, 0, elements.length - 1);
69         elements[elements.length - 1] = lastElement;
70     }
71
72
73     /**
74      * Construct a new TreePath from an array of objects.
75      *
76      * @param pathElements path elements
77      * @param length lenght of the new path
78      */

79     protected TreePath(Object JavaDoc[] pathElements, int length)
80     {
81         Object JavaDoc[] elements = new Object JavaDoc[length];
82
83         System.arraycopy(pathElements, 0, elements, 0, length);
84     }
85
86
87     /**
88      * Return an array of Objects containing the components of this
89      * TreePath.
90      *
91      * @return an array of Objects representing the TreePath
92      */

93     public Object JavaDoc[] getPath()
94     {
95         Object JavaDoc[] answer = new Object JavaDoc[elements.length];
96         System.arraycopy(elements, 0, answer, 0, elements.length);
97         return answer;
98     }
99
100
101     /**
102      * Returns the last component of this path.
103      *
104      * @return the Object at the end of the path
105      */

106     public Object JavaDoc getLastPathComponent()
107     {
108         return elements[elements.length - 1];
109     }
110
111
112     /**
113      * Return the number of elements in the path.
114      *
115      * @return an int giving a count of items the path
116      */

117     public int getPathCount()
118     {
119         return elements.length;
120     }
121
122
123     /**
124      * Return the path component at the specified index.
125      *
126      * @param index int specifying an index in the path
127      * @return the Object at that index location
128      * @throws IllegalArgumentException if the index is beyond the length
129      * of the path
130      */

131     public Object JavaDoc getPathComponent(int index)
132     {
133         if (index < 0 || index >= elements.length)
134         {
135             throw new IllegalArgumentException JavaDoc("Index " + index + " is out of range");
136         }
137
138         return elements[index];
139     }
140
141
142     /**
143      * Test two TreePaths for equality by checking each element of the
144      * paths for equality. Two paths are considered equal if they are of
145      * the same length and all element positions are equal.
146      *
147      * @param o the Object to compare
148      */

149     public boolean equals(Object JavaDoc o)
150     {
151         if (o == this)
152         {
153             return true;
154         }
155
156         if (!(o instanceof TreePath))
157         {
158             return false;
159         }
160         TreePath other = (TreePath)o;
161
162         if (elements.length != other.elements.length)
163         {
164             return false;
165         }
166
167         for (int i = 0; i < elements.length; i++)
168         {
169             Object JavaDoc thisElement = elements[i];
170             Object JavaDoc otherElement = other.elements[i];
171
172             if (!thisElement.equals(otherElement))
173             {
174                 return false;
175             }
176         }
177         return true;
178     }
179
180
181     /**
182      * Return the hashCode for the object. The hash code of a TreePath
183      * is defined to be the hash code of the last component in the path.
184      *
185      * @return the hashCode for the object
186      */

187     public int hashCode()
188     {
189         return elements[elements.length - 1].hashCode();
190     }
191
192
193     /**
194      * Return true if <code>path</code> is a
195      * descendant of this
196      * TreePath. A TreePath P1 is a descendent of a TreePath P2
197      * if P1 contains all of the components that make up
198      * P2's path. If P1 and P2 are equal P2 is not considered a descendant of
199      * P1.
200      *
201      * @return true if <code>path</code> is a descendant of this path
202      */

203     public boolean isDescendant(TreePath path)
204     {
205         if (path == null)
206         {
207             return false;
208         }
209
210         if (elements.length < path.elements.length)
211         {
212             // Can't be a descendant, has fewer components in the path.
213
return false;
214         }
215
216         for (int i = 0; i < elements.length; i++)
217         {
218             Object JavaDoc thisElement = elements[i];
219             Object JavaDoc otherElement = path.elements[i];
220
221             if (!thisElement.equals(otherElement))
222             {
223                 return false;
224             }
225         }
226         return true;
227     }
228
229
230     /**
231      * Return a new path by appending child to this path.
232      *
233      * @param child element to append
234      * @return new path
235      * @throws NullPointerException if child is null
236      */

237     public TreePath pathByAddingChild(Object JavaDoc child)
238     {
239         if (child == null)
240         {
241             throw new NullPointerException JavaDoc("Null child not allowed");
242         }
243
244         return new TreePath(this, child);
245     }
246
247
248     /**
249      * Return a path containing all the elements of this object, except
250      * the last path component.
251      */

252     public TreePath getParentPath()
253     {
254         return new TreePath(elements, elements.length - 1);
255     }
256
257
258     /**
259      * Return a string that displays and identifies this
260      * object's properties.
261      *
262      * @return a String representation of this object
263      */

264     public String JavaDoc toString()
265     {
266         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("[");
267
268         for (int i = 0; i < elements.length; i++)
269         {
270             if (i > 0)
271             {
272                 buffer.append(", ");
273             }
274             buffer.append(elements[i]);
275
276         }
277
278         buffer.append("]");
279         return buffer.toString();
280     }
281 }
282
283
Popular Tags