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