KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > Position


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.text;
12
13 import org.eclipse.core.runtime.Assert;
14
15
16 /**
17  * Positions describe text ranges of a document. Positions are adapted to
18  * changes applied to that document. The text range is specified by an offset
19  * and a length. Positions can be marked as deleted. Deleted positions are
20  * considered to no longer represent a valid text range in the managing
21  * document.
22  * <p>
23  * Positions attached to documents are usually updated by position updaters.
24  * Because position updaters are freely definable and because of the frequency
25  * in which they are used, the fields of a position are made publicly
26  * accessible. Clients other than position updaters are not allowed to access
27  * these public fields.
28  * </p>
29  * <p>
30  * Positions cannot be used as keys in hash tables as they override
31  * <code>equals</code> and <code>hashCode</code> as they would be value
32  * objects.
33  * </p>
34  *
35  * @see org.eclipse.jface.text.IDocument
36  */

37 public class Position {
38
39     /** The offset of the position */
40     public int offset;
41     /** The length of the position */
42     public int length;
43     /** Indicates whether the position has been deleted */
44     public boolean isDeleted;
45
46     /**
47      * Creates a new position with the given offset and length 0.
48      *
49      * @param offset the position offset, must be >= 0
50      */

51     public Position(int offset) {
52         this(offset, 0);
53     }
54
55     /**
56      * Creates a new position with the given offset and length.
57      *
58      * @param offset the position offset, must be >= 0
59      * @param length the position length, must be >= 0
60      */

61     public Position(int offset, int length) {
62         Assert.isTrue(offset >= 0);
63         Assert.isTrue(length >= 0);
64         this.offset= offset;
65         this.length= length;
66     }
67
68     /**
69      * Creates a new, not initialized position.
70      */

71     protected Position() {
72     }
73
74      /*
75      * @see java.lang.Object#hashCode()
76      */

77     public int hashCode() {
78         int deleted= isDeleted ? 0 : 1;
79         return (offset << 24) | (length << 16) | deleted;
80      }
81
82     /**
83      * Marks this position as deleted.
84      */

85     public void delete() {
86         isDeleted= true;
87     }
88
89     /**
90      * Marks this position as not deleted.
91      *
92      * @since 2.0
93      */

94     public void undelete() {
95         isDeleted= false;
96     }
97
98     /*
99      * @see java.lang.Object#equals(java.lang.Object)
100      */

101     public boolean equals(Object JavaDoc other) {
102         if (other instanceof Position) {
103             Position rp= (Position) other;
104             return (rp.offset == offset) && (rp.length == length);
105         }
106         return super.equals(other);
107     }
108
109     /**
110      * Returns the length of this position.
111      *
112      * @return the length of this position
113      */

114     public int getLength() {
115         return length;
116     }
117
118     /**
119      * Returns the offset of this position.
120      *
121      * @return the offset of this position
122      */

123     public int getOffset() {
124         return offset;
125     }
126
127     /**
128      * Checks whether the given index is inside
129      * of this position's text range.
130      *
131      * @param index the index to check
132      * @return <code>true</code> if <code>index</code> is inside of this position
133      */

134     public boolean includes(int index) {
135
136         if (isDeleted)
137             return false;
138
139         return (this.offset <= index) && (index < this.offset + length);
140     }
141
142     /**
143      * Checks whether the intersection of the given text range
144      * and the text range represented by this position is empty
145      * or not.
146      *
147      * @param rangeOffset the offset of the range to check
148      * @param rangeLength the length of the range to check
149      * @return <code>true</code> if intersection is not empty
150      */

151     public boolean overlapsWith(int rangeOffset, int rangeLength) {
152
153         if (isDeleted)
154             return false;
155
156         int end= rangeOffset + rangeLength;
157         int thisEnd= this.offset + this.length;
158
159         if (rangeLength > 0) {
160             if (this.length > 0)
161                 return this.offset < end && rangeOffset < thisEnd;
162             return rangeOffset <= this.offset && this.offset < end;
163         }
164
165         if (this.length > 0)
166             return this.offset <= rangeOffset && rangeOffset < thisEnd;
167         return this.offset == rangeOffset;
168     }
169
170     /**
171      * Returns whether this position has been deleted or not.
172      *
173      * @return <code>true</code> if position has been deleted
174      */

175     public boolean isDeleted() {
176         return isDeleted;
177     }
178
179     /**
180      * Changes the length of this position to the given length.
181      *
182      * @param length the new length of this position
183      */

184     public void setLength(int length) {
185         Assert.isTrue(length >= 0);
186         this.length= length;
187     }
188
189     /**
190      * Changes the offset of this position to the given offset.
191      *
192      * @param offset the new offset of this position
193      */

194     public void setOffset(int offset) {
195         Assert.isTrue(offset >= 0);
196         this.offset= offset;
197     }
198 }
199
Popular Tags