1 /*
2 * Copyright (C) 2008-2009, Johannes E. Schindelin <johannes.schindelin@gmx.de>
3 * and other copyright owners as documented in the project's IP log.
4 *
5 * This program and the accompanying materials are made available
6 * under the terms of the Eclipse Distribution License v1.0 which
7 * accompanies this distribution, is reproduced below, and is
8 * available at http://www.eclipse.org/org/documents/edl-v10.php
9 *
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials provided
22 * with the distribution.
23 *
24 * - Neither the name of the Eclipse Foundation, Inc. nor the
25 * names of its contributors may be used to endorse or promote
26 * products derived from this software without specific prior
27 * written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 */
43
44 package org.eclipse.jgit.diff;
45
46 /**
47 * A modified region detected between two versions of roughly the same content.
48 * <p>
49 * An edit covers the modified region only. It does not cover a common region.
50 * <p>
51 * Regions should be specified using 0 based notation, so add 1 to the start and
52 * end marks for line numbers in a file.
53 * <p>
54 * An edit where {@code beginA == endA && beginB < endB} is an insert edit, that
55 * is sequence B inserted the elements in region <code>[beginB, endB)</code> at
56 * <code>beginA</code>.
57 * <p>
58 * An edit where {@code beginA < endA && beginB == endB} is a delete edit, that
59 * is sequence B has removed the elements between <code>[beginA, endA)</code>.
60 * <p>
61 * An edit where {@code beginA < endA && beginB < endB} is a replace edit, that
62 * is sequence B has replaced the range of elements between
63 * <code>[beginA, endA)</code> with those found in <code>[beginB, endB)</code>.
64 */
65 public class Edit {
66 /** Type of edit */
67 public static enum Type {
68 /** Sequence B has inserted the region. */
69 INSERT,
70
71 /** Sequence B has removed the region. */
72 DELETE,
73
74 /** Sequence B has replaced the region with different content. */
75 REPLACE,
76
77 /** Sequence A and B have zero length, describing nothing. */
78 EMPTY;
79 }
80
81 int beginA;
82
83 int endA;
84
85 int beginB;
86
87 int endB;
88
89 /**
90 * Create a new empty edit.
91 *
92 * @param as
93 * beginA: start and end of region in sequence A; 0 based.
94 * @param bs
95 * beginB: start and end of region in sequence B; 0 based.
96 */
97 public Edit(int as, int bs) {
98 this(as, as, bs, bs);
99 }
100
101 /**
102 * Create a new edit.
103 *
104 * @param as
105 * beginA: start of region in sequence A; 0 based.
106 * @param ae
107 * endA: end of region in sequence A; must be >= as.
108 * @param bs
109 * beginB: start of region in sequence B; 0 based.
110 * @param be
111 * endB: end of region in sequence B; must be > = bs.
112 */
113 public Edit(int as, int ae, int bs, int be) {
114 beginA = as;
115 endA = ae;
116
117 beginB = bs;
118 endB = be;
119 }
120
121 /**
122 * Get type
123 *
124 * @return the type of this region
125 */
126 public final Type getType() {
127 if (beginA < endA) {
128 if (beginB < endB)
129 return Type.REPLACE;
130 else /* if (beginB == endB) */
131 return Type.DELETE;
132
133 } else /* if (beginA == endA) */{
134 if (beginB < endB)
135 return Type.INSERT;
136 else /* if (beginB == endB) */
137 return Type.EMPTY;
138 }
139 }
140
141 /**
142 * Whether edit is empty
143 *
144 * @return {@code true} if the edit is empty (lengths of both a and b is
145 * zero)
146 */
147 public final boolean isEmpty() {
148 return beginA == endA && beginB == endB;
149 }
150
151 /**
152 * Get start point in sequence A
153 *
154 * @return start point in sequence A
155 */
156 public final int getBeginA() {
157 return beginA;
158 }
159
160 /**
161 * Get end point in sequence A
162 *
163 * @return end point in sequence A
164 */
165 public final int getEndA() {
166 return endA;
167 }
168
169 /**
170 * Get start point in sequence B
171 *
172 * @return start point in sequence B
173 */
174 public final int getBeginB() {
175 return beginB;
176 }
177
178 /**
179 * Get end point in sequence B
180 *
181 * @return end point in sequence B
182 */
183 public final int getEndB() {
184 return endB;
185 }
186
187 /**
188 * Get length of the region in A
189 *
190 * @return length of the region in A
191 */
192 public final int getLengthA() {
193 return endA - beginA;
194 }
195
196 /**
197 * Get length of the region in B
198 *
199 * @return return length of the region in B
200 */
201 public final int getLengthB() {
202 return endB - beginB;
203 }
204
205 /**
206 * Move the edit region by the specified amount.
207 *
208 * @param amount
209 * the region is shifted by this amount, and can be positive or
210 * negative.
211 * @since 4.8
212 */
213 public final void shift(int amount) {
214 beginA += amount;
215 endA += amount;
216 beginB += amount;
217 endB += amount;
218 }
219
220 /**
221 * Construct a new edit representing the region before cut.
222 *
223 * @param cut
224 * the cut point. The beginning A and B points are used as the
225 * end points of the returned edit.
226 * @return an edit representing the slice of {@code this} edit that occurs
227 * before {@code cut} starts.
228 */
229 public final Edit before(Edit cut) {
230 return new Edit(beginA, cut.beginA, beginB, cut.beginB);
231 }
232
233 /**
234 * Construct a new edit representing the region after cut.
235 *
236 * @param cut
237 * the cut point. The ending A and B points are used as the
238 * starting points of the returned edit.
239 * @return an edit representing the slice of {@code this} edit that occurs
240 * after {@code cut} ends.
241 */
242 public final Edit after(Edit cut) {
243 return new Edit(cut.endA, endA, cut.endB, endB);
244 }
245
246 /**
247 * Increase {@link #getEndA()} by 1.
248 */
249 public void extendA() {
250 endA++;
251 }
252
253 /**
254 * Increase {@link #getEndB()} by 1.
255 */
256 public void extendB() {
257 endB++;
258 }
259
260 /**
261 * Swap A and B, so the edit goes the other direction.
262 */
263 public void swap() {
264 final int sBegin = beginA;
265 final int sEnd = endA;
266
267 beginA = beginB;
268 endA = endB;
269
270 beginB = sBegin;
271 endB = sEnd;
272 }
273
274 /** {@inheritDoc} */
275 @Override
276 public int hashCode() {
277 return beginA ^ endA;
278 }
279
280 /** {@inheritDoc} */
281 @Override
282 public boolean equals(Object o) {
283 if (o instanceof Edit) {
284 final Edit e = (Edit) o;
285 return this.beginA == e.beginA && this.endA == e.endA
286 && this.beginB == e.beginB && this.endB == e.endB;
287 }
288 return false;
289 }
290
291 /** {@inheritDoc} */
292 @SuppressWarnings("nls")
293 @Override
294 public String toString() {
295 final Type t = getType();
296 return t + "(" + beginA + "-" + endA + "," + beginB + "-" + endB + ")";
297 }
298 }