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.

Subsequence.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  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. * Wraps a {@link Sequence} to have a narrower range of elements.
  46. *
  47. * This sequence acts as a proxy for the real sequence, translating element
  48. * indexes on the fly by adding {@code begin} to them. Sequences of this type
  49. * must be used with a {@link SubsequenceComparator}.
  50. *
  51. * @param <S>
  52. * the base sequence type.
  53. */
  54. public final class Subsequence<S extends Sequence> extends Sequence {
  55. /**
  56. * Construct a subsequence around the A region/base sequence.
  57. *
  58. * @param <S>
  59. * the base sequence type.
  60. * @param a
  61. * the A sequence.
  62. * @param region
  63. * the region of {@code a} to create a subsequence around.
  64. * @return subsequence of {@code base} as described by A in {@code region}.
  65. */
  66. public static <S extends Sequence> Subsequence<S> a(S a, Edit region) {
  67. return new Subsequence<S>(a, region.beginA, region.endA);
  68. }
  69. /**
  70. * Construct a subsequence around the B region/base sequence.
  71. *
  72. * @param <S>
  73. * the base sequence type.
  74. * @param b
  75. * the B sequence.
  76. * @param region
  77. * the region of {@code b} to create a subsequence around.
  78. * @return subsequence of {@code base} as described by B in {@code region}.
  79. */
  80. public static <S extends Sequence> Subsequence<S> b(S b, Edit region) {
  81. return new Subsequence<S>(b, region.beginB, region.endB);
  82. }
  83. /**
  84. * Adjust the Edit to reflect positions in the base sequence.
  85. *
  86. * @param <S>
  87. * the base sequence type.
  88. * @param e
  89. * edit to adjust in-place. Prior to invocation the indexes are
  90. * in terms of the two subsequences; after invocation the indexes
  91. * are in terms of the base sequences.
  92. * @param a
  93. * the A sequence.
  94. * @param b
  95. * the B sequence.
  96. */
  97. public static <S extends Sequence> void toBase(Edit e, Subsequence<S> a,
  98. Subsequence<S> b) {
  99. e.beginA += a.begin;
  100. e.endA += a.begin;
  101. e.beginB += b.begin;
  102. e.endB += b.begin;
  103. }
  104. /**
  105. * Adjust the Edits to reflect positions in the base sequence.
  106. *
  107. * @param <S>
  108. * the base sequence type.
  109. * @param edits
  110. * edits to adjust in-place. Prior to invocation the indexes are
  111. * in terms of the two subsequences; after invocation the indexes
  112. * are in terms of the base sequences.
  113. * @param a
  114. * the A sequence.
  115. * @param b
  116. * the B sequence.
  117. * @return always {@code edits} (as the list was updated in-place).
  118. */
  119. public static <S extends Sequence> EditList toBase(EditList edits,
  120. Subsequence<S> a, Subsequence<S> b) {
  121. for (Edit e : edits)
  122. toBase(e, a, b);
  123. return edits;
  124. }
  125. final S base;
  126. final int begin;
  127. private final int size;
  128. /**
  129. * Construct a subset of another sequence.
  130. *
  131. * The size of the subsequence will be {@code end - begin}.
  132. *
  133. * @param base
  134. * the real sequence.
  135. * @param begin
  136. * First element index of {@code base} that will be part of this
  137. * new subsequence. The element at {@code begin} will be this
  138. * sequence's element 0.
  139. * @param end
  140. * One past the last element index of {@code base} that will be
  141. * part of this new subsequence.
  142. */
  143. public Subsequence(S base, int begin, int end) {
  144. this.base = base;
  145. this.begin = begin;
  146. this.size = end - begin;
  147. }
  148. @Override
  149. public int size() {
  150. return size;
  151. }
  152. }