001: package org.incava.util.diff;
002:
003: /**
004: * Represents a difference, as used in <code>Diff</code>. A difference consists
005: * of two pairs of starting and ending points, each pair representing either the
006: * "from" or the "to" collection passed to <code>Diff</code>. If an ending point
007: * is -1, then the difference was either a deletion or an addition. For example,
008: * if <code>getDeletedEnd()</code> returns -1, then the difference represents an
009: * addition.
010: */
011: public class Difference {
012: public static final int NONE = -1;
013:
014: /**
015: * The point at which the deletion starts.
016: */
017: private int delStart = NONE;
018:
019: /**
020: * The point at which the deletion ends.
021: */
022: private int delEnd = NONE;
023:
024: /**
025: * The point at which the addition starts.
026: */
027: private int addStart = NONE;
028:
029: /**
030: * The point at which the addition ends.
031: */
032: private int addEnd = NONE;
033:
034: /**
035: * Creates the difference for the given start and end points for the
036: * deletion and addition.
037: */
038: public Difference(int delStart, int delEnd, int addStart, int addEnd) {
039: this .delStart = delStart;
040: this .delEnd = delEnd;
041: this .addStart = addStart;
042: this .addEnd = addEnd;
043: }
044:
045: /**
046: * The point at which the deletion starts, if any. A value equal to
047: * <code>NONE</code> means this is an addition.
048: */
049: public int getDeletedStart() {
050: return delStart;
051: }
052:
053: /**
054: * The point at which the deletion ends, if any. A value equal to
055: * <code>NONE</code> means this is an addition.
056: */
057: public int getDeletedEnd() {
058: return delEnd;
059: }
060:
061: /**
062: * The point at which the addition starts, if any. A value equal to
063: * <code>NONE</code> means this must be an addition.
064: */
065: public int getAddedStart() {
066: return addStart;
067: }
068:
069: /**
070: * The point at which the addition ends, if any. A value equal to
071: * <code>NONE</code> means this must be an addition.
072: */
073: public int getAddedEnd() {
074: return addEnd;
075: }
076:
077: /**
078: * Sets the point as deleted. The start and end points will be modified to
079: * include the given line.
080: */
081: public void setDeleted(int line) {
082: delStart = Math.min(line, delStart);
083: delEnd = Math.max(line, delEnd);
084: }
085:
086: /**
087: * Sets the point as added. The start and end points will be modified to
088: * include the given line.
089: */
090: public void setAdded(int line) {
091: addStart = Math.min(line, addStart);
092: addEnd = Math.max(line, addEnd);
093: }
094:
095: /**
096: * Compares this object to the other for equality. Both objects must be of
097: * type Difference, with the same starting and ending points.
098: */
099: public boolean equals(Object obj) {
100: if (obj instanceof Difference) {
101: Difference other = (Difference) obj;
102:
103: return (delStart == other.delStart
104: && delEnd == other.delEnd
105: && addStart == other.addStart && addEnd == other.addEnd);
106: } else {
107: return false;
108: }
109: }
110:
111: /**
112: * Returns a string representation of this difference.
113: */
114: public String toString() {
115: StringBuffer buf = new StringBuffer();
116: buf.append("del: [" + delStart + ", " + delEnd + "]");
117: buf.append(" ");
118: buf.append("add: [" + addStart + ", " + addEnd + "]");
119: return buf.toString();
120: }
121:
122: }
|