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(final int as, final 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(final int as, final int ae, final int bs, final int be) { 114 beginA = as; 115 endA = ae; 116 117 beginB = bs; 118 endB = be; 119 } 120 121 /** @return the type of this region */ 122 public final Type getType() { 123 if (beginA < endA) { 124 if (beginB < endB) 125 return Type.REPLACE; 126 else /* if (beginB == endB) */ 127 return Type.DELETE; 128 129 } else /* if (beginA == endA) */{ 130 if (beginB < endB) 131 return Type.INSERT; 132 else /* if (beginB == endB) */ 133 return Type.EMPTY; 134 } 135 } 136 137 /** @return true if the edit is empty (lengths of both a and b is zero). */ 138 public final boolean isEmpty() { 139 return beginA == endA && beginB == endB; 140 } 141 142 /** @return start point in sequence A. */ 143 public final int getBeginA() { 144 return beginA; 145 } 146 147 /** @return end point in sequence A. */ 148 public final int getEndA() { 149 return endA; 150 } 151 152 /** @return start point in sequence B. */ 153 public final int getBeginB() { 154 return beginB; 155 } 156 157 /** @return end point in sequence B. */ 158 public final int getEndB() { 159 return endB; 160 } 161 162 /** @return length of the region in A. */ 163 public final int getLengthA() { 164 return endA - beginA; 165 } 166 167 /** @return length of the region in B. */ 168 public final int getLengthB() { 169 return endB - beginB; 170 } 171 172 /** 173 * Move the edit region by the specified amount. 174 * 175 * @param amount 176 * the region is shifted by this amount, and can be positive or 177 * negative. 178 * @since 4.8 179 */ 180 public final void shift(int amount) { 181 beginA += amount; 182 endA += amount; 183 beginB += amount; 184 endB += amount; 185 } 186 187 /** 188 * Construct a new edit representing the region before cut. 189 * 190 * @param cut 191 * the cut point. The beginning A and B points are used as the 192 * end points of the returned edit. 193 * @return an edit representing the slice of {@code this} edit that occurs 194 * before {@code cut} starts. 195 */ 196 public final Edit before(Edit cut) { 197 return new Edit(beginA, cut.beginA, beginB, cut.beginB); 198 } 199 200 /** 201 * Construct a new edit representing the region after cut. 202 * 203 * @param cut 204 * the cut point. The ending A and B points are used as the 205 * starting points of the returned edit. 206 * @return an edit representing the slice of {@code this} edit that occurs 207 * after {@code cut} ends. 208 */ 209 public final Edit after(Edit cut) { 210 return new Edit(cut.endA, endA, cut.endB, endB); 211 } 212 213 /** Increase {@link #getEndA()} by 1. */ 214 public void extendA() { 215 endA++; 216 } 217 218 /** Increase {@link #getEndB()} by 1. */ 219 public void extendB() { 220 endB++; 221 } 222 223 /** Swap A and B, so the edit goes the other direction. */ 224 public void swap() { 225 final int sBegin = beginA; 226 final int sEnd = endA; 227 228 beginA = beginB; 229 endA = endB; 230 231 beginB = sBegin; 232 endB = sEnd; 233 } 234 235 @Override 236 public int hashCode() { 237 return beginA ^ endA; 238 } 239 240 @Override 241 public boolean equals(final Object o) { 242 if (o instanceof Edit) { 243 final Edit e = (Edit) o; 244 return this.beginA == e.beginA && this.endA == e.endA 245 && this.beginB == e.beginB && this.endB == e.endB; 246 } 247 return false; 248 } 249 250 @SuppressWarnings("nls") 251 @Override 252 public String toString() { 253 final Type t = getType(); 254 return t + "(" + beginA + "-" + endA + "," + beginB + "-" + endB + ")"; 255 } 256 }