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 11 package org.eclipse.jgit.blame; 12 13 /** 14 * Region of the result that still needs to be computed. 15 * <p> 16 * Regions are held in a singly-linked-list by {@link Candidate} using the 17 * {@link Candidate#regionList} field. The list is kept in sorted order by 18 * {@link #resultStart}. 19 */ 20 class Region { 21 /** Next entry in the region linked list. */ 22 Region next; 23 24 /** First position of this region in the result file blame is computing. */ 25 int resultStart; 26 27 /** First position in the {@link Candidate} that owns this Region. */ 28 int sourceStart; 29 30 /** Length of the region, always >= 1. */ 31 int length; 32 33 Region(int rs, int ss, int len) { 34 resultStart = rs; 35 sourceStart = ss; 36 length = len; 37 } 38 39 /** 40 * Copy the entire result region, but at a new source position. 41 * 42 * @param newSource 43 * the new source position. 44 * @return the same result region, but offset for a new source. 45 */ 46 Region copy(int newSource) { 47 return new Region(resultStart, newSource, length); 48 } 49 50 /** 51 * Split the region, assigning a new source position to the first half. 52 * 53 * @param newSource 54 * the new source position. 55 * @param newLen 56 * length of the new region. 57 * @return the first half of the region, at the new source. 58 */ 59 Region splitFirst(int newSource, int newLen) { 60 return new Region(resultStart, newSource, newLen); 61 } 62 63 /** 64 * Edit this region to remove the first {@code d} elements. 65 * 66 * @param d 67 * number of elements to remove from the start of this region. 68 */ 69 void slideAndShrink(int d) { 70 resultStart += d; 71 sourceStart += d; 72 length -= d; 73 } 74 75 Region deepCopy() { 76 Region head = new Region(resultStart, sourceStart, length); 77 Region tail = head; 78 for (Region n = next; n != null; n = n.next) { 79 Region q = new Region(n.resultStart, n.sourceStart, n.length); 80 tail.next = q; 81 tail = q; 82 } 83 return head; 84 } 85 86 /** {@inheritDoc} */ 87 @Override 88 public String toString() { 89 StringBuilder buf = new StringBuilder(); 90 Region r = this; 91 do { 92 if (r != this) 93 buf.append(','); 94 buf.append(r.resultStart); 95 buf.append('-'); 96 buf.append(r.resultStart + r.length); 97 r = r.next; 98 } while (r != null); 99 return buf.toString(); 100 } 101 }