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