KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > databinding > observable > tree > 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.core.internal.databinding.observable.tree;
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.
73      *
74      * @return the first element in this path
75      */

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

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

100     public boolean equals(Object JavaDoc other) {
101         if (!(other instanceof TreePath)) {
102             return false;
103         }
104         TreePath otherPath = (TreePath) other;
105         if (segments.length != otherPath.segments.length) {
106             return false;
107         }
108         for (int i = 0; i < segments.length; i++) {
109                 if (!segments[i].equals(otherPath.segments[i])) {
110                     return false;
111                 }
112         }
113         return true;
114     }
115
116     public int hashCode() {
117         if (hash == 0) {
118             for (int i = 0; i < segments.length; i++) {
119                     hash += segments[i].hashCode();
120             }
121         }
122         return hash;
123     }
124
125     /**
126      * Returns whether this path starts with the same segments as the given
127      * path, using the given comparer to compare segments.
128      *
129      * @param treePath
130      * path to compare to
131      * @return whether the given path is a prefix of this path, or the same as
132      * this path
133      */

134     public boolean startsWith(TreePath treePath) {
135         int thisSegmentCount = getSegmentCount();
136         int otherSegmentCount = treePath.getSegmentCount();
137         if (otherSegmentCount == thisSegmentCount) {
138             return equals(treePath);
139         }
140         if (otherSegmentCount > thisSegmentCount) {
141             return false;
142         }
143         for (int i = 0; i < otherSegmentCount; i++) {
144             Object JavaDoc otherSegment = treePath.getSegment(i);
145                 if (!otherSegment.equals(segments[i])) {
146                     return false;
147                 }
148         }
149         return true;
150     }
151
152     /**
153      * Returns a copy of this tree path with one segment removed from the end,
154      * or <code>null</code> if this tree path has no segments.
155      * @return a tree path
156      */

157     public TreePath getParentPath() {
158         int segmentCount = getSegmentCount();
159         if (segmentCount <= 1) {
160             return null;
161         }
162         Object JavaDoc[] parentSegments = new Object JavaDoc[segmentCount - 1];
163         System.arraycopy(segments, 0, parentSegments, 0, segmentCount - 1);
164         return new TreePath(parentSegments);
165     }
166
167     /**
168      * Returns a copy of this tree path with the given segment added at the end.
169      * @param newSegment
170      * @return a tree path
171      */

172     public TreePath createChildPath(Object JavaDoc newSegment) {
173         int segmentCount = getSegmentCount();
174         Object JavaDoc[] childSegments = new Object JavaDoc[segmentCount + 1];
175         if(segmentCount>0) {
176             System.arraycopy(segments, 0, childSegments, 0, segmentCount);
177         }
178         childSegments[segmentCount] = newSegment;
179         return new TreePath(childSegments);
180     }
181 }
182
Popular Tags