comparison org.eclipse.core.databinding.observable/src/org/eclipse/core/internal/databinding/observable/tree/TreePath.d @ 95:6208d4f6a277

Added trees for databinding.beans and observable
author Frank Benoit <benoit@tionex.de>
date Tue, 21 Apr 2009 10:55:51 +0200
parents
children b74ac5dfcc06
comparison
equal deleted inserted replaced
94:1d37a7813832 95:6208d4f6a277
1 /*******************************************************************************
2 * Copyright (c) 2005, 2008 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 * Matthew Hall - bug 118516
11 *******************************************************************************/
12 package org.eclipse.core.internal.databinding.observable.tree;
13
14 import org.eclipse.core.runtime.Assert;
15
16 /**
17 * A tree path denotes a model element in a tree viewer. Tree path objects have
18 * value semantics. A model element is represented by a path of elements in the
19 * tree from the root element to the leaf element.
20 * <p>
21 * Clients may instantiate this class. Not intended to be subclassed.
22 * </p>
23 *
24 * @since 3.2
25 */
26 public final class TreePath {
27
28 /**
29 * Constant for representing an empty tree path.
30 */
31 public static final TreePath EMPTY = new TreePath(new Object[0]);
32
33 private Object[] segments;
34
35 private int hash;
36
37 /**
38 * Constructs a path identifying a leaf node in a tree.
39 *
40 * @param segments
41 * path of elements to a leaf node in a tree, starting with the
42 * root element
43 */
44 public this(Object[] segments) {
45 Assert.isNotNull(segments, "Segments array cannot be null"); //$NON-NLS-1$
46 this.segments = new Object[segments.length];
47 for (int i = 0; i < segments.length; i++) {
48 Assert.isNotNull(segments[i], "Segments array cannot contain null"); //$NON-NLS-1$
49 this.segments[i] = segments[i];
50 }
51 }
52
53 /**
54 * Returns the element at the specified index in this path.
55 *
56 * @param index
57 * index of element to return
58 * @return element at the specified index
59 */
60 public Object getSegment(int index) {
61 return segments[index];
62 }
63
64 /**
65 * Returns the number of elements in this path.
66 *
67 * @return the number of elements in this path
68 */
69 public int getSegmentCount() {
70 return segments.length;
71 }
72
73 /**
74 * Returns the first element in this path.
75 *
76 * @return the first element in this path
77 */
78 public Object getFirstSegment() {
79 if (segments.length is 0) {
80 return null;
81 }
82 return segments[0];
83 }
84
85 /**
86 * Returns the last element in this path.
87 *
88 * @return the last element in this path
89 */
90 public Object getLastSegment() {
91 if (segments.length is 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 override equals_t opEquals(Object other) {
103 if (!( null !is cast(TreePath)other )) {
104 return false;
105 }
106 TreePath otherPath = cast(TreePath) other;
107 if (segments.length !is otherPath.segments.length) {
108 return false;
109 }
110 for (int i = 0; i < segments.length; i++) {
111 if (!segments[i].equals(otherPath.segments[i])) {
112 return false;
113 }
114 }
115 return true;
116 }
117
118 public override hash_t toHash() {
119 if (hash is 0) {
120 for (int i = 0; i < segments.length; i++) {
121 hash += segments[i].hashCode();
122 }
123 }
124 return hash;
125 }
126
127 /**
128 * Returns whether this path starts with the same segments as the given
129 * path, using the given comparer to compare segments.
130 *
131 * @param treePath
132 * path to compare to
133 * @return whether the given path is a prefix of this path, or the same as
134 * this path
135 */
136 public bool startsWith(TreePath treePath) {
137 int thisSegmentCount = getSegmentCount();
138 int otherSegmentCount = treePath.getSegmentCount();
139 if (otherSegmentCount is thisSegmentCount) {
140 return equals(treePath);
141 }
142 if (otherSegmentCount > thisSegmentCount) {
143 return false;
144 }
145 for (int i = 0; i < otherSegmentCount; i++) {
146 Object otherSegment = treePath.getSegment(i);
147 if (!otherSegment.equals(segments[i])) {
148 return false;
149 }
150 }
151 return true;
152 }
153
154 /**
155 * Returns a copy of this tree path with one segment removed from the end,
156 * or <code>null</code> if this tree path has no segments.
157 * @return a tree path
158 */
159 public TreePath getParentPath() {
160 int segmentCount = getSegmentCount();
161 if (segmentCount <= 1) {
162 return null;
163 }
164 Object[] parentSegments = new Object[segmentCount - 1];
165 System.arraycopy(segments, 0, parentSegments, 0, segmentCount - 1);
166 return new TreePath(parentSegments);
167 }
168
169 /**
170 * Returns a copy of this tree path with the given segment added at the end.
171 * @param newSegment
172 * @return a tree path
173 */
174 public TreePath createChildPath(Object newSegment) {
175 int segmentCount = getSegmentCount();
176 Object[] childSegments = new Object[segmentCount + 1];
177 if(segmentCount>0) {
178 System.arraycopy(segments, 0, childSegments, 0, segmentCount);
179 }
180 childSegments[segmentCount] = newSegment;
181 return new TreePath(childSegments);
182 }
183 }