/** * Creates a new range with the given start offset and length. * * @param start the first line of the new range, must be >= 0 * @param length the number of lines included in the new range, must be > 0 * @return a <code>Range</code> with the given start and length * @throws LineIndexOutOfBoundsException if the parameters violate the invariant of * {@link Range} */ public static Range createRelative(int start, int length) throws LineIndexOutOfBoundsException { return new Range(start, length); }
@Override public Object clone() { return Range.copy(this); } }
/** * Creates a new range equal to the passed line range. * * @param range the range to copy * @return a <code>Range</code> equal to <code>range</code> */ public static Range copy(Range range) { return createRelative(range.start(), range.length()); }
/** * Sets the length of this range, keeping {@link #end()} constant. * * @param length the new length, must be > 0 and <= {@link #end()} * @throws LineIndexOutOfBoundsException if <code>length</code> <= 0 */ public void setLengthAndMove(int length) throws LineIndexOutOfBoundsException { setStart(end() - length); }
/** * Adjusts this change region to a diff hunk. This will change the adjusted ranges. * * @param hunk the diff hunk to adjust to */ public void adjustTo(Hunk hunk) { for (ListIterator<Range> it= fAdjusted.listIterator(); it.hasNext();) { Range range= it.next(); // do we need a split? int unchanged= getUnchanged(hunk, range.start()); if (unchanged > 0) { if (unchanged >= range.length()) continue; range= range.split(unchanged); it.add(range); it.previous(); it.next(); // needed so we can remove below } int line= range.start(); Assert.isTrue(hunk.line <= line); // by how much do we shrink? int overlap= getOverlap(hunk, line); if (overlap >= range.length()) { it.remove(); continue; } // by how much do we move? range.moveBy(hunk.delta + overlap); range.resizeBy(-overlap); } }
/** * Splits a range off the end of the receiver. The receiver is shortened to only include * <code>remaining</code> lines after the split. * * @param remaining the number of lines to remain in the receiver, must be in [1, {@link #length() length}) * @return the split off range * @throws LineIndexOutOfBoundsException if <code>remaining</code>>= {@link #length()} or <code>remaining</code><t;= 0 */ public Range split(int remaining) throws LineIndexOutOfBoundsException { if (!(remaining < length())) // assert before modification throw new LineIndexOutOfBoundsException("Remaining must be less than length: " + length()); //$NON-NLS-1$ int splitLength= length() - remaining; setLength(remaining); return new Range(end(), splitLength); }
/** * Returns the line coverage of the adjusted ranges, an empty range if the coverage is empty. * * @return the line coverage of the adjusted ranges */ public ILineRange getAdjustedCoverage() { if (fAdjusted.isEmpty()) return new LineRange(fLines.getStartLine(), 0); Range first= fAdjusted.get(0); Range last= fAdjusted.get(fAdjusted.size() - 1); return Range.createAbsolute(first.start(), last.end()); }
/** * Moves the start offset to <code>start</code>, keeping {@link #end()} constant. * * @param start the new start, must be >= 0 and < {@link #end()} * @throws LineIndexOutOfBoundsException if <code>start</code> < 0 or >= {@link #end()} */ public void setStart(int start) throws LineIndexOutOfBoundsException { int end= end(); if (!(start >= 0 && start < end)) throw new LineIndexOutOfBoundsException("Cannot set a negative start: " + start); //$NON-NLS-1$ moveTo(start); setEnd(end); }
/** * Resizes the range by <code>delta</code> lines, keeping {@link #start()} constant. * * @param delta the number of lines to resize the range * @throws LineIndexOutOfBoundsException if <code>-delta</code> >= {@link #length()} */ public void resizeBy(int delta) throws LineIndexOutOfBoundsException { setLength(length() + delta); }
/** * Moves this range such that the {@link #end()} is at <code>end</code>, keeping * {@link #length()} constant. * * @param end the new end * @throws LineIndexOutOfBoundsException if <code>end</code> <= {@link #start()} */ public void moveEndTo(int end) throws LineIndexOutOfBoundsException { moveTo(end - length()); }
/** * Moves the range by <code>delta</code> lines, keeping {@link #length()} constant. The * resulting start line must be >= 0. * * @param delta the number of lines to shift the range * @throws LineIndexOutOfBoundsException if <code>-delta</code> > {@link #start()} */ public void moveBy(int delta) throws LineIndexOutOfBoundsException { moveTo(start() + delta); }
@Override public int getNumberOfLines() { return length(); }
/** * Creates a new range with the same start and length as the passed line range. * * @param range the range to copy * @return a <code>Range</code> with the same start and length as <code>range</code> * @throws LineIndexOutOfBoundsException if the passed {@link ILineRange} does not adhere to the * contract of {@link Range} */ public static Range copy(ILineRange range) throws LineIndexOutOfBoundsException { return createRelative(range.getStartLine(), range.getNumberOfLines()); }
/** * Adjusts this change region to a diff hunk. This will change the adjusted ranges. * * @param hunk the diff hunk to adjust to */ public void adjustTo(Hunk hunk) { for (ListIterator<Range> it= fAdjusted.listIterator(); it.hasNext();) { Range range= it.next(); // do we need a split? int unchanged= getUnchanged(hunk, range.start()); if (unchanged > 0) { if (unchanged >= range.length()) continue; range= range.split(unchanged); it.add(range); it.previous(); it.next(); // needed so we can remove below } int line= range.start(); Assert.isTrue(hunk.line <= line); // by how much do we shrink? int overlap= getOverlap(hunk, line); if (overlap >= range.length()) { it.remove(); continue; } // by how much do we move? range.moveBy(hunk.delta + overlap); range.resizeBy(-overlap); } }
/** * Splits a range off the end of the receiver. The receiver is shortened to only include * <code>remaining</code> lines after the split. * * @param remaining the number of lines to remain in the receiver, must be in [1, {@link #length() length}) * @return the split off range * @throws LineIndexOutOfBoundsException if <code>remaining</code>>= {@link #length()} or <code>remaining</code><t;= 0 */ public Range split(int remaining) throws LineIndexOutOfBoundsException { if (!(remaining < length())) // assert before modification throw new LineIndexOutOfBoundsException("Remaining must be less than length: " + length()); //$NON-NLS-1$ int splitLength= length() - remaining; setLength(remaining); return new Range(end(), splitLength); }
/** * Returns the line coverage of the adjusted ranges, an empty range if the coverage is empty. * * @return the line coverage of the adjusted ranges */ public ILineRange getAdjustedCoverage() { if (fAdjusted.isEmpty()) return new LineRange(fLines.getStartLine(), 0); Range first= fAdjusted.get(0); Range last= fAdjusted.get(fAdjusted.size() - 1); return Range.createAbsolute(first.start(), last.end()); }
/** * Moves the start offset to <code>start</code>, keeping {@link #end()} constant. * * @param start the new start, must be >= 0 and < {@link #end()} * @throws LineIndexOutOfBoundsException if <code>start</code> < 0 or >= {@link #end()} */ public void setStart(int start) throws LineIndexOutOfBoundsException { int end= end(); if (!(start >= 0 && start < end)) throw new LineIndexOutOfBoundsException("Cannot set a negative start: " + start); //$NON-NLS-1$ moveTo(start); setEnd(end); }