You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Edit.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. package org.eclipse.jgit.diff;
  44. /**
  45. * A modified region detected between two versions of roughly the same content.
  46. * <p>
  47. * An edit covers the modified region only. It does not cover a common region.
  48. * <p>
  49. * Regions should be specified using 0 based notation, so add 1 to the start and
  50. * end marks for line numbers in a file.
  51. * <p>
  52. * An edit where <code>beginA == endA && beginB < endB</code> is an insert edit,
  53. * that is sequence B inserted the elements in region
  54. * <code>[beginB, endB)</code> at <code>beginA</code>.
  55. * <p>
  56. * An edit where <code>beginA < endA && beginB == endB</code> is a delete edit,
  57. * that is sequence B has removed the elements between
  58. * <code>[beginA, endA)</code>.
  59. * <p>
  60. * An edit where <code>beginA < endA && beginB < endB</code> is a replace edit,
  61. * that is sequence B has replaced the range of elements between
  62. * <code>[beginA, endA)</code> with those found in <code>[beginB, endB)</code>.
  63. */
  64. public class Edit {
  65. /** Type of edit */
  66. public static enum Type {
  67. /** Sequence B has inserted the region. */
  68. INSERT,
  69. /** Sequence B has removed the region. */
  70. DELETE,
  71. /** Sequence B has replaced the region with different content. */
  72. REPLACE,
  73. /** Sequence A and B have zero length, describing nothing. */
  74. EMPTY;
  75. }
  76. int beginA;
  77. int endA;
  78. int beginB;
  79. int endB;
  80. /**
  81. * Create a new empty edit.
  82. *
  83. * @param as
  84. * beginA: start and end of region in sequence A; 0 based.
  85. * @param bs
  86. * beginB: start and end of region in sequence B; 0 based.
  87. */
  88. public Edit(final int as, final int bs) {
  89. this(as, as, bs, bs);
  90. }
  91. /**
  92. * Create a new edit.
  93. *
  94. * @param as
  95. * beginA: start of region in sequence A; 0 based.
  96. * @param ae
  97. * endA: end of region in sequence A; must be >= as.
  98. * @param bs
  99. * beginB: start of region in sequence B; 0 based.
  100. * @param be
  101. * endB: end of region in sequence B; must be >= bs.
  102. */
  103. public Edit(final int as, final int ae, final int bs, final int be) {
  104. beginA = as;
  105. endA = ae;
  106. beginB = bs;
  107. endB = be;
  108. }
  109. /** @return the type of this region */
  110. public final Type getType() {
  111. if (beginA < endA) {
  112. if (beginB < endB)
  113. return Type.REPLACE;
  114. else /* if (beginB == endB) */
  115. return Type.DELETE;
  116. } else /* if (beginA == endA) */{
  117. if (beginB < endB)
  118. return Type.INSERT;
  119. else /* if (beginB == endB) */
  120. return Type.EMPTY;
  121. }
  122. }
  123. /** @return true if the edit is empty (lengths of both a and b is zero). */
  124. public final boolean isEmpty() {
  125. return beginA == endA && beginB == endB;
  126. }
  127. /** @return start point in sequence A. */
  128. public final int getBeginA() {
  129. return beginA;
  130. }
  131. /** @return end point in sequence A. */
  132. public final int getEndA() {
  133. return endA;
  134. }
  135. /** @return start point in sequence B. */
  136. public final int getBeginB() {
  137. return beginB;
  138. }
  139. /** @return end point in sequence B. */
  140. public final int getEndB() {
  141. return endB;
  142. }
  143. /** @return length of the region in A. */
  144. public final int getLengthA() {
  145. return endA - beginA;
  146. }
  147. /** @return length of the region in B. */
  148. public final int getLengthB() {
  149. return endB - beginB;
  150. }
  151. /**
  152. * Construct a new edit representing the region before cut.
  153. *
  154. * @param cut
  155. * the cut point. The beginning A and B points are used as the
  156. * end points of the returned edit.
  157. * @return an edit representing the slice of {@code this} edit that occurs
  158. * before {@code cut} starts.
  159. */
  160. public final Edit before(Edit cut) {
  161. return new Edit(beginA, cut.beginA, beginB, cut.beginB);
  162. }
  163. /**
  164. * Construct a new edit representing the region after cut.
  165. *
  166. * @param cut
  167. * the cut point. The ending A and B points are used as the
  168. * starting points of the returned edit.
  169. * @return an edit representing the slice of {@code this} edit that occurs
  170. * after {@code cut} ends.
  171. */
  172. public final Edit after(Edit cut) {
  173. return new Edit(cut.endA, endA, cut.endB, endB);
  174. }
  175. /** Increase {@link #getEndA()} by 1. */
  176. public void extendA() {
  177. endA++;
  178. }
  179. /** Increase {@link #getEndB()} by 1. */
  180. public void extendB() {
  181. endB++;
  182. }
  183. /** Swap A and B, so the edit goes the other direction. */
  184. public void swap() {
  185. final int sBegin = beginA;
  186. final int sEnd = endA;
  187. beginA = beginB;
  188. endA = endB;
  189. beginB = sBegin;
  190. endB = sEnd;
  191. }
  192. @Override
  193. public int hashCode() {
  194. return beginA ^ endA;
  195. }
  196. @Override
  197. public boolean equals(final Object o) {
  198. if (o instanceof Edit) {
  199. final Edit e = (Edit) o;
  200. return this.beginA == e.beginA && this.endA == e.endA
  201. && this.beginB == e.beginB && this.endB == e.endB;
  202. }
  203. return false;
  204. }
  205. @SuppressWarnings("nls")
  206. @Override
  207. public String toString() {
  208. final Type t = getType();
  209. return t + "(" + beginA + "-" + endA + "," + beginB + "-" + endB + ")";
  210. }
  211. }