1 11 package org.eclipse.jface.internal.text.revisions; 12 13 import org.eclipse.jface.text.source.ILineRange; 14 15 29 public final class Range implements ILineRange, Cloneable { 30 38 public static Range copy(ILineRange range) throws LineIndexOutOfBoundsException { 39 return createRelative(range.getStartLine(), range.getNumberOfLines()); 40 } 41 42 48 public static Range copy(Range range) { 49 return createRelative(range.start(), range.length()); 50 } 51 52 61 public static Range createRelative(int start, int length) throws LineIndexOutOfBoundsException { 62 return new Range(start, length); 63 } 64 65 74 public static Range createAbsolute(int start, int end) { 75 return new Range(start, end - start); 76 } 77 78 private int fStart; 79 private int fLength; 80 81 84 private Range(int start, int length) { 85 moveTo(start); 86 setLength(length); 87 } 88 89 92 public int getStartLine() { 93 return start(); 94 } 95 96 99 public int getNumberOfLines() { 100 return length(); 101 } 102 103 108 public int start() { 109 return fStart; 110 } 111 112 117 public int length() { 118 return fLength; 119 } 120 121 126 public int end() { 127 return start() + length(); 128 } 129 130 136 public void moveTo(int start) throws LineIndexOutOfBoundsException { 137 if (!(start >= 0)) 138 throw new LineIndexOutOfBoundsException("Cannot set a negative start: " + start); fStart= start; 140 } 141 142 149 public void moveEndTo(int end) throws LineIndexOutOfBoundsException { 150 moveTo(end - length()); 151 } 152 153 160 public void moveBy(int delta) throws LineIndexOutOfBoundsException { 161 moveTo(start() + delta); 162 } 163 164 170 public void setStart(int start) throws LineIndexOutOfBoundsException { 171 int end= end(); 172 if (!(start >= 0 && start < end)) 173 throw new LineIndexOutOfBoundsException("Cannot set a negative start: " + start); moveTo(start); 175 setEnd(end); 176 } 177 178 184 public void setEnd(int end) throws LineIndexOutOfBoundsException { 185 setLength(end - start()); 186 } 187 188 194 public void setLength(int length) throws LineIndexOutOfBoundsException { 195 if (!(length > 0)) 196 throw new LineIndexOutOfBoundsException("Cannot set length <= 0: " + length); fLength= length; 198 } 199 200 206 public void setLengthAndMove(int length) throws LineIndexOutOfBoundsException { 207 setStart(end() - length); 208 } 209 210 216 public void resizeBy(int delta) throws LineIndexOutOfBoundsException { 217 setLength(length() + delta); 218 } 219 220 226 public void resizeAndMoveBy(int delta) throws LineIndexOutOfBoundsException { 227 setStart(start() + delta); 228 } 229 230 238 public Range split(int remaining) throws LineIndexOutOfBoundsException { 239 if (!(remaining < length())) throw new LineIndexOutOfBoundsException("Remaining must be less than length: " + length()); 242 int splitLength= length() - remaining; 243 setLength(remaining); 244 return new Range(end(), splitLength); 245 } 246 247 253 public boolean equalRange(ILineRange range) { 254 if (range == this) 255 return true; 256 if (range == null) 257 return false; 258 return range.getStartLine() == start() && range.getNumberOfLines() == length(); 259 } 260 261 264 public Object clone() { 265 return Range.copy(this); 266 } 267 } | Popular Tags |