1 11 package org.eclipse.jface.viewers; 12 13 import org.eclipse.core.runtime.Assert; 14 15 25 public final class TreePath { 26 27 30 public static final TreePath EMPTY = new TreePath(new Object [0]); 31 32 private Object [] segments; 33 34 private int hash; 35 36 43 public TreePath(Object [] 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 58 public Object getSegment(int index) { 59 return segments[index]; 60 } 61 62 67 public int getSegmentCount() { 68 return segments.length; 69 } 70 71 77 public Object getFirstSegment() { 78 if (segments.length == 0) { 79 return null; 80 } 81 return segments[0]; 82 } 83 84 90 public Object getLastSegment() { 91 if (segments.length == 0) { 92 return null; 93 } 94 return segments[segments.length - 1]; 95 } 96 97 102 public boolean equals(Object other) { 103 if (!(other instanceof TreePath)) { 104 return false; 105 } 106 return equals((TreePath) other, null); 107 } 108 109 114 public int hashCode() { 115 if (hash == 0) { 116 hash = hashCode(null); 117 } 118 return hash; 119 } 120 121 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 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 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 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 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 [] parentSegments = new Object [segmentCount - 1]; 223 System.arraycopy(segments, 0, parentSegments, 0, segmentCount - 1); 224 return new TreePath(parentSegments); 225 } 226 227 232 public TreePath createChildPath(Object newSegment) { 233 int segmentCount = getSegmentCount(); 234 Object [] childSegments = new Object [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 |