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