KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > text > revisions > Range


1 /*******************************************************************************
2  * Copyright (c) 2006 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.internal.text.revisions;
12
13 import org.eclipse.jface.text.source.ILineRange;
14
15 /**
16  * A variable {@link org.eclipse.jface.text.source.ILineRange} with the following invariant:
17  * <ul>
18  * <li>{@link #start() start} &gt;= 0
19  * <li>{@link #length() length} &gt; 0, i.e. a range cannot be empty
20  * </ul>
21  * <p>
22  * Attempts to create or modify a <code>Range</code> such that this invariant would be violated
23  * result in a {@link LineIndexOutOfBoundsException} being
24  * thrown.
25  * </p>
26  *
27  * @since 3.2
28  */

29 public final class Range implements ILineRange, Cloneable JavaDoc {
30     /**
31      * Creates a new range with the same start and length as the passed line range.
32      *
33      * @param range the range to copy
34      * @return a <code>Range</code> with the same start and length as <code>range</code>
35      * @throws LineIndexOutOfBoundsException if the passed {@link ILineRange} does not adhere to the
36      * contract of {@link Range}
37      */

38     public static Range copy(ILineRange range) throws LineIndexOutOfBoundsException {
39         return createRelative(range.getStartLine(), range.getNumberOfLines());
40     }
41     
42     /**
43      * Creates a new range equal to the passed line range.
44      *
45      * @param range the range to copy
46      * @return a <code>Range</code> equal to <code>range</code>
47      */

48     public static Range copy(Range range) {
49         return createRelative(range.start(), range.length());
50     }
51     
52     /**
53      * Creates a new range with the given start offset and length.
54      *
55      * @param start the first line of the new range, must be &gt;= 0
56      * @param length the number of lines included in the new range, must be &gt; 0
57      * @return a <code>Range</code> with the given start and length
58      * @throws LineIndexOutOfBoundsException if the parameters violate the invariant of
59      * {@link Range}
60      */

61     public static Range createRelative(int start, int length) throws LineIndexOutOfBoundsException {
62         return new Range(start, length);
63     }
64
65     /**
66      * Creates a new range with the given start and end offsets.
67      *
68      * @param start the first line of the new range, must be &gt;= 0
69      * @param end the first line not in the range any more (exclusive), must be &gt; <code>start</code>
70      * @return a <code>Range</code> with the given start and end offsets
71      * @throws LineIndexOutOfBoundsException if the parameters violate the invariant of
72      * {@link Range}
73      */

74     public static Range createAbsolute(int start, int end) {
75         return new Range(start, end - start);
76     }
77
78     private int fStart;
79     private int fLength;
80
81     /*
82      * Private constructor.
83      */

84     private Range(int start, int length) {
85         moveTo(start);
86         setLength(length);
87     }
88     
89     /*
90      * @see org.eclipse.jface.text.source.ILineRange#getStartLine()
91      */

92     public int getStartLine() {
93         return start();
94     }
95
96     /*
97      * @see org.eclipse.jface.text.source.ILineRange#getNumberOfLines()
98      */

99     public int getNumberOfLines() {
100         return length();
101     }
102     
103     /**
104      * Returns the first line contained in this range. Short equivalent of {@link #getStartLine()}.
105      *
106      * @return the first line contained in this range
107      */

108     public int start() {
109         return fStart;
110     }
111     
112     /**
113      * Returns the number of lines contained in this range. Short equivalent of {@link #getNumberOfLines()}.
114      *
115      * @return the number of lines contained in this range
116      */

117     public int length() {
118         return fLength;
119     }
120     
121     /**
122      * Returns the first line after this range. Equivalent to {@linkplain #start() start} + {@linkplain #length() length}.
123      *
124      * @return the first line after this range
125      */

126     public int end() {
127         return start() + length();
128     }
129     
130     /**
131      * Moves the receiver to <code>start</code>, keeping {@link #length()} constant.
132      *
133      * @param start the new start, must be &gt;= 0
134      * @throws LineIndexOutOfBoundsException if <code>start</code> &lt; 0
135      */

136     public void moveTo(int start) throws LineIndexOutOfBoundsException {
137         if (!(start >= 0))
138             throw new LineIndexOutOfBoundsException("Cannot set a negative start: " + start); //$NON-NLS-1$
139
fStart= start;
140     }
141     
142     /**
143      * Moves this range such that the {@link #end()} is at <code>end</code>, keeping
144      * {@link #length()} constant.
145      *
146      * @param end the new end
147      * @throws LineIndexOutOfBoundsException if <code>end</code> &lt;= {@link #start()}
148      */

149     public void moveEndTo(int end) throws LineIndexOutOfBoundsException {
150         moveTo(end - length());
151     }
152     
153     /**
154      * Moves the range by <code>delta</code> lines, keeping {@link #length()} constant. The
155      * resulting start line must be &gt;= 0.
156      *
157      * @param delta the number of lines to shift the range
158      * @throws LineIndexOutOfBoundsException if <code>-delta</code> &gt; {@link #start()}
159      */

160     public void moveBy(int delta) throws LineIndexOutOfBoundsException {
161         moveTo(start() + delta);
162     }
163     
164     /**
165      * Moves the start offset to <code>start</code>, keeping {@link #end()} constant.
166      *
167      * @param start the new start, must be &gt;= 0 and &lt; {@link #end()}
168      * @throws LineIndexOutOfBoundsException if <code>start</code> &lt; 0 or &gt;= {@link #end()}
169      */

170     public void setStart(int start) throws LineIndexOutOfBoundsException {
171         int end= end();
172         if (!(start >= 0 && start < end))
173             throw new LineIndexOutOfBoundsException("Cannot set a negative start: " + start); //$NON-NLS-1$
174
moveTo(start);
175         setEnd(end);
176     }
177     
178     /**
179      * Sets the end of this range, keeping {@link #start()} constant.
180      *
181      * @param end the new end, must be &gt; {@link #start()}
182      * @throws LineIndexOutOfBoundsException if <code>end</code> &lt;= {@link #start()}
183      */

184     public void setEnd(int end) throws LineIndexOutOfBoundsException {
185         setLength(end - start());
186     }
187     
188     /**
189      * Sets the length of this range, keeping {@link #start()} constant.
190      *
191      * @param length the new length, must be &gt; 0
192      * @throws LineIndexOutOfBoundsException if <code>length</code> &lt;= 0
193      */

194     public void setLength(int length) throws LineIndexOutOfBoundsException {
195         if (!(length > 0))
196             throw new LineIndexOutOfBoundsException("Cannot set length <= 0: " + length); //$NON-NLS-1$
197
fLength= length;
198     }
199     
200     /**
201      * Sets the length of this range, keeping {@link #end()} constant.
202      *
203      * @param length the new length, must be &gt; 0 and &lt;= {@link #end()}
204      * @throws LineIndexOutOfBoundsException if <code>length</code> &lt;= 0
205      */

206     public void setLengthAndMove(int length) throws LineIndexOutOfBoundsException {
207         setStart(end() - length);
208     }
209     
210     /**
211      * Resizes the range by <code>delta</code> lines, keeping {@link #start()} constant.
212      *
213      * @param delta the number of lines to resize the range
214      * @throws LineIndexOutOfBoundsException if <code>-delta</code> &gt;= {@link #length()}
215      */

216     public void resizeBy(int delta) throws LineIndexOutOfBoundsException {
217         setLength(length() + delta);
218     }
219     
220     /**
221      * Resizes the range by <code>delta</code> lines by moving the start offset, {@link #end()} remains unchanged.
222      *
223      * @param delta the number of lines to resize the range
224      * @throws LineIndexOutOfBoundsException if <code>-delta</code> &gt;= {@link #length()}
225      */

226     public void resizeAndMoveBy(int delta) throws LineIndexOutOfBoundsException {
227         setStart(start() + delta);
228     }
229     
230     /**
231      * Splits a range off the end of the receiver. The receiver is shortened to only include
232      * <code>remaining</code> lines after the split.
233      *
234      * @param remaining the number of lines to remain in the receiver, must be in [1, {@link #length() length})
235      * @return the split off range
236      * @throws LineIndexOutOfBoundsException if <code>remaining</code>&gt;= {@link #length()} or <code>remaining</code>&ltt;= 0
237      */

238     public Range split(int remaining) throws LineIndexOutOfBoundsException {
239         if (!(remaining < length())) // assert before modification
240
throw new LineIndexOutOfBoundsException("Remaining must be less than length: " + length()); //$NON-NLS-1$
241

242         int splitLength= length() - remaining;
243         setLength(remaining);
244         return new Range(end(), splitLength);
245     }
246     
247     /**
248      * Returns <code>true</code> if the passed range has the same offset and length as the receiver.
249      *
250      * @param range another line range to compare the receiver to
251      * @return <code>true</code> if <code>range</code> has the same offset and length as the receiver
252      */

253     public boolean equalRange(ILineRange range) {
254         if (range == this)
255             return true;
256         if (range == null)
257             return false;
258         return range.getStartLine() == start() && range.getNumberOfLines() == length();
259     }
260     
261     /*
262      * @see java.lang.Object#clone()
263      */

264     public Object JavaDoc clone() {
265         return Range.copy(this);
266     }
267 }
Popular Tags