2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.corext.textmanipulation;
7 // import net.sourceforge.phpdt.core.ISourceRange;
9 // import net.sourceforge.phpdt.internal.corext.Assert;
11 public final class TextRange {
13 /* package */int fOffset;
15 /* package */int fLength;
17 public static final TextRange UNDEFINED = new TextRange((TextRange) null);
20 * Creates a insert position with the given offset.
23 * the position offset, must be >= 0
25 public TextRange(int offset) {
30 * Creates a new range with the given offset and length.
33 * the position offset, must be >= 0
35 * the position length, must be >= 0
37 public TextRange(int offset, int length) {
39 // Assert.isTrue(fOffset >= 0);
41 // Assert.isTrue(fLength >= 0);
45 * Constructor for the undefined text range.
47 private TextRange(TextRange dummy) {
52 public static TextRange createFromStartAndLength(int start, int length) {
53 return new TextRange(start, length);
56 public static TextRange createFromStartAndInclusiveEnd(int start, int end) {
57 return new TextRange(start, end - start + 1);
60 public static TextRange createFromStartAndExclusiveEnd(int start, int end) {
61 return new TextRange(start, end - start);
65 * Creates a new range from the given source range.
67 * @range the source range denoting offset and length
69 // public TextRange(ISourceRange range) {
70 // this(range.getOffset(), range.getLength());
73 * Returns the offset of this range.
75 * @return the length of this range
77 public int getOffset() {
82 * Returns the length of this range.
84 * @return the length of this range
86 public int getLength() {
91 * Returns the inclusive end position of this range. That means that the end
92 * position denotes the last character of this range.
94 * @return the inclusive end position
96 public int getInclusiveEnd() {
97 return fOffset + fLength - 1;
101 * Returns the exclusive end position of this range. That means that the end
102 * position denotes the first character after this range.
104 * @return the exclusive end position
106 public int getExclusiveEnd() {
107 return fOffset + fLength;
111 * Creates a copy of this <code>TextRange</code>.
113 * @return a copy of this <code>TextRange</code>
115 public TextRange copy() {
118 return new TextRange(fOffset, fLength);
122 * Returns <code>true</code> if this text range is the
123 * <code>UNDEFINED</code> text range. Otherwise <code>false</code> is
126 public boolean isUndefined() {
127 return UNDEFINED == this;
131 * Checks if this <code>TextRange</code> is valid. For valid text range
132 * the following expression evaluates to <code>true</code>:
135 * getOffset() >= 0 && getLength() >= 0
138 * @return <code>true</code> if this text range is a valid range.
142 public boolean isValid() {
143 return fOffset >= 0 && fLength >= 0;
146 /* package */boolean isInsertionPoint() {
150 /* package */boolean equals(TextRange range) {
151 return fOffset == range.fOffset && fLength == range.fLength;
154 /* package */boolean isEqualInsertionPoint(TextRange range) {
155 return fLength == 0 && range.fLength == 0 && fOffset == range.fOffset;
158 /* package */boolean liesBehind(TextRange range) {
159 return fOffset >= range.fOffset + range.fLength;
162 /* package */boolean isInsertionPointAt(int o) {
163 return fOffset == o && fLength == 0;
166 /* package */boolean covers(TextRange other) {
167 if (fLength == 0) { // an insertion point can't cover anything
169 } else if (other.fLength == 0) {
170 int otherOffset = other.fOffset;
171 return fOffset < otherOffset && otherOffset < fOffset + fLength;
173 int otherOffset = other.fOffset;
174 return fOffset <= otherOffset
175 && otherOffset + other.fLength <= fOffset + fLength;
182 * @see Object#toString()
184 public String toString() {
185 StringBuffer buffer = new StringBuffer();
186 buffer.append(TextManipulationMessages.getString("TextRange.offset")); //$NON-NLS-1$
187 buffer.append(fOffset);
188 buffer.append(TextManipulationMessages.getString("TextRange.length")); //$NON-NLS-1$
189 buffer.append(fLength);
190 return buffer.toString();
193 public boolean equals(Object obj) {
194 if (!(obj instanceof TextRange))
196 TextRange other = (TextRange) obj;
197 return fOffset == other.getOffset() && fLength == other.getLength();
200 public int hashCode() {
201 return fOffset ^ fLength;