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