View Javadoc

1   package org.incava.util.diff;
2   
3   
4   /**
5    * Represents a difference, as used in <code>Diff</code>. A difference consists
6    * of two pairs of starting and ending points, each pair representing either the
7    * "from" or the "to" collection passed to <code>Diff</code>. If an ending point
8    * is -1, then the difference was either a deletion or an addition. For example,
9    * if <code>getDeletedEnd()</code> returns -1, then the difference represents an
10   * addition.
11   */
12  public class Difference
13  {
14      public static final int NONE = -1;
15      
16      /**
17       * The point at which the deletion starts.
18       */
19      private int delStart = NONE;
20  
21      /**
22       * The point at which the deletion ends.
23       */
24      private int delEnd = NONE;
25  
26      /**
27       * The point at which the addition starts.
28       */
29      private int addStart = NONE;
30  
31      /**
32       * The point at which the addition ends.
33       */
34      private int addEnd = NONE;
35  
36      /**
37       * Creates the difference for the given start and end points for the
38       * deletion and addition.
39       */
40      public Difference(int delStart, int delEnd, int addStart, int addEnd)
41      {
42          this.delStart = delStart;
43          this.delEnd   = delEnd;
44          this.addStart = addStart;
45          this.addEnd   = addEnd;
46      }
47  
48      /**
49       * The point at which the deletion starts, if any. A value equal to
50       * <code>NONE</code> means this is an addition.
51       */
52      public int getDeletedStart()
53      {
54          return delStart;
55      }
56  
57      /**
58       * The point at which the deletion ends, if any. A value equal to
59       * <code>NONE</code> means this is an addition.
60       */
61      public int getDeletedEnd()
62      {
63          return delEnd;
64      }
65  
66      /**
67       * The point at which the addition starts, if any. A value equal to
68       * <code>NONE</code> means this must be an addition.
69       */
70      public int getAddedStart()
71      {
72          return addStart;
73      }
74  
75      /**
76       * The point at which the addition ends, if any. A value equal to
77       * <code>NONE</code> means this must be an addition.
78       */
79      public int getAddedEnd()
80      {
81          return addEnd;
82      }
83  
84      /**
85       * Sets the point as deleted. The start and end points will be modified to
86       * include the given line.
87       */
88      public void setDeleted(int line)
89      {
90          delStart = Math.min(line, delStart);
91          delEnd   = Math.max(line, delEnd);
92      }
93  
94      /**
95       * Sets the point as added. The start and end points will be modified to
96       * include the given line.
97       */
98      public void setAdded(int line)
99      {
100         addStart = Math.min(line, addStart);
101         addEnd   = Math.max(line, addEnd);
102     }
103 
104     /**
105      * Compares this object to the other for equality. Both objects must be of
106      * type Difference, with the same starting and ending points.
107      */
108     public boolean equals(Object obj)
109     {
110         if (obj instanceof Difference) {
111             Difference other = (Difference)obj;
112 
113             return (delStart == other.delStart && 
114                     delEnd   == other.delEnd && 
115                     addStart == other.addStart && 
116                     addEnd   == other.addEnd);
117         }
118         else {
119             return false;
120         }
121     }
122 
123     /**
124      * Returns a string representation of this difference.
125      */
126     public String toString()
127     {
128         StringBuffer buf = new StringBuffer();
129         buf.append("del: [" + delStart + ", " + delEnd + "]");
130         buf.append(" ");
131         buf.append("add: [" + addStart + ", " + addEnd + "]");
132         return buf.toString();
133     }
134 
135 }