comparison src/fschmidt/util/diff/Difference.java @ 68:00520880ad02

add fschmidt source
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 05 Oct 2025 17:24:15 -0600
parents
children
comparison
equal deleted inserted replaced
67:9d0fefce6985 68:00520880ad02
1 /*
2 Copyright (c) 2008 Franklin Schmidt <fschmidt@gmail.com>
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22
23 package fschmidt.util.diff;
24
25 /*
26 taken from http://www.incava.org/projects/java-diff/
27 */
28
29 /**
30 * Represents a difference, as used in <code>Diff</code>. A difference consists
31 * of two pairs of starting and ending points, each pair representing either the
32 * "from" or the "to" collection passed to <code>Diff</code>. If an ending point
33 * is -1, then the difference was either a deletion or an addition. For example,
34 * if <code>getDeletedEnd()</code> returns -1, then the difference represents an
35 * addition.
36 */
37 public class Difference
38 {
39 public static final int NONE = -1;
40
41 /**
42 * The point at which the deletion starts.
43 */
44 private int delStart = NONE;
45
46 /**
47 * The point at which the deletion ends.
48 */
49 private int delEnd = NONE;
50
51 /**
52 * The point at which the addition starts.
53 */
54 private int addStart = NONE;
55
56 /**
57 * The point at which the addition ends.
58 */
59 private int addEnd = NONE;
60
61 /**
62 * Creates the difference for the given start and end points for the
63 * deletion and addition.
64 */
65 public Difference(int delStart, int delEnd, int addStart, int addEnd)
66 {
67 this.delStart = delStart;
68 this.delEnd = delEnd;
69 this.addStart = addStart;
70 this.addEnd = addEnd;
71 }
72
73 /**
74 * The point at which the deletion starts, if any. A value equal to
75 * <code>NONE</code> means this is an addition.
76 */
77 public int getDeletedStart()
78 {
79 return delStart;
80 }
81
82 /**
83 * The point at which the deletion ends, if any. A value equal to
84 * <code>NONE</code> means this is an addition.
85 */
86 public int getDeletedEnd()
87 {
88 return delEnd;
89 }
90
91 /**
92 * The point at which the addition starts, if any. A value equal to
93 * <code>NONE</code> means this must be an addition.
94 */
95 public int getAddedStart()
96 {
97 return addStart;
98 }
99
100 /**
101 * The point at which the addition ends, if any. A value equal to
102 * <code>NONE</code> means this must be an addition.
103 */
104 public int getAddedEnd()
105 {
106 return addEnd;
107 }
108
109 /**
110 * Sets the point as deleted. The start and end points will be modified to
111 * include the given line.
112 */
113 public void setDeleted(int line)
114 {
115 delStart = Math.min(line, delStart);
116 delEnd = Math.max(line, delEnd);
117 }
118
119 /**
120 * Sets the point as added. The start and end points will be modified to
121 * include the given line.
122 */
123 public void setAdded(int line)
124 {
125 addStart = Math.min(line, addStart);
126 addEnd = Math.max(line, addEnd);
127 }
128
129 /**
130 * Compares this object to the other for equality. Both objects must be of
131 * type Difference, with the same starting and ending points.
132 */
133 public boolean equals(Object obj)
134 {
135 if (obj instanceof Difference) {
136 Difference other = (Difference)obj;
137
138 return (delStart == other.delStart &&
139 delEnd == other.delEnd &&
140 addStart == other.addStart &&
141 addEnd == other.addEnd);
142 }
143 else {
144 return false;
145 }
146 }
147
148 /**
149 * Returns a string representation of this difference.
150 */
151 public String toString()
152 {
153 StringBuffer buf = new StringBuffer();
154 buf.append("del: [" + delStart + ", " + delEnd + "]");
155 buf.append(" ");
156 buf.append("add: [" + addStart + ", " + addEnd + "]");
157 return buf.toString();
158 }
159
160 }