Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 22e464e00990dab1faf7bea29be862b73bb9b559 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jface.text;


/**
 * Positions describe text ranges of a document. Positions are adapted to
 * changes applied to that document. The text range is specified by an offset
 * and a length. Positions can be marked as deleted. Deleted positions are
 * considered to no longer represent a valid text range in the managing
 * document.
 * <p>
 * Positions attached to documents are usually updated by position updaters.
 * Because position updaters are freely definable and because of the frequency
 * in which they are used, the fields of a position are made publicly
 * accessible. Clients other than position updaters are not allowed to access
 * these public fields.
 * </p>
 * <p>
 * Positions cannot be used as keys in hash tables as they override
 * <code>equals</code> and <code>hashCode</code> as they would be value
 * objects.
 * </p>
 *
 * @see org.eclipse.jface.text.IDocument
 */
public class Position {

	/** The offset of the position */
	public int offset;
	/** The length of the position */
	public int length;
	/** Indicates whether the position has been deleted */
	public boolean isDeleted;

	/**
	 * Creates a new position with the given offset and length 0.
	 *
	 * @param offset the position offset, must be >= 0
	 */
	public Position(int offset) {
		this(offset, 0);
	}

	/**
	 * Creates a new position with the given offset and length.
	 *
	 * @param offset the position offset, must be >= 0
	 * @param length the position length, must be >= 0
	 */
	public Position(int offset, int length) {
		Assert.isTrue(offset >= 0);
		Assert.isTrue(length >= 0);
		this.offset= offset;
		this.length= length;
	}

	/**
	 * Creates a new, not initialized position.
	 */
	protected Position() {
	}

	 /*
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
	 	int deleted= isDeleted ? 0 : 1;
	 	return (offset << 24) | (length << 16) | deleted;
	 }

	/**
	 * Marks this position as deleted.
	 */
	public void delete() {
		isDeleted= true;
	}

	/**
	 * Marks this position as not deleted.
	 *
	 * @since 2.0
	 */
	public void undelete() {
		isDeleted= false;
	}

	/*
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object other) {
		if (other instanceof Position) {
			Position rp= (Position) other;
			return (rp.offset == offset) && (rp.length == length);
		}
		return super.equals(other);
	}

	/**
	 * Returns the length of this position.
	 *
	 * @return the length of this position
	 */
	public int getLength() {
		return length;
	}

	/**
	 * Returns the offset of this position.
	 *
	 * @return the length of this position
	 */
	public int getOffset() {
		return offset;
	}

	/**
	 * Checks whether the given index is inside
	 * of this position's text range.
	 *
	 * @param index the index to check
	 * @return <code>true</code> if <code>index</code> is inside of this position
	 */
	public boolean includes(int index) {

		if (isDeleted)
			return false;

		return (this.offset <= index) && (index < this.offset + length);
	}

	/**
	 * Checks whether the intersection of the given text range
	 * and the text range represented by this position is empty
	 * or not.
	 *
	 * @param rangeOffset the offset of the range to check
	 * @param rangeLength the length of the range to check
	 * @return <code>true</code> if intersection is not empty
	 */
	public boolean overlapsWith(int rangeOffset, int rangeLength) {

		if (isDeleted)
			return false;

		int end= rangeOffset + rangeLength;
		int thisEnd= this.offset + this.length;

		if (rangeLength > 0) {
			if (this.length > 0)
				return this.offset < end && rangeOffset < thisEnd;
			return  rangeOffset <= this.offset && this.offset < end;
		}

		if (this.length > 0)
			return this.offset <= rangeOffset && rangeOffset < thisEnd;
		return this.offset == rangeOffset;
	}

	/**
	 * Returns whether this position has been deleted or not.
	 *
	 * @return <code>true</code> if position has been deleted
	 */
	public boolean isDeleted() {
		return isDeleted;
	}

	/**
	 * Changes the length of this position to the given length.
	 *
	 * @param length the new length of this position
	 */
	public void setLength(int length) {
		Assert.isTrue(length >= 0);
		this.length= length;
	}

	/**
	 * Changes the offset of this position to the given offset.
	 *
	 * @param offset the new offset of this position
	 */
	public void setOffset(int offset) {
		Assert.isTrue(offset >= 0);
		this.offset= offset;
	}
}

Back to the top