KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > viewers > TreePath


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.viewers;
12
13 import org.eclipse.core.runtime.Assert;
14
15 /**
16  * A tree path denotes a model element in a tree viewer. Tree path objects have
17  * value semantics. A model element is represented by a path of elements in the
18  * tree from the root element to the leaf element.
19  * <p>
20  * Clients may instantiate this class. Not intended to be subclassed.
21  * </p>
22  *
23  * @since 3.2
24  */

25 public final class TreePath {
26     
27     /**
28      * Constant for representing an empty tree path.
29      */

30     public static final TreePath EMPTY = new TreePath(new Object JavaDoc[0]);
31     
32     private Object JavaDoc[] segments;
33
34     private int hash;
35
36     /**
37      * Constructs a path identifying a leaf node in a tree.
38      *
39      * @param segments
40      * path of elements to a leaf node in a tree, starting with the
41      * root element
42      */

43     public TreePath(Object JavaDoc[] segments) {
44         Assert.isNotNull(segments);
45         for (int i = 0; i < segments.length; i++) {
46             Assert.isNotNull(segments[i]);
47         }
48         this.segments = segments;
49     }
50
51     /**
52      * Returns the element at the specified index in this path.
53      *
54      * @param index
55      * index of element to return
56      * @return element at the specified index
57      */

58     public Object JavaDoc getSegment(int index) {
59         return segments[index];
60     }
61
62     /**
63      * Returns the number of elements in this path.
64      *
65      * @return the number of elements in this path
66      */

67     public int getSegmentCount() {
68         return segments.length;
69     }
70
71     /**
72      * Returns the first element in this path, or <code>null</code> if this
73      * path has no segments.
74      *
75      * @return the first element in this path
76      */

77     public Object JavaDoc getFirstSegment() {
78         if (segments.length == 0) {
79             return null;
80         }
81         return segments[0];
82     }
83
84     /**
85      * Returns the last element in this path, or <code>null</code> if this
86      * path has no segments.
87      *
88      * @return the last element in this path
89      */

90     public Object JavaDoc getLastSegment() {
91         if (segments.length == 0) {
92             return null;
93         }
94         return segments[segments.length - 1];
95     }
96
97     /*
98      * (non-Javadoc)
99      *
100      * @see java.lang.Object#equals(java.lang.Object)
101      */

102     public boolean equals(Object JavaDoc other) {
103         if (!(other instanceof TreePath)) {
104             return false;
105         }
106         return equals((TreePath) other, null);
107     }
108
109     /**
110      * (non-Javadoc)
111      *
112      * @see java.lang.Object#hashCode()
113      */

114     public int hashCode() {
115         if (hash == 0) {
116             hash = hashCode(null);
117         }
118         return hash;
119     }
120
121     /**
122      * Returns a hash code computed from the hash codes of the segments, using
123      * the given comparer to compute the hash codes of the segments.
124      *
125      * @param comparer
126      * comparer to use or <code>null</code> if the segments' hash
127      * codes should be computed by calling their hashCode() methods.
128      * @return the computed hash code
129      */

130     public int hashCode(IElementComparer comparer) {
131         int result = 0;
132         for (int i = 0; i < segments.length; i++) {
133             if (comparer == null) {
134                 result += segments[i].hashCode();
135             } else {
136                 result += comparer.hashCode(segments[i]);
137             }
138         }
139         return result;
140     }
141
142     /**
143      * Returns whether this path is equivalent to the given path using the
144      * specified comparer to compare individual elements.
145      *
146      * @param otherPath
147      * tree path to compare to
148      * @param comparer
149      * comparator to use or <code>null</code> if segments should be
150      * compared using equals()
151      * @return whether the paths are equal
152      */

153     public boolean equals(TreePath otherPath, IElementComparer comparer) {
154         if (otherPath == null) {
155             return false;
156         }
157         if (segments.length != otherPath.segments.length) {
158             return false;
159         }
160         for (int i = 0; i < segments.length; i++) {
161             if (comparer == null) {
162                 if (!segments[i].equals(otherPath.segments[i])) {
163                     return false;
164                 }
165             } else {
166                 if (!comparer.equals(segments[i], otherPath.segments[i])) {
167                     return false;
168                 }
169             }
170         }
171         return true;
172     }
173
174     /**
175      * Returns whether this path starts with the same segments as the given
176      * path, using the given comparer to compare segments.
177      *
178      * @param treePath
179      * path to compare to
180      * @param comparer
181      * the comparer to use, or <code>null</code> if equals() should
182      * be used to compare segments
183      * @return whether the given path is a prefix of this path, or the same as
184      * this path
185      */

186     public boolean startsWith(TreePath treePath, IElementComparer comparer) {
187         int thisSegmentCount = getSegmentCount();
188         int otherSegmentCount = treePath.getSegmentCount();
189         if (otherSegmentCount == thisSegmentCount) {
190             return equals(treePath, comparer);
191         }
192         if (otherSegmentCount > thisSegmentCount) {
193             return false;
194         }
195         for (int i = 0; i < otherSegmentCount; i++) {
196             Object JavaDoc otherSegment = treePath.getSegment(i);
197             if (comparer == null) {
198                 if (!otherSegment.equals(segments[i])) {
199                     return false;
200                 }
201             } else {
202                 if (!comparer.equals(otherSegment, segments[i])) {
203                     return false;
204                 }
205             }
206         }
207         return true;
208     }
209
210     /**
211      * Returns a copy of this tree path with one segment removed from the end,
212      * or <code>null</code> if this tree path has no segments.
213      * @return a tree path
214      */

215     public TreePath getParentPath() {
216         int segmentCount = getSegmentCount();
217         if (segmentCount < 1) {
218             return null;
219         } else if (segmentCount == 1) {
220             return EMPTY;
221         }
222         Object JavaDoc[] parentSegments = new Object JavaDoc[segmentCount - 1];
223         System.arraycopy(segments, 0, parentSegments, 0, segmentCount - 1);
224         return new TreePath(parentSegments);
225     }
226
227     /**
228      * Returns a copy of this tree path with the given segment added at the end.
229      * @param newSegment
230      * @return a tree path
231      */

232     public TreePath createChildPath(Object JavaDoc newSegment) {
233         int segmentCount = getSegmentCount();
234         Object JavaDoc[] childSegments = new Object JavaDoc[segmentCount + 1];
235         if(segmentCount>0) {
236             System.arraycopy(segments, 0, childSegments, 0, segmentCount);
237         }
238         childSegments[segmentCount] = newSegment;
239         return new TreePath(childSegments);
240     }
241 }
242
Popular Tags