001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.compare.rangedifferencer;
011:
012: /**
013: * Description of a change between two or three ranges of comparable entities.
014: * <p>
015: * <code>RangeDifference</code> objects are the elements of a compare result returned from
016: * the <code>RangeDifferencer</code> <code>find* </code> methods.
017: * Clients use these objects as they are returned from the differencer.
018: * This class is not intended to be instantiated or subclassed outside of the Compare framework.
019: * <p>
020: * Note: A range in the <code>RangeDifference</code> object is given as a start index
021: * and length in terms of comparable entities. However, these entity indices and counts
022: * are not necessarily character positions. For example, if an entity represents a line
023: * in a document, the start index would be a line number and the count would be in lines.
024: * </p>
025: *
026: * @see RangeDifferencer
027: */
028: public class RangeDifference {
029:
030: /** Two-way change constant indicating no change. */
031: public final static int NOCHANGE = 0;
032: /** Two-way change constant indicating two-way change (same as <code>RIGHT</code>) */
033: public final static int CHANGE = 2;
034:
035: /** Three-way change constant indicating a change in both right and left. */
036: public final static int CONFLICT = 1;
037: /** Three-way change constant indicating a change in right. */
038: public final static int RIGHT = 2;
039: /** Three-way change constant indicating a change in left. */
040: public final static int LEFT = 3;
041: /**
042: * Three-way change constant indicating the same change in both right and left,
043: * that is only the ancestor is different.
044: */
045: public final static int ANCESTOR = 4;
046:
047: /** Constant indicating an unknown change kind. */
048: public final static int ERROR = 5;
049:
050: /** the kind of change: NOCHANGE, CHANGE, LEFT, RIGHT, ANCESTOR, CONFLICT, ERROR */
051: int fKind;
052:
053: int fLeftStart;
054: int fLeftLength;
055: int fRightStart;
056: int fRightLength;
057: int lAncestorStart;
058: int lAncestorLength;
059:
060: /**
061: * Creates a new range difference with the given change kind.
062: *
063: * @param changeKind the kind of change
064: */
065: /* package */RangeDifference(int changeKind) {
066: fKind = changeKind;
067: }
068:
069: /**
070: * Creates a new <code>RangeDifference</code> with the given change kind
071: * and left and right ranges.
072: *
073: * @param kind the kind of change
074: * @param rightStart start index of entity on right side
075: * @param rightLength number of entities on right side
076: * @param leftStart start index of entity on left side
077: * @param leftLength number of entities on left side
078: */
079: public RangeDifference(int kind, int rightStart, int rightLength,
080: int leftStart, int leftLength) {
081: fKind = kind;
082: fRightStart = rightStart;
083: fRightLength = rightLength;
084: fLeftStart = leftStart;
085: fLeftLength = leftLength;
086: }
087:
088: /**
089: * Creates a new <code>RangeDifference</code> with the given change kind
090: * and left, right, and ancestor ranges.
091: *
092: * @param kind the kind of change
093: * @param rightStart start index of entity on right side
094: * @param rightLength number of entities on right side
095: * @param leftStart start index of entity on left side
096: * @param leftLength number of entities on left side
097: * @param ancestorStart start index of entity on ancestor side
098: * @param ancestorLength number of entities on ancestor side
099: */
100: /* package */RangeDifference(int kind, int rightStart,
101: int rightLength, int leftStart, int leftLength,
102: int ancestorStart, int ancestorLength) {
103: this (kind, rightStart, rightLength, leftStart, leftLength);
104: lAncestorStart = ancestorStart;
105: lAncestorLength = ancestorLength;
106: }
107:
108: /**
109: * Returns the kind of difference.
110: *
111: * @return the kind of difference, one of
112: * <code>NOCHANGE</code>, <code>CHANGE</code>, <code>LEFT</code>, <code>RIGHT</code>,
113: * <code>ANCESTOR</code>, <code>CONFLICT</code>, <code>ERROR</code>
114: */
115: public int kind() {
116: return fKind;
117: }
118:
119: /**
120: * Returns the start index of the entity range on the ancestor side.
121: *
122: * @return the start index of the entity range on the ancestor side
123: */
124: public int ancestorStart() {
125: return lAncestorStart;
126: }
127:
128: /**
129: * Returns the number of entities on the ancestor side.
130: *
131: * @return the number of entities on the ancestor side
132: */
133: public int ancestorLength() {
134: return lAncestorLength;
135: }
136:
137: /**
138: * Returns the end index of the entity range on the ancestor side.
139: *
140: * @return the end index of the entity range on the ancestor side
141: */
142: public int ancestorEnd() {
143: return lAncestorStart + lAncestorLength;
144: }
145:
146: /**
147: * Returns the start index of the entity range on the right side.
148: *
149: * @return the start index of the entity range on the right side
150: */
151: public int rightStart() {
152: return fRightStart;
153: }
154:
155: /**
156: * Returns the number of entities on the right side.
157: *
158: * @return the number of entities on the right side
159: */
160: public int rightLength() {
161: return fRightLength;
162: }
163:
164: /**
165: * Returns the end index of the entity range on the right side.
166: *
167: * @return the end index of the entity range on the right side
168: */
169: public int rightEnd() {
170: return fRightStart + fRightLength;
171: }
172:
173: /**
174: * Returns the start index of the entity range on the left side.
175: *
176: * @return the start index of the entity range on the left side
177: */
178: public int leftStart() {
179: return fLeftStart;
180: }
181:
182: /**
183: * Returns the number of entities on the left side.
184: *
185: * @return the number of entities on the left side
186: */
187: public int leftLength() {
188: return fLeftLength;
189: }
190:
191: /**
192: * Returns the end index of the entity range on the left side.
193: *
194: * @return the end index of the entity range on the left side
195: */
196: public int leftEnd() {
197: return fLeftStart + fLeftLength;
198: }
199:
200: /**
201: * Returns the maximum number of entities in the left, right, and ancestor sides of this range.
202: *
203: * @return the maximum number of entities in the left, right, and ancestor sides of this range
204: */
205: public int maxLength() {
206: return Math.max(fRightLength, Math.max(fLeftLength,
207: lAncestorLength));
208: }
209:
210: public boolean equals(Object obj) {
211: if (obj instanceof RangeDifference) {
212: RangeDifference other = (RangeDifference) obj;
213: return fKind == other.fKind
214: && fLeftStart == other.fLeftStart
215: && fLeftLength == other.fLeftLength
216: && fRightStart == other.fRightStart
217: && fRightLength == other.fRightLength
218: && lAncestorStart == other.lAncestorStart
219: && lAncestorLength == other.lAncestorLength;
220: }
221: return super .equals(obj);
222: }
223:
224: public String toString() {
225: String string = "Left: " + toRangeString(fLeftStart, fLeftLength) + " Right: " + toRangeString(fRightStart, fRightLength); //$NON-NLS-1$ //$NON-NLS-2$
226: if (lAncestorLength > 0 || lAncestorStart > 0)
227: string += " Ancestor: " + toRangeString(lAncestorStart, lAncestorLength); //$NON-NLS-1$
228: return string;
229: }
230:
231: private String toRangeString(int start, int length) {
232: return "(" + start + ", " + length + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
233: }
234: }
|