KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > rangedifferencer > RangeDifference


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.compare.rangedifferencer;
12
13 /**
14  * Description of a change between two or three ranges of comparable entities.
15  * <p>
16  * <code>RangeDifference</code> objects are the elements of a compare result returned from
17  * the <code>RangeDifferencer</code> <code>find* </code> methods.
18  * Clients use these objects as they are returned from the differencer.
19  * This class is not intended to be instantiated or subclassed outside of the Compare framework.
20  * <p>
21  * Note: A range in the <code>RangeDifference</code> object is given as a start index
22  * and length in terms of comparable entities. However, these entity indices and counts
23  * are not necessarily character positions. For example, if an entity represents a line
24  * in a document, the start index would be a line number and the count would be in lines.
25  * </p>
26  *
27  * @see RangeDifferencer
28  */

29 public class RangeDifference {
30
31     /** Two-way change constant indicating no change. */
32     public final static int NOCHANGE= 0;
33     /** Two-way change constant indicating two-way change (same as <code>RIGHT</code>) */
34     public final static int CHANGE= 2;
35
36     /** Three-way change constant indicating a change in both right and left. */
37     public final static int CONFLICT= 1;
38     /** Three-way change constant indicating a change in right. */
39     public final static int RIGHT= 2;
40     /** Three-way change constant indicating a change in left. */
41     public final static int LEFT= 3;
42     /**
43      * Three-way change constant indicating the same change in both right and left,
44      * that is only the ancestor is different.
45      */

46     public final static int ANCESTOR= 4;
47     
48     /** Constant indicating an unknown change kind. */
49     public final static int ERROR= 5;
50
51     /** the kind of change: NOCHANGE, CHANGE, LEFT, RIGHT, ANCESTOR, CONFLICT, ERROR */
52     int fKind;
53
54     int fLeftStart;
55     int fLeftLength;
56     int fRightStart;
57     int fRightLength;
58     int lAncestorStart;
59     int lAncestorLength;
60     
61     /**
62      * Creates a new range difference with the given change kind.
63      *
64      * @param changeKind the kind of change
65      */

66     /* package */ RangeDifference(int changeKind) {
67         fKind= changeKind;
68     }
69
70     /**
71      * Creates a new <code>RangeDifference</code> with the given change kind
72      * and left and right ranges.
73      *
74      * @param kind the kind of change
75      * @param rightStart start index of entity on right side
76      * @param rightLength number of entities on right side
77      * @param leftStart start index of entity on left side
78      * @param leftLength number of entities on left side
79      */

80     /* package */ RangeDifference(int kind, int rightStart, int rightLength, int leftStart, int leftLength) {
81         fKind= kind;
82         fRightStart= rightStart;
83         fRightLength= rightLength;
84         fLeftStart= leftStart;
85         fLeftLength= leftLength;
86     }
87
88     /**
89      * Creates a new <code>RangeDifference</code> with the given change kind
90      * and left, right, and ancestor ranges.
91      *
92      * @param kind the kind of change
93      * @param rightStart start index of entity on right side
94      * @param rightLength number of entities on right side
95      * @param leftStart start index of entity on left side
96      * @param leftLength number of entities on left side
97      * @param ancestorStart start index of entity on ancestor side
98      * @param ancestorLength number of entities on ancestor side
99      */

100     /* package */ RangeDifference(int kind, int rightStart, int rightLength, int leftStart, int leftLength,
101                                     int ancestorStart, int ancestorLength) {
102         this(kind, rightStart, rightLength, leftStart, leftLength);
103         lAncestorStart= ancestorStart;
104         lAncestorLength= ancestorLength;
105     }
106
107     /**
108      * Returns the kind of difference.
109      *
110      * @return the kind of difference, one of
111      * <code>NOCHANGE</code>, <code>CHANGE</code>, <code>LEFT</code>, <code>RIGHT</code>,
112      * <code>ANCESTOR</code>, <code>CONFLICT</code>, <code>ERROR</code>
113      */

114     public int kind() {
115         return fKind;
116     }
117
118     /**
119      * Returns the start index of the entity range on the ancestor side.
120      *
121      * @return the start index of the entity range on the ancestor side
122      */

123     public int ancestorStart() {
124         return lAncestorStart;
125     }
126
127     /**
128      * Returns the number of entities on the ancestor side.
129      *
130      * @return the number of entities on the ancestor side
131      */

132     public int ancestorLength() {
133         return lAncestorLength;
134     }
135
136     /**
137      * Returns the end index of the entity range on the ancestor side.
138      *
139      * @return the end index of the entity range on the ancestor side
140      */

141     public int ancestorEnd() {
142         return lAncestorStart + lAncestorLength;
143     }
144
145     /**
146      * Returns the start index of the entity range on the right side.
147      *
148      * @return the start index of the entity range on the right side
149      */

150     public int rightStart() {
151         return fRightStart;
152     }
153
154     /**
155      * Returns the number of entities on the right side.
156      *
157      * @return the number of entities on the right side
158      */

159     public int rightLength() {
160         return fRightLength;
161     }
162
163     /**
164      * Returns the end index of the entity range on the right side.
165      *
166      * @return the end index of the entity range on the right side
167      */

168     public int rightEnd() {
169         return fRightStart + fRightLength;
170     }
171
172     /**
173      * Returns the start index of the entity range on the left side.
174      *
175      * @return the start index of the entity range on the left side
176      */

177     public int leftStart() {
178         return fLeftStart;
179     }
180
181     /**
182      * Returns the number of entities on the left side.
183      *
184      * @return the number of entities on the left side
185      */

186     public int leftLength() {
187         return fLeftLength;
188     }
189
190     /**
191      * Returns the end index of the entity range on the left side.
192      *
193      * @return the end index of the entity range on the left side
194      */

195     public int leftEnd() {
196         return fLeftStart + fLeftLength;
197     }
198
199     /**
200      * Returns the maximum number of entities in the left, right, and ancestor sides of this range.
201      *
202      * @return the maximum number of entities in the left, right, and ancestor sides of this range
203      */

204     public int maxLength() {
205         return Math.max(fRightLength, Math.max(fLeftLength, lAncestorLength));
206     }
207     
208     public boolean equals(Object JavaDoc obj) {
209         if (obj instanceof RangeDifference) {
210             RangeDifference other = (RangeDifference) obj;
211             return fKind == other.fKind
212                 && fLeftStart == other.fLeftStart
213                 && fLeftLength == other.fLeftLength
214                 && fRightStart == other.fRightStart
215                 && fRightLength == other.fRightLength
216                 && lAncestorStart == other.lAncestorStart
217                 && lAncestorLength == other.lAncestorLength;
218         }
219         return super.equals(obj);
220     }
221     
222     public String JavaDoc toString() {
223         String JavaDoc string = "Left: " + toRangeString(fLeftStart, fLeftLength) + " Right: " + toRangeString(fRightStart, fRightLength); //$NON-NLS-1$ //$NON-NLS-2$
224
if (lAncestorLength > 0 || lAncestorStart> 0)
225             string += " Ancestor: " + toRangeString(lAncestorStart, lAncestorLength); //$NON-NLS-1$
226
return string;
227     }
228
229     private String JavaDoc toRangeString(int start, int length) {
230         return "(" + start + ", " + length + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
231
}
232 }
233
234
Popular Tags