KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
14  * This class represents the changes in a single node between two data trees.
15  */

16 public final class NodeComparison {
17     /**
18      * The data of the old tree
19      */

20     private Object JavaDoc oldData;
21
22     /**
23      * The data of the new tree
24      */

25     private Object JavaDoc newData;
26
27     /**
28      * Integer describing changes between two data elements
29      */

30     private int comparison;
31
32     /**
33      * Extra integer that can be assigned by the client
34      */

35     private int userInt;
36
37     /**
38      * Special bits in the comparison flag to indicate the type of change
39      */

40     public final static int K_ADDED = 1;
41     public final static int K_REMOVED = 2;
42     public final static int K_CHANGED = 4;
43
44     NodeComparison(Object JavaDoc oldData, Object JavaDoc newData, int realComparison, int userComparison) {
45         this.oldData = oldData;
46         this.newData = newData;
47         this.comparison = realComparison;
48         this.userInt = userComparison;
49     }
50
51     /**
52      * Reverse the nature of the comparison.
53      */

54     NodeComparison asReverseComparison(IComparator comparator) {
55         /* switch the data */
56         Object JavaDoc tempData = oldData;
57         oldData = newData;
58         newData = tempData;
59
60         /* re-calculate user comparison */
61         userInt = comparator.compare(oldData, newData);
62
63         if (comparison == K_ADDED) {
64             comparison = K_REMOVED;
65         } else {
66             if (comparison == K_REMOVED) {
67                 comparison = K_ADDED;
68             }
69         }
70         return this;
71     }
72
73     /**
74      * Returns an integer describing the changes between the two data objects.
75      * The four possible values are K_ADDED, K_REMOVED, K_CHANGED, or 0 representing
76      * no change.
77      */

78     public int getComparison() {
79         return comparison;
80     }
81
82     /**
83      * Returns the data of the new node.
84      */

85     public Object JavaDoc getNewData() {
86         return newData;
87     }
88
89     /**
90      * Returns the data of the old node.
91      */

92     public Object JavaDoc getOldData() {
93         return oldData;
94     }
95
96     /**
97      * Returns the client specified integer
98      */

99     public int getUserComparison() {
100         return userInt;
101     }
102
103     /**
104      * Returns true if this comparison has no change, and false otherwise.
105      */

106     boolean isUnchanged() {
107         return userInt == 0;
108     }
109
110     /**
111      * For debugging
112      */

113     public String JavaDoc toString() {
114         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("NodeComparison("); //$NON-NLS-1$
115
switch (comparison) {
116             case K_ADDED :
117                 buf.append("Added, "); //$NON-NLS-1$
118
break;
119             case K_REMOVED :
120                 buf.append("Removed, "); //$NON-NLS-1$
121
break;
122             case K_CHANGED :
123                 buf.append("Changed, "); //$NON-NLS-1$
124
break;
125             case 0 :
126                 buf.append("No change, "); //$NON-NLS-1$
127
break;
128             default :
129                 buf.append("Corrupt(" + comparison + "), "); //$NON-NLS-1$ //$NON-NLS-2$
130
}
131         buf.append(userInt);
132         buf.append(")"); //$NON-NLS-1$
133
return buf.toString();
134     }
135 }
136
Popular Tags