KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > texteditor > quickdiff > 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.ui.internal.texteditor.quickdiff.compare.rangedifferencer;
12
13 import java.util.List JavaDoc;
14
15 import org.eclipse.core.runtime.Assert;
16
17 import org.eclipse.jface.text.IDocument;
18
19 import org.eclipse.ui.internal.texteditor.quickdiff.DiffRegion;
20
21 /**
22  * Description of a change between two or three ranges of comparable entities.
23  * <p>
24  * <code>RangeDifference</code> objects are the elements of a compare result returned from
25  * the <code>RangeDifferencer</code> <code>find* </code> methods.
26  * Clients use these objects as they are returned from the differencer.
27  * This class is not intended to be instantiated or subclassed.
28  * <p>
29  * Note: A range in the <code>RangeDifference</code> object is given as a start index
30  * and length in terms of comparable entities. However, these entity indices and counts
31  * are not necessarily character positions. For example, if an entity represents a line
32  * in a document, the start index would be a line number and the count would be in lines.
33  * </p>
34  *
35  * @see RangeDifferencer
36  * @since 3.0
37  */

38 public class RangeDifference {
39
40     /** Two-way change constant indicating no change. */
41     public final static int NOCHANGE= 0;
42     /** Two-way change constant indicating two-way change (same as <code>RIGHT</code>) */
43     public final static int CHANGE= 2;
44
45     /** Three-way change constant indicating a change in both right and left. */
46     public final static int CONFLICT= 1;
47     /** Three-way change constant indicating a change in right. */
48     public final static int RIGHT= 2;
49     /** Three-way change constant indicating a change in left. */
50     public final static int LEFT= 3;
51     /**
52      * Three-way change constant indicating the same change in both right and left,
53      * that is only the ancestor is different.
54      */

55     public final static int ANCESTOR= 4;
56
57     /** Constant indicating an unknown change kind. */
58     public final static int ERROR= 5;
59
60     /** the kind of change: NOCHANGE, CHANGE, LEFT, RIGHT, ANCESTOR, CONFLICT, ERROR */
61     final int fKind;
62
63     int fLeftStart;
64     int fLeftLength;
65     int fRightStart;
66     int fRightLength;
67     int lAncestorStart;
68     int lAncestorLength;
69     private DiffRegion fRegion;
70
71     /**
72      * Creates a new range difference with the given change kind.
73      *
74      * @param changeKind the kind of change
75      */

76     public RangeDifference(int changeKind) {
77         fKind= changeKind;
78     }
79
80     /**
81      * Creates a new <code>RangeDifference</code> with the given change kind
82      * and left and right ranges.
83      *
84      * @param kind the kind of change
85      * @param rightStart start index of entity on right side
86      * @param rightLength number of entities on right side
87      * @param leftStart start index of entity on left side
88      * @param leftLength number of entities on left side
89      */

90     public RangeDifference(int kind, int rightStart, int rightLength, int leftStart, int leftLength) {
91         fKind= kind;
92         fRightStart= rightStart;
93         fRightLength= rightLength;
94         fLeftStart= leftStart;
95         fLeftLength= leftLength;
96     }
97
98     /**
99      * Creates a new <code>RangeDifference</code> with the given change kind
100      * and left, right, and ancestor ranges.
101      *
102      * @param kind the kind of change
103      * @param rightStart start index of entity on right side
104      * @param rightLength number of entities on right side
105      * @param leftStart start index of entity on left side
106      * @param leftLength number of entities on left side
107      * @param ancestorStart start index of entity on ancestor side
108      * @param ancestorLength number of entities on ancestor side
109      */

110     public RangeDifference(int kind, int rightStart, int rightLength, int leftStart, int leftLength,
111                                     int ancestorStart, int ancestorLength) {
112         this(kind, rightStart, rightLength, leftStart, leftLength);
113         lAncestorStart= ancestorStart;
114         lAncestorLength= ancestorLength;
115     }
116
117     /**
118      * Returns the kind of difference.
119      *
120      * @return the kind of difference, one of
121      * <code>NOCHANGE</code>, <code>CHANGE</code>, <code>LEFT</code>, <code>RIGHT</code>,
122      * <code>ANCESTOR</code>, <code>CONFLICT</code>, <code>ERROR</code>
123      */

124     public int kind() {
125         return fKind;
126     }
127
128     /**
129      * Returns the start index of the entity range on the ancestor side.
130      *
131      * @return the start index of the entity range on the ancestor side
132      */

133     public int ancestorStart() {
134         return lAncestorStart;
135     }
136
137     /**
138      * Returns the number of entities on the ancestor side.
139      *
140      * @return the number of entities on the ancestor side
141      */

142     public int ancestorLength() {
143         return lAncestorLength;
144     }
145
146     /**
147      * Returns the end index of the entity range on the ancestor side.
148      *
149      * @return the end index of the entity range on the ancestor side
150      */

151     public int ancestorEnd() {
152         return lAncestorStart + lAncestorLength;
153     }
154
155     /**
156      * Returns the start index of the entity range on the right side.
157      *
158      * @return the start index of the entity range on the right side
159      */

160     public int rightStart() {
161         return fRightStart;
162     }
163
164     /**
165      * Returns the number of entities on the right side.
166      *
167      * @return the number of entities on the right side
168      */

169     public int rightLength() {
170         return fRightLength;
171     }
172
173     /**
174      * Returns the end index of the entity range on the right side.
175      *
176      * @return the end index of the entity range on the right side
177      */

178     public int rightEnd() {
179         return fRightStart + fRightLength;
180     }
181
182     /**
183      * Returns the start index of the entity range on the left side.
184      *
185      * @return the start index of the entity range on the left side
186      */

187     public int leftStart() {
188         return fLeftStart;
189     }
190
191     /**
192      * Returns the number of entities on the left side.
193      *
194      * @return the number of entities on the left side
195      */

196     public int leftLength() {
197         return fLeftLength;
198     }
199
200     /**
201      * Returns the end index of the entity range on the left side.
202      *
203      * @return the end index of the entity range on the left side
204      */

205     public int leftEnd() {
206         return fLeftStart + fLeftLength;
207     }
208
209     /**
210      * Returns the maximum number of entities in the left, right, and ancestor sides of this range.
211      *
212      * @return the maximum number of entities in the left, right, and ancestor sides of this range
213      */

214     public int maxLength() {
215         return Math.max(fRightLength, Math.max(fLeftLength, lAncestorLength));
216     }
217
218     /**
219      * Shifts the offset into the left document of the receiver.
220      *
221      * @param shift the number of elements to shift
222      */

223     public void shiftLeft(int shift) {
224         Assert.isTrue(shift + fLeftStart >= 0);
225         fLeftStart += shift;
226     }
227
228     /**
229      * Shifts the offset into the right document of the receiver.
230      *
231      * @param shift the number of elements to shift
232      */

233     public void shiftRight(int shift) {
234         Assert.isTrue(shift + fRightStart >= 0);
235         fRightStart += shift;
236     }
237
238     /**
239      * Resizes the receiver <code>shift</code> units, on both sides, by
240      * moving the start of the difference.
241      *
242      * @param shift the number of elements to shift
243      */

244     public void extendStart(int shift) {
245         Assert.isTrue(shift + fRightStart >= 0);
246         Assert.isTrue(shift + fLeftStart >= 0);
247         fRightStart += shift;
248         fRightLength -= shift;
249         fLeftStart += shift;
250         fLeftLength -= shift;
251     }
252
253     /**
254      * Resizes the receiver <code>shift</code> units, on both sides, by
255      * moving the end of the difference.
256      *
257      * @param shift the number of elements to shift
258      */

259     public void extendEnd(int shift) {
260         Assert.isTrue(shift + fRightLength >= 0);
261         Assert.isTrue(shift + fLeftLength >= 0);
262         fRightLength += shift;
263         fLeftLength += shift;
264     }
265
266     /*
267      * @see java.lang.Object#equals(java.lang.Object)
268      */

269     public boolean equals(Object JavaDoc obj) {
270         if (obj instanceof RangeDifference) {
271             RangeDifference d= (RangeDifference) obj;
272             return fKind == d.fKind && fRightStart == d.fRightStart && fRightLength == d.fRightLength && fLeftStart == d.fLeftStart && fLeftLength == d.fLeftLength;
273         }
274         return false;
275     }
276
277     /**
278      * Returns {@link Object#hashCode()}.
279      *
280      * @return the hash code which is {@link Object#hashCode()}
281      */

282     public final int hashCode() {
283         return super.hashCode();
284     }
285
286     /**
287      * Returns the diff region corresponding to this range difference.
288      *
289      * @param differences the list of differences around this one difference
290      * @param source the original document (left document) that this difference refers to
291      * @return a <code>DiffRegion</code> corresponding to this difference
292      */

293     public DiffRegion getDiffRegion(List JavaDoc differences, IDocument source) {
294         if (fRegion == null)
295             fRegion= new DiffRegion(this, 0, differences, source);
296         return fRegion;
297     }
298     
299     /*
300      * @since 3.2
301      */

302     public String JavaDoc toString() {
303         StringBuffer JavaDoc buf= new StringBuffer JavaDoc("RangeDifference {"); //$NON-NLS-1$
304
switch (fKind) {
305             case NOCHANGE:
306                 buf.append("NOCHANGE"); //$NON-NLS-1$
307
break;
308             case CHANGE:
309                 buf.append("CHANGE/RIGHT"); //$NON-NLS-1$
310
break;
311             case CONFLICT:
312                 buf.append("CONFLICT"); //$NON-NLS-1$
313
break;
314             case LEFT:
315                 buf.append("LEFT"); //$NON-NLS-1$
316
break;
317             case ERROR:
318                 buf.append("ERROR"); //$NON-NLS-1$
319
break;
320             case ANCESTOR:
321                 buf.append("ANCESTOR"); //$NON-NLS-1$
322
break;
323             default:
324                 break;
325         }
326         
327         buf.append(", Left: [" + fLeftStart + "+" + fLeftLength + ")"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
328
buf.append(", Right: [" + fRightStart + "+" + fRightLength + ")"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
329
buf.append("]"); //$NON-NLS-1$
330

331         return buf.toString();
332     }
333 }
334
335
Popular Tags