KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > dtree > DataDeltaNode


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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  *******************************************************************************/

11 package org.eclipse.core.internal.dtree;
12
13 import org.eclipse.core.runtime.IPath;
14
15 /**
16  * A <code>DataDeltaNode</code> contains information that represents the differences
17  * between itself and a node in another tree. Refer to the <code>DeltaDataTree</code>
18  * API and comments for details.
19  *
20  * @see DeltaDataTree
21  */

22 public class DataDeltaNode extends DataTreeNode {
23     /**
24      * Creates a node with the given name and data, but with no children.
25      */

26     DataDeltaNode(String JavaDoc name, Object JavaDoc data) {
27         super(name, data);
28     }
29
30     /**
31      * Creates a node with the given name, data and children.
32      */

33     DataDeltaNode(String JavaDoc name, Object JavaDoc data, AbstractDataTreeNode[] children) {
34         super(name, data, children);
35     }
36
37     /**
38      * @see AbstractDataTreeNode#asBackwardDelta(DeltaDataTree, DeltaDataTree, IPath)
39      */

40     AbstractDataTreeNode asBackwardDelta(DeltaDataTree myTree, DeltaDataTree parentTree, IPath key) {
41         AbstractDataTreeNode[] newChildren;
42         if (children.length == 0) {
43             newChildren = NO_CHILDREN;
44         } else {
45             newChildren = new AbstractDataTreeNode[children.length];
46             for (int i = children.length; --i >= 0;) {
47                 newChildren[i] = children[i].asBackwardDelta(myTree, parentTree, key.append(children[i].getName()));
48             }
49         }
50         return new DataDeltaNode(name, parentTree.getData(key), newChildren);
51     }
52
53     AbstractDataTreeNode compareWithParent(IPath key, DeltaDataTree parent, IComparator comparator) {
54         AbstractDataTreeNode[] comparedChildren = compareWithParent(children, key, parent, comparator);
55         Object JavaDoc oldData = parent.getData(key);
56         Object JavaDoc newData = data;
57         /* don't compare data of root */
58         int userComparison = 0;
59         if (key != parent.rootKey()) {
60             /* allow client to specify user comparison bits */
61             userComparison = comparator.compare(oldData, newData);
62         }
63         return new DataTreeNode(key.lastSegment(), new NodeComparison(oldData, newData, NodeComparison.K_CHANGED, userComparison), comparedChildren);
64     }
65
66     /**
67      * Creates and returns a new copy of the receiver. Makes a deep copy of
68      * children, but a shallow copy of name and data.
69      */

70     AbstractDataTreeNode copy() {
71         AbstractDataTreeNode[] childrenCopy;
72         if (children.length == 0) {
73             childrenCopy = NO_CHILDREN;
74         } else {
75             childrenCopy = new AbstractDataTreeNode[children.length];
76             System.arraycopy(children, 0, childrenCopy, 0, children.length);
77         }
78         return new DataDeltaNode(name, data, childrenCopy);
79     }
80
81     /**
82      * Returns true if the receiver represents delta information,
83      * false if it represents the complete information.
84      */

85     boolean isDelta() {
86         return true;
87     }
88
89     /**
90      * Simplifies the given node, and answers its replacement.
91      */

92     AbstractDataTreeNode simplifyWithParent(IPath key, DeltaDataTree parent, IComparator comparer) {
93         AbstractDataTreeNode[] simplifiedChildren = simplifyWithParent(children, key, parent, comparer);
94         /* don't compare root nodes */
95         if (!key.isRoot() && comparer.compare(parent.getData(key), data) == 0)
96             return new NoDataDeltaNode(name, simplifiedChildren);
97         return new DataDeltaNode(name, data, simplifiedChildren);
98     }
99
100     /**
101      * Returns a unicode representation of the node. This method is used
102      * for debugging purposes only (no NLS support needed)
103      */

104     public String JavaDoc toString() {
105         return "a DataDeltaNode(" + this.getName() + ") with " + getChildren().length + " children."; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
106
}
107
108     /**
109      * Returns a constant describing the type of node.
110      */

111     int type() {
112         return T_DELTA_NODE;
113     }
114 }
115
Popular Tags