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 7.0KB

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