d92fba2208b336a9a23d1cc5414daf5eda7ad2bf
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / corext / textmanipulation / TextRange.java
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001.
3  * All Rights Reserved.
4  */
5 package net.sourceforge.phpdt.internal.corext.textmanipulation;
6
7 //import org.eclipse.jdt.core.ISourceRange;
8 //
9 //import org.eclipse.jdt.internal.corext.Assert;
10
11 public final class TextRange {
12
13         /* package */ int fOffset;
14         /* package */ int fLength;
15         
16         public static final TextRange UNDEFINED= new TextRange((TextRange)null);
17
18         /**
19          * Creates a insert position with the given offset.
20          *
21          * @param offset the position offset, must be >= 0
22          */
23         public TextRange(int offset) {
24                 this(offset, 0);
25         }
26         
27         /**
28          * Creates a new range with the given offset and length.
29          *
30          * @param offset the position offset, must be >= 0
31          * @param length the position length, must be >= 0
32          */
33         public TextRange(int offset, int length) {
34                 fOffset= offset;
35 //              Assert.isTrue(fOffset >= 0);
36                 fLength= length;
37 //              Assert.isTrue(fLength >= 0);
38         }
39         
40         /**
41          * Constructor for the undefined text range.
42          */
43         private TextRange(TextRange dummy) {
44                 fOffset= -1;
45                 fLength= -1;
46         }
47         
48         public static TextRange createFromStartAndLength(int start, int length) {
49                 return new TextRange(start, length);
50         }
51         
52         public static TextRange createFromStartAndInclusiveEnd(int start, int end) {
53                 return new TextRange(start, end - start + 1);
54         }
55         
56         public static TextRange createFromStartAndExclusiveEnd(int start, int end) {
57                 return new TextRange(start, end - start);
58         }
59         
60         /**
61          * Creates a new range from the given source range.
62          * 
63          * @range the source range denoting offset and length
64          */
65 //      public TextRange(ISourceRange range) {
66 //              this(range.getOffset(), range.getLength());
67 //      }
68         
69         /**
70          * Returns the offset of this range.
71          *
72          * @return the length of this range
73          */
74         public int getOffset() {
75                 return fOffset;
76         }
77         
78         /**
79          * Returns the length of this range.
80          *
81          * @return the length of this range
82          */
83         public int getLength() {
84                 return fLength;
85         }
86         
87         /**
88          * Returns the inclusive end position of this range. That means that the end position
89          * denotes the last character of this range.
90          * 
91          * @return the inclusive end position
92          */
93         public int getInclusiveEnd() {
94                 return fOffset + fLength - 1;
95         }
96         
97         /**
98          * Returns the exclusive end position of this range. That means that the end position
99          * denotes the first character after this range.
100          * 
101          * @return the exclusive end position
102          */
103         public int getExclusiveEnd() {
104                 return fOffset + fLength;
105         }
106         
107         /**
108          * Creates a copy of this <code>TextRange</code>.
109          * 
110          * @return a copy of this <code>TextRange</code>
111          */
112         public TextRange copy() {
113                 if (isUndefined())
114                         return this;
115                 return new TextRange(fOffset, fLength);
116         }
117         
118         /**
119          * Returns <code>true</code> if this text range is the <code>UNDEFINED</code>
120          * text range. Otherwise <code>false</code> is returned.
121          */
122         public boolean isUndefined() {
123                 return UNDEFINED == this;
124         }
125         
126         /**
127          * Checks if this <code>TextRange</code> is valid. For valid text range the following
128          * expression evaluates to <code>true</code>:
129          * <pre>
130          *      getOffset() >= 0 && getLength() >= 0
131          * </pre>
132          * 
133          * @return <code>true</code> if this text range is a valid range. Otherwise <code>
134          *      false</code>
135          */
136         public boolean isValid() {
137                 return fOffset >= 0 && fLength >= 0;
138         }
139         
140         /* package */ boolean isInsertionPoint() {
141                 return fLength == 0;
142         }
143         
144         /* package */ boolean equals(TextRange range) {
145                 return fOffset == range.fOffset && fLength == range.fLength;
146         }
147
148         /* package */ boolean isEqualInsertionPoint(TextRange range)    {
149                 return fLength == 0 && range.fLength == 0 && fOffset == range.fOffset;
150         }
151
152         /* package */ boolean liesBehind(TextRange range) {
153                 return fOffset >= range.fOffset + range.fLength;
154         }
155
156         /* package */ boolean isInsertionPointAt(int o) {
157                 return fOffset == o && fLength == 0;
158         }
159         
160         /* package */ boolean covers(TextRange other) {
161                 if (fLength == 0) {     // an insertion point can't cover anything
162                         return false;
163                 } else if (other.fLength == 0) {
164                         int otherOffset= other.fOffset;
165                         return fOffset < otherOffset && otherOffset < fOffset + fLength;
166                 } else {
167                         int otherOffset= other.fOffset;
168                         return fOffset <= otherOffset && otherOffset + other.fLength <= fOffset + fLength;
169                 }
170         }
171         /* non Java-doc
172          * @see Object#toString()
173          */
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();
181         }
182
183         public boolean equals(Object obj) {
184                 if (! (obj instanceof TextRange))
185                         return false;
186                 TextRange other= (TextRange)obj;        
187                 return fOffset == other.getOffset() && fLength == other.getLength();
188         }
189
190         public int hashCode() {
191                 return fOffset ^ fLength;
192         }
193
194 }
195