Region.java

  1. /*
  2.  * Copyright (C) 2011, 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.blame;

  11. /**
  12.  * Region of the result that still needs to be computed.
  13.  * <p>
  14.  * Regions are held in a singly-linked-list by {@link Candidate} using the
  15.  * {@link Candidate#regionList} field. The list is kept in sorted order by
  16.  * {@link #resultStart}.
  17.  */
  18. class Region {
  19.     /** Next entry in the region linked list. */
  20.     Region next;

  21.     /** First position of this region in the result file blame is computing. */
  22.     int resultStart;

  23.     /** First position in the {@link Candidate} that owns this Region. */
  24.     int sourceStart;

  25.     /** Length of the region, always &gt;= 1. */
  26.     int length;

  27.     Region(int rs, int ss, int len) {
  28.         resultStart = rs;
  29.         sourceStart = ss;
  30.         length = len;
  31.     }

  32.     /**
  33.      * Copy the entire result region, but at a new source position.
  34.      *
  35.      * @param newSource
  36.      *            the new source position.
  37.      * @return the same result region, but offset for a new source.
  38.      */
  39.     Region copy(int newSource) {
  40.         return new Region(resultStart, newSource, length);
  41.     }

  42.     /**
  43.      * Split the region, assigning a new source position to the first half.
  44.      *
  45.      * @param newSource
  46.      *            the new source position.
  47.      * @param newLen
  48.      *            length of the new region.
  49.      * @return the first half of the region, at the new source.
  50.      */
  51.     Region splitFirst(int newSource, int newLen) {
  52.         return new Region(resultStart, newSource, newLen);
  53.     }

  54.     /**
  55.      * Edit this region to remove the first {@code d} elements.
  56.      *
  57.      * @param d
  58.      *            number of elements to remove from the start of this region.
  59.      */
  60.     void slideAndShrink(int d) {
  61.         resultStart += d;
  62.         sourceStart += d;
  63.         length -= d;
  64.     }

  65.     Region deepCopy() {
  66.         Region head = new Region(resultStart, sourceStart, length);
  67.         Region tail = head;
  68.         for (Region n = next; n != null; n = n.next) {
  69.             Region q = new Region(n.resultStart, n.sourceStart, n.length);
  70.             tail.next = q;
  71.             tail = q;
  72.         }
  73.         return head;
  74.     }

  75.     /** {@inheritDoc} */
  76.     @Override
  77.     public String toString() {
  78.         StringBuilder buf = new StringBuilder();
  79.         Region r = this;
  80.         do {
  81.             if (r != this)
  82.                 buf.append(',');
  83.             buf.append(r.resultStart);
  84.             buf.append('-');
  85.             buf.append(r.resultStart + r.length);
  86.             r = r.next;
  87.         } while (r != null);
  88.         return buf.toString();
  89.     }
  90. }