1 11 package org.eclipse.core.internal.dtree; 12 13 import org.eclipse.core.internal.utils.*; 14 import org.eclipse.core.runtime.Assert; 15 import org.eclipse.core.runtime.IPath; 16 17 22 public class DataTreeNode extends AbstractDataTreeNode { 23 protected Object data; 24 25 31 public DataTreeNode(String name, Object data) { 32 super(name, AbstractDataTreeNode.NO_CHILDREN); 33 this.data = data; 34 } 35 36 43 public DataTreeNode(String name, Object data, AbstractDataTreeNode[] children) { 44 super(name, children); 45 this.data = data; 46 } 47 48 51 AbstractDataTreeNode asBackwardDelta(DeltaDataTree myTree, DeltaDataTree parentTree, IPath key) { 52 if (parentTree.includes(key)) 53 return parentTree.copyCompleteSubtree(key); 54 return new DeletedNode(name); 55 } 56 57 62 AbstractDataTreeNode asReverseComparisonNode(IComparator comparator) { 63 NodeComparison comparison = null; 64 try { 65 comparison = ((NodeComparison) data).asReverseComparison(comparator); 66 } catch (ClassCastException e) { 67 Assert.isTrue(false, Messages.dtree_reverse); 68 } 69 70 int nextChild = 0; 71 for (int i = 0; i < children.length; i++) { 72 AbstractDataTreeNode child = children[i].asReverseComparisonNode(comparator); 73 if (child != null) { 74 children[nextChild++] = child; 75 } 76 } 77 78 if (nextChild == 0 && comparison.getUserComparison() == 0) { 79 80 return null; 81 } 82 83 84 data = comparison; 85 86 87 if (nextChild < children.length) { 88 AbstractDataTreeNode[] newChildren = new AbstractDataTreeNode[nextChild]; 89 System.arraycopy(children, 0, newChildren, 0, nextChild); 90 children = newChildren; 91 } 92 93 return this; 94 } 95 96 AbstractDataTreeNode compareWith(DataTreeNode other, IComparator comparator) { 97 AbstractDataTreeNode[] comparedChildren = compareWith(children, other.children, comparator); 98 Object oldData = data; 99 Object newData = other.data; 100 101 102 int userComparison = 0; 103 if (name != null) { 104 userComparison = comparator.compare(oldData, newData); 105 } 106 107 return new DataTreeNode(name, new NodeComparison(oldData, newData, NodeComparison.K_CHANGED, userComparison), comparedChildren); 108 } 109 110 AbstractDataTreeNode compareWithParent(IPath key, DeltaDataTree parent, IComparator comparator) { 111 if (!parent.includes(key)) 112 return convertToAddedComparisonNode(this, NodeComparison.K_ADDED); 113 DataTreeNode inParent = (DataTreeNode) parent.copyCompleteSubtree(key); 114 return inParent.compareWith(this, comparator); 115 } 116 117 120 AbstractDataTreeNode copy() { 121 if (children.length > 0) { 122 AbstractDataTreeNode[] childrenCopy = new AbstractDataTreeNode[children.length]; 123 System.arraycopy(children, 0, childrenCopy, 0, children.length); 124 return new DataTreeNode(name, data, childrenCopy); 125 } 126 return new DataTreeNode(name, data, children); 127 } 128 129 138 DataTreeNode copyWithNewChild(String localName, DataTreeNode childNode) { 139 140 AbstractDataTreeNode[] children = this.children; 141 int left = 0; 142 int right = children.length - 1; 143 while (left <= right) { 144 int mid = (left + right) / 2; 145 int compare = localName.compareTo(children[mid].name); 146 if (compare < 0) { 147 right = mid - 1; 148 } else if (compare > 0) { 149 left = mid + 1; 150 } else { 151 throw new Error (); } 153 } 154 155 AbstractDataTreeNode[] newChildren = new AbstractDataTreeNode[children.length + 1]; 156 System.arraycopy(children, 0, newChildren, 0, left); 157 childNode.setName(localName); 158 newChildren[left] = childNode; 159 System.arraycopy(children, left, newChildren, left + 1, children.length - left); 160 return new DataTreeNode(this.getName(), this.getData(), newChildren); 161 } 162 163 170 DataTreeNode copyWithoutChild(String localName) { 171 172 int index, newSize; 173 DataTreeNode newNode; 174 AbstractDataTreeNode children[]; 175 176 index = this.indexOfChild(localName); 177 if (index == -1) { 178 newNode = (DataTreeNode) this.copy(); 179 } else { 180 newSize = this.size() - 1; 181 children = new AbstractDataTreeNode[newSize]; 182 newNode = new DataTreeNode(this.getName(), this.getData(), children); 183 newNode.copyChildren(0, index - 1, this, 0); newNode.copyChildren(index, newSize - 1, this, index + 1); 185 } 186 return newNode; 187 } 188 189 194 protected static AbstractDataTreeNode[] forwardDeltaWith(AbstractDataTreeNode[] oldNodes, AbstractDataTreeNode[] newNodes, IComparator comparer) { 195 if (oldNodes.length == 0 && newNodes.length == 0) { 196 return NO_CHILDREN; 197 } 198 199 AbstractDataTreeNode[] childDeltas = null; 200 int numChildDeltas = 0; 201 int childDeltaMax = 0; 202 203 int oldIndex = 0; 205 int newIndex = 0; 206 while (oldIndex < oldNodes.length && newIndex < newNodes.length) { 207 String oldName = oldNodes[oldIndex].name; 208 String newName = newNodes[newIndex].name; 209 int compare = oldName.compareTo(newName); 210 if (compare == 0) { 211 AbstractDataTreeNode deltaNode = forwardDeltaWithOrNullIfEqual(oldNodes[oldIndex++], newNodes[newIndex++], comparer); 212 if (deltaNode != null) { 213 if (numChildDeltas >= childDeltaMax) { 214 if (childDeltas == null) 215 childDeltas = new AbstractDataTreeNode[childDeltaMax = 5]; 216 else 217 System.arraycopy(childDeltas, 0, childDeltas = new AbstractDataTreeNode[childDeltaMax = childDeltaMax * 2 + 1], 0, numChildDeltas); 218 } 219 childDeltas[numChildDeltas++] = deltaNode; 220 } 221 } else if (compare < 0) { 222 if (numChildDeltas >= childDeltaMax) { 223 if (childDeltas == null) 224 childDeltas = new AbstractDataTreeNode[childDeltaMax = 5]; 225 else 226 System.arraycopy(childDeltas, 0, childDeltas = new AbstractDataTreeNode[childDeltaMax = childDeltaMax * 2 + 1], 0, numChildDeltas); 227 } 228 childDeltas[numChildDeltas++] = new DeletedNode(oldName); 229 oldIndex++; 230 } else { 231 if (numChildDeltas >= childDeltaMax) { 232 if (childDeltas == null) 233 childDeltas = new AbstractDataTreeNode[childDeltaMax = 5]; 234 else 235 System.arraycopy(childDeltas, 0, childDeltas = new AbstractDataTreeNode[childDeltaMax = childDeltaMax * 2 + 1], 0, numChildDeltas); 236 } 237 childDeltas[numChildDeltas++] = newNodes[newIndex++]; 238 } 239 } 240 while (oldIndex < oldNodes.length) { 241 if (numChildDeltas >= childDeltaMax) { 242 if (childDeltas == null) 243 childDeltas = new AbstractDataTreeNode[childDeltaMax = 5]; 244 else 245 System.arraycopy(childDeltas, 0, childDeltas = new AbstractDataTreeNode[childDeltaMax = childDeltaMax * 2 + 1], 0, numChildDeltas); 246 } 247 childDeltas[numChildDeltas++] = new DeletedNode(oldNodes[oldIndex++].name); 248 } 249 while (newIndex < newNodes.length) { 250 if (numChildDeltas >= childDeltaMax) { 251 if (childDeltas == null) 252 childDeltas = new AbstractDataTreeNode[childDeltaMax = 5]; 253 else 254 System.arraycopy(childDeltas, 0, childDeltas = new AbstractDataTreeNode[childDeltaMax = childDeltaMax * 2 + 1], 0, numChildDeltas); 255 } 256 childDeltas[numChildDeltas++] = newNodes[newIndex++]; 257 } 258 259 if (numChildDeltas == 0) { 261 return NO_CHILDREN; 262 } 263 if (numChildDeltas < childDeltaMax) { 264 System.arraycopy(childDeltas, 0, childDeltas = new AbstractDataTreeNode[numChildDeltas], 0, numChildDeltas); 265 } 266 return childDeltas; 267 } 268 269 273 protected AbstractDataTreeNode forwardDeltaWith(DataTreeNode other, IComparator comparer) { 274 AbstractDataTreeNode deltaNode = forwardDeltaWithOrNullIfEqual(this, other, comparer); 275 if (deltaNode == null) { 276 return new NoDataDeltaNode(name, NO_CHILDREN); 277 } 278 return deltaNode; 279 } 280 281 286 protected static AbstractDataTreeNode forwardDeltaWithOrNullIfEqual(AbstractDataTreeNode oldNode, AbstractDataTreeNode newNode, IComparator comparer) { 287 AbstractDataTreeNode[] childDeltas = forwardDeltaWith(oldNode.children, newNode.children, comparer); 288 Object newData = newNode.getData(); 289 if (comparer.compare(oldNode.getData(), newData) == 0) { 290 if (childDeltas.length == 0) { 291 return null; 292 } 293 return new NoDataDeltaNode(newNode.name, childDeltas); 294 } 295 return new DataDeltaNode(newNode.name, newData, childDeltas); 296 } 297 298 301 public Object getData() { 302 return data; 303 } 304 305 308 boolean hasData() { 309 return true; 310 } 311 312 315 void setData(Object o) { 316 data = o; 317 } 318 319 322 AbstractDataTreeNode simplifyWithParent(IPath key, DeltaDataTree parent, IComparator comparer) { 323 324 if (!parent.includes(key)) { 325 return this; 326 } 327 330 DataTreeNode parentsNode = (DataTreeNode) parent.copyCompleteSubtree(key); 331 return parentsNode.forwardDeltaWith(this, comparer); 332 } 333 334 337 public void storeStrings(StringPool set) { 338 super.storeStrings(set); 339 Object o = data; 341 if (o instanceof IStringPoolParticipant) 342 ((IStringPoolParticipant) o).shareStrings(set); 343 } 344 345 349 public String toString() { 350 return "a DataTreeNode(" + this.getName() + ") with " + getChildren().length + " children."; } 352 353 356 int type() { 357 return T_COMPLETE_NODE; 358 } 359 } 360 | Popular Tags |