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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 && beginB < endB)
  112. return Type.INSERT;
  113. if (beginA < endA && beginB == endB)
  114. return Type.DELETE;
  115. if (beginA == endA && beginB == endB)
  116. return Type.EMPTY;
  117. return Type.REPLACE;
  118. }
  119. /** @return start point in sequence A. */
  120. public final int getBeginA() {
  121. return beginA;
  122. }
  123. /** @return end point in sequence A. */
  124. public final int getEndA() {
  125. return endA;
  126. }
  127. /** @return start point in sequence B. */
  128. public final int getBeginB() {
  129. return beginB;
  130. }
  131. /** @return end point in sequence B. */
  132. public final int getEndB() {
  133. return endB;
  134. }
  135. /** Increase {@link #getEndA()} by 1. */
  136. public void extendA() {
  137. endA++;
  138. }
  139. /** Increase {@link #getEndB()} by 1. */
  140. public void extendB() {
  141. endB++;
  142. }
  143. /** Swap A and B, so the edit goes the other direction. */
  144. public void swap() {
  145. final int sBegin = beginA;
  146. final int sEnd = endA;
  147. beginA = beginB;
  148. endA = endB;
  149. beginB = sBegin;
  150. endB = sEnd;
  151. }
  152. @Override
  153. public int hashCode() {
  154. return beginA ^ endA;
  155. }
  156. @Override
  157. public boolean equals(final Object o) {
  158. if (o instanceof Edit) {
  159. final Edit e = (Edit) o;
  160. return this.beginA == e.beginA && this.endA == e.endA
  161. && this.beginB == e.beginB && this.endB == e.endB;
  162. }
  163. return false;
  164. }
  165. @Override
  166. public String toString() {
  167. final Type t = getType();
  168. return t + "(" + beginA + "-" + endA + "," + beginB + "-" + endB + ")";
  169. }
  170. }