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 * Construct a new edit representing the region before cut. 174 * 175 * @param cut 176 * the cut point. The beginning A and B points are used as the 177 * end points of the returned edit. 178 * @return an edit representing the slice of {@code this} edit that occurs 179 * before {@code cut} starts. 180 */ 181 public final Edit before(Edit cut) { 182 return new Edit(beginA, cut.beginA, beginB, cut.beginB); 183 } 184 185 /** 186 * Construct a new edit representing the region after cut. 187 * 188 * @param cut 189 * the cut point. The ending A and B points are used as the 190 * starting points of the returned edit. 191 * @return an edit representing the slice of {@code this} edit that occurs 192 * after {@code cut} ends. 193 */ 194 public final Edit after(Edit cut) { 195 return new Edit(cut.endA, endA, cut.endB, endB); 196 } 197 198 /** Increase {@link #getEndA()} by 1. */ 199 public void extendA() { 200 endA++; 201 } 202 203 /** Increase {@link #getEndB()} by 1. */ 204 public void extendB() { 205 endB++; 206 } 207 208 /** Swap A and B, so the edit goes the other direction. */ 209 public void swap() { 210 final int sBegin = beginA; 211 final int sEnd = endA; 212 213 beginA = beginB; 214 endA = endB; 215 216 beginB = sBegin; 217 endB = sEnd; 218 } 219 220 @Override 221 public int hashCode() { 222 return beginA ^ endA; 223 } 224 225 @Override 226 public boolean equals(final Object o) { 227 if (o instanceof Edit) { 228 final Edit e = (Edit) o; 229 return this.beginA == e.beginA && this.endA == e.endA 230 && this.beginB == e.beginB && this.endB == e.endB; 231 } 232 return false; 233 } 234 235 @SuppressWarnings("nls") 236 @Override 237 public String toString() { 238 final Type t = getType(); 239 return t + "(" + beginA + "-" + endA + "," + beginB + "-" + endB + ")"; 240 } 241 }