1 /*
2 * Copyright (C) 2010, Google Inc. and others
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Distribution License v. 1.0 which is available at
6 * https://www.eclipse.org/org/documents/edl-v10.php.
7 *
8 * SPDX-License-Identifier: BSD-3-Clause
9 */
10
11 package org.eclipse.jgit.diff;
12
13 /**
14 * Wraps a {@link org.eclipse.jgit.diff.Sequence} to have a narrower range of
15 * elements.
16 * <p>
17 * This sequence acts as a proxy for the real sequence, translating element
18 * indexes on the fly by adding {@code begin} to them. Sequences of this type
19 * must be used with a {@link org.eclipse.jgit.diff.SubsequenceComparator}.
20 *
21 * @param <S>
22 * the base sequence type.
23 */
24 public final class Subsequence<S extends Sequence> extends Sequence {
25 /**
26 * Construct a subsequence around the A region/base sequence.
27 *
28 * @param a
29 * the A sequence.
30 * @param region
31 * the region of {@code a} to create a subsequence around.
32 * @return subsequence of {@code base} as described by A in {@code region}.
33 */
34 public static <S extends Sequence> Subsequence<S> a(S a, Edit region) {
35 return new Subsequence<>(a, region.beginA, region.endA);
36 }
37
38 /**
39 * Construct a subsequence around the B region/base sequence.
40 *
41 * @param b
42 * the B sequence.
43 * @param region
44 * the region of {@code b} to create a subsequence around.
45 * @return subsequence of {@code base} as described by B in {@code region}.
46 */
47 public static <S extends Sequence> Subsequence<S> b(S b, Edit region) {
48 return new Subsequence<>(b, region.beginB, region.endB);
49 }
50
51 /**
52 * Adjust the Edit to reflect positions in the base sequence.
53 *
54 * @param e
55 * edit to adjust in-place. Prior to invocation the indexes are
56 * in terms of the two subsequences; after invocation the indexes
57 * are in terms of the base sequences.
58 * @param a
59 * the A sequence.
60 * @param b
61 * the B sequence.
62 */
63 public static <S extends Sequence> void toBase(Edit e, Subsequence<S> a,
64 Subsequence<S> b) {
65 e.beginA += a.begin;
66 e.endA += a.begin;
67
68 e.beginB += b.begin;
69 e.endB += b.begin;
70 }
71
72 /**
73 * Adjust the Edits to reflect positions in the base sequence.
74 *
75 * @param edits
76 * edits to adjust in-place. Prior to invocation the indexes are
77 * in terms of the two subsequences; after invocation the indexes
78 * are in terms of the base sequences.
79 * @param a
80 * the A sequence.
81 * @param b
82 * the B sequence.
83 * @return always {@code edits} (as the list was updated in-place).
84 */
85 public static <S extends Sequence> EditList"../../../../org/eclipse/jgit/diff/EditList.html#EditList">EditList toBase(EditList edits,
86 Subsequence<S> a, Subsequence<S> b) {
87 for (Edit e : edits)
88 toBase(e, a, b);
89 return edits;
90 }
91
92 final S base;
93
94 final int begin;
95
96 private final int size;
97
98 /**
99 * Construct a subset of another sequence.
100 *
101 * The size of the subsequence will be {@code end - begin}.
102 *
103 * @param base
104 * the real sequence.
105 * @param begin
106 * First element index of {@code base} that will be part of this
107 * new subsequence. The element at {@code begin} will be this
108 * sequence's element 0.
109 * @param end
110 * One past the last element index of {@code base} that will be
111 * part of this new subsequence.
112 */
113 public Subsequence(S base, int begin, int end) {
114 this.base = base;
115 this.begin = begin;
116 this.size = end - begin;
117 }
118
119 /** {@inheritDoc} */
120 @Override
121 public int size() {
122 return size;
123 }
124 }