comparison org.eclipse.jface/src/org/eclipse/jface/viewers/TreePath.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
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 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module org.eclipse.jface.viewers.TreePath;
14
15 import org.eclipse.jface.viewers.IElementComparer;
16
17 import org.eclipse.core.runtime.Assert;
18
19 import java.lang.all;
20
21 /**
22 * A tree path denotes a model element in a tree viewer. Tree path objects have
23 * value semantics. A model element is represented by a path of elements in the
24 * tree from the root element to the leaf element.
25 * <p>
26 * Clients may instantiate this class. Not intended to be subclassed.
27 * </p>
28 *
29 * @since 3.2
30 */
31 public final class TreePath {
32
33 /**
34 * Constant for representing an empty tree path.
35 */
36 public static const TreePath EMPTY;
37
38 private Object[] segments;
39
40 private int hash;
41
42 static this(){
43 EMPTY = new TreePath(new Object[0]);
44 }
45
46 /**
47 * Constructs a path identifying a leaf node in a tree.
48 *
49 * @param segments
50 * path of elements to a leaf node in a tree, starting with the
51 * root element
52 */
53 public this(Object[] segments) {
54 // Assert.isNotNull(segments);
55 for (int i = 0; i < segments.length; i++) {
56 Assert.isNotNull(segments[i]);
57 }
58 this.segments = segments;
59 }
60
61 /**
62 * Returns the element at the specified index in this path.
63 *
64 * @param index
65 * index of element to return
66 * @return element at the specified index
67 */
68 public Object getSegment(int index) {
69 return segments[index];
70 }
71
72 /**
73 * Returns the number of elements in this path.
74 *
75 * @return the number of elements in this path
76 */
77 public int getSegmentCount() {
78 return segments.length;
79 }
80
81 /**
82 * Returns the first element in this path, or <code>null</code> if this
83 * path has no segments.
84 *
85 * @return the first element in this path
86 */
87 public Object getFirstSegment() {
88 if (segments.length is 0) {
89 return null;
90 }
91 return segments[0];
92 }
93
94 /**
95 * Returns the last element in this path, or <code>null</code> if this
96 * path has no segments.
97 *
98 * @return the last element in this path
99 */
100 public Object getLastSegment() {
101 if (segments.length is 0) {
102 return null;
103 }
104 return segments[segments.length - 1];
105 }
106
107 /*
108 * (non-Javadoc)
109 *
110 * @see java.lang.Object#equals(java.lang.Object)
111 */
112 public override int opEquals(Object other) {
113 if (!(cast(TreePath)other )) {
114 return false;
115 }
116 return opEquals(cast(TreePath) other, null);
117 }
118
119 /**
120 * (non-Javadoc)
121 *
122 * @see java.lang.Object#hashCode()
123 */
124 public override hash_t toHash() {
125 if (hash is 0) {
126 hash = toHash(null);
127 }
128 return hash;
129 }
130
131 /**
132 * Returns a hash code computed from the hash codes of the segments, using
133 * the given comparer to compute the hash codes of the segments.
134 *
135 * @param comparer
136 * comparer to use or <code>null</code> if the segments' hash
137 * codes should be computed by calling their hashCode() methods.
138 * @return the computed hash code
139 */
140 public hash_t toHash(IElementComparer comparer) {
141 int result = 0;
142 for (int i = 0; i < segments.length; i++) {
143 if (comparer is null) {
144 result += segments[i].toHash();
145 } else {
146 result += comparer.toHash(segments[i]);
147 }
148 }
149 return result;
150 }
151
152 /**
153 * Returns whether this path is equivalent to the given path using the
154 * specified comparer to compare individual elements.
155 *
156 * @param otherPath
157 * tree path to compare to
158 * @param comparer
159 * comparator to use or <code>null</code> if segments should be
160 * compared using equals()
161 * @return whether the paths are equal
162 */
163 public int opEquals(TreePath otherPath, IElementComparer comparer) {
164 if (otherPath is null) {
165 return false;
166 }
167 if (segments.length !is otherPath.segments.length) {
168 return false;
169 }
170 for (int i = 0; i < segments.length; i++) {
171 if (comparer is null) {
172 if (!segments[i].opEquals(otherPath.segments[i])) {
173 return false;
174 }
175 } else {
176 if (!comparer.opEquals(segments[i], otherPath.segments[i])) {
177 return false;
178 }
179 }
180 }
181 return true;
182 }
183
184 /**
185 * Returns whether this path starts with the same segments as the given
186 * path, using the given comparer to compare segments.
187 *
188 * @param treePath
189 * path to compare to
190 * @param comparer
191 * the comparer to use, or <code>null</code> if equals() should
192 * be used to compare segments
193 * @return whether the given path is a prefix of this path, or the same as
194 * this path
195 */
196 public bool startsWith(TreePath treePath, IElementComparer comparer) {
197 int thisSegmentCount = getSegmentCount();
198 int otherSegmentCount = treePath.getSegmentCount();
199 if (otherSegmentCount is thisSegmentCount) {
200 return opEquals(treePath, comparer) !is 0;
201 }
202 if (otherSegmentCount > thisSegmentCount) {
203 return false;
204 }
205 for (int i = 0; i < otherSegmentCount; i++) {
206 Object otherSegment = treePath.getSegment(i);
207 if (comparer is null) {
208 if (!otherSegment.opEquals(segments[i])) {
209 return false;
210 }
211 } else {
212 if (!comparer.opEquals(otherSegment, segments[i])) {
213 return false;
214 }
215 }
216 }
217 return true;
218 }
219
220 /**
221 * Returns a copy of this tree path with one segment removed from the end,
222 * or <code>null</code> if this tree path has no segments.
223 * @return a tree path
224 */
225 public TreePath getParentPath() {
226 int segmentCount = getSegmentCount();
227 if (segmentCount < 1) {
228 return null;
229 } else if (segmentCount is 1) {
230 return EMPTY;
231 }
232 Object[] parentSegments = new Object[segmentCount - 1];
233 System.arraycopy(segments, 0, parentSegments, 0, segmentCount - 1);
234 return new TreePath(parentSegments);
235 }
236
237 /**
238 * Returns a copy of this tree path with the given segment added at the end.
239 * @param newSegment
240 * @return a tree path
241 */
242 public TreePath createChildPath(Object newSegment) {
243 int segmentCount = getSegmentCount();
244 Object[] childSegments = new Object[segmentCount + 1];
245 if(segmentCount>0) {
246 System.arraycopy(segments, 0, childSegments, 0, segmentCount);
247 }
248 childSegments[segmentCount] = newSegment;
249 return new TreePath(childSegments);
250 }
251 }