2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.corext.textmanipulation;
7 //import org.eclipse.jdt.core.ISourceRange;
9 //import org.eclipse.jdt.internal.corext.Assert;
11 public final class TextRange {
13 /* package */ int fOffset;
14 /* package */ int fLength;
16 public static final TextRange UNDEFINED= new TextRange((TextRange)null);
19 * Creates a insert position with the given offset.
21 * @param offset the position offset, must be >= 0
23 public TextRange(int offset) {
28 * Creates a new range with the given offset and length.
30 * @param offset the position offset, must be >= 0
31 * @param length the position length, must be >= 0
33 public TextRange(int offset, int length) {
35 // Assert.isTrue(fOffset >= 0);
37 // Assert.isTrue(fLength >= 0);
41 * Constructor for the undefined text range.
43 private TextRange(TextRange dummy) {
48 public static TextRange createFromStartAndLength(int start, int length) {
49 return new TextRange(start, length);
52 public static TextRange createFromStartAndInclusiveEnd(int start, int end) {
53 return new TextRange(start, end - start + 1);
56 public static TextRange createFromStartAndExclusiveEnd(int start, int end) {
57 return new TextRange(start, end - start);
61 * Creates a new range from the given source range.
63 * @range the source range denoting offset and length
65 // public TextRange(ISourceRange range) {
66 // this(range.getOffset(), range.getLength());
70 * Returns the offset of this range.
72 * @return the length of this range
74 public int getOffset() {
79 * Returns the length of this range.
81 * @return the length of this range
83 public int getLength() {
88 * Returns the inclusive end position of this range. That means that the end position
89 * denotes the last character of this range.
91 * @return the inclusive end position
93 public int getInclusiveEnd() {
94 return fOffset + fLength - 1;
98 * Returns the exclusive end position of this range. That means that the end position
99 * denotes the first character after this range.
101 * @return the exclusive end position
103 public int getExclusiveEnd() {
104 return fOffset + fLength;
108 * Creates a copy of this <code>TextRange</code>.
110 * @return a copy of this <code>TextRange</code>
112 public TextRange copy() {
115 return new TextRange(fOffset, fLength);
119 * Returns <code>true</code> if this text range is the <code>UNDEFINED</code>
120 * text range. Otherwise <code>false</code> is returned.
122 public boolean isUndefined() {
123 return UNDEFINED == this;
127 * Checks if this <code>TextRange</code> is valid. For valid text range the following
128 * expression evaluates to <code>true</code>:
130 * getOffset() >= 0 && getLength() >= 0
133 * @return <code>true</code> if this text range is a valid range. Otherwise <code>
136 public boolean isValid() {
137 return fOffset >= 0 && fLength >= 0;
140 /* package */ boolean isInsertionPoint() {
144 /* package */ boolean equals(TextRange range) {
145 return fOffset == range.fOffset && fLength == range.fLength;
148 /* package */ boolean isEqualInsertionPoint(TextRange range) {
149 return fLength == 0 && range.fLength == 0 && fOffset == range.fOffset;
152 /* package */ boolean liesBehind(TextRange range) {
153 return fOffset >= range.fOffset + range.fLength;
156 /* package */ boolean isInsertionPointAt(int o) {
157 return fOffset == o && fLength == 0;
160 /* package */ boolean covers(TextRange other) {
161 if (fLength == 0) { // an insertion point can't cover anything
163 } else if (other.fLength == 0) {
164 int otherOffset= other.fOffset;
165 return fOffset < otherOffset && otherOffset < fOffset + fLength;
167 int otherOffset= other.fOffset;
168 return fOffset <= otherOffset && otherOffset + other.fLength <= fOffset + fLength;
172 * @see Object#toString()
174 public String toString() {
175 StringBuffer buffer= new StringBuffer();
176 buffer.append(TextManipulationMessages.getString("TextRange.offset")); //$NON-NLS-1$
177 buffer.append(fOffset);
178 buffer.append(TextManipulationMessages.getString("TextRange.length")); //$NON-NLS-1$
179 buffer.append(fLength);
180 return buffer.toString();
183 public boolean equals(Object obj) {
184 if (! (obj instanceof TextRange))
186 TextRange other= (TextRange)obj;
187 return fOffset == other.getOffset() && fLength == other.getLength();
190 public int hashCode() {
191 return fOffset ^ fLength;