Subsequence.java

  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. package org.eclipse.jgit.diff;

  11. /**
  12.  * Wraps a {@link org.eclipse.jgit.diff.Sequence} to have a narrower range of
  13.  * elements.
  14.  * <p>
  15.  * This sequence acts as a proxy for the real sequence, translating element
  16.  * indexes on the fly by adding {@code begin} to them. Sequences of this type
  17.  * must be used with a {@link org.eclipse.jgit.diff.SubsequenceComparator}.
  18.  *
  19.  * @param <S>
  20.  *            the base sequence type.
  21.  */
  22. public final class Subsequence<S extends Sequence> extends Sequence {
  23.     /**
  24.      * Construct a subsequence around the A region/base sequence.
  25.      *
  26.      * @param a
  27.      *            the A sequence.
  28.      * @param region
  29.      *            the region of {@code a} to create a subsequence around.
  30.      * @return subsequence of {@code base} as described by A in {@code region}.
  31.      */
  32.     public static <S extends Sequence> Subsequence<S> a(S a, Edit region) {
  33.         return new Subsequence<>(a, region.beginA, region.endA);
  34.     }

  35.     /**
  36.      * Construct a subsequence around the B region/base sequence.
  37.      *
  38.      * @param b
  39.      *            the B sequence.
  40.      * @param region
  41.      *            the region of {@code b} to create a subsequence around.
  42.      * @return subsequence of {@code base} as described by B in {@code region}.
  43.      */
  44.     public static <S extends Sequence> Subsequence<S> b(S b, Edit region) {
  45.         return new Subsequence<>(b, region.beginB, region.endB);
  46.     }

  47.     /**
  48.      * Adjust the Edit to reflect positions in the base sequence.
  49.      *
  50.      * @param e
  51.      *            edit to adjust in-place. Prior to invocation the indexes are
  52.      *            in terms of the two subsequences; after invocation the indexes
  53.      *            are in terms of the base sequences.
  54.      * @param a
  55.      *            the A sequence.
  56.      * @param b
  57.      *            the B sequence.
  58.      */
  59.     public static <S extends Sequence> void toBase(Edit e, Subsequence<S> a,
  60.             Subsequence<S> b) {
  61.         e.beginA += a.begin;
  62.         e.endA += a.begin;

  63.         e.beginB += b.begin;
  64.         e.endB += b.begin;
  65.     }

  66.     /**
  67.      * Adjust the Edits to reflect positions in the base sequence.
  68.      *
  69.      * @param edits
  70.      *            edits to adjust in-place. Prior to invocation the indexes are
  71.      *            in terms of the two subsequences; after invocation the indexes
  72.      *            are in terms of the base sequences.
  73.      * @param a
  74.      *            the A sequence.
  75.      * @param b
  76.      *            the B sequence.
  77.      * @return always {@code edits} (as the list was updated in-place).
  78.      */
  79.     public static <S extends Sequence> EditList toBase(EditList edits,
  80.             Subsequence<S> a, Subsequence<S> b) {
  81.         for (Edit e : edits)
  82.             toBase(e, a, b);
  83.         return edits;
  84.     }

  85.     final S base;

  86.     final int begin;

  87.     private final int size;

  88.     /**
  89.      * Construct a subset of another sequence.
  90.      *
  91.      * The size of the subsequence will be {@code end - begin}.
  92.      *
  93.      * @param base
  94.      *            the real sequence.
  95.      * @param begin
  96.      *            First element index of {@code base} that will be part of this
  97.      *            new subsequence. The element at {@code begin} will be this
  98.      *            sequence's element 0.
  99.      * @param end
  100.      *            One past the last element index of {@code base} that will be
  101.      *            part of this new subsequence.
  102.      */
  103.     public Subsequence(S base, int begin, int end) {
  104.         this.base = base;
  105.         this.begin = begin;
  106.         this.size = end - begin;
  107.     }

  108.     /** {@inheritDoc} */
  109.     @Override
  110.     public int size() {
  111.         return size;
  112.     }
  113. }