KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > text > Segment


1 /*
2  * @(#)Segment.java 1.22 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing.text;
8
9 import java.text.CharacterIterator JavaDoc;
10
11 /**
12  * A segment of a character array representing a fragment
13  * of text. It should be treated as immutable even though
14  * the array is directly accessible. This gives fast access
15  * to fragments of text without the overhead of copying
16  * around characters. This is effectively an unprotected
17  * String.
18  * <p>
19  * The Segment implements the java.text.CharacterIterator
20  * interface to support use with the i18n support without
21  * copying text into a string.
22  *
23  * @author Timothy Prinzing
24  * @version 1.22 12/19/03
25  */

26 public class Segment implements Cloneable JavaDoc, CharacterIterator JavaDoc {
27
28     /**
29      * This is the array containing the text of
30      * interest. This array should never be modified;
31      * it is available only for efficiency.
32      */

33     public char[] array;
34
35     /**
36      * This is the offset into the array that
37      * the desired text begins.
38      */

39     public int offset;
40
41     /**
42      * This is the number of array elements that
43      * make up the text of interest.
44      */

45     public int count;
46
47     private boolean partialReturn;
48
49     /**
50      * Creates a new segment.
51      */

52     public Segment() {
53     this(null, 0, 0);
54     }
55
56     /**
57      * Creates a new segment referring to an existing array.
58      *
59      * @param array the array to refer to
60      * @param offset the offset into the array
61      * @param count the number of characters
62      */

63     public Segment(char[] array, int offset, int count) {
64     this.array = array;
65     this.offset = offset;
66     this.count = count;
67     partialReturn = false;
68     }
69
70     /**
71      * Flag to indicate that partial returns are valid. If the flag is true,
72      * an implementation of the interface method Document.getText(position,length,Segment)
73      * should return as much text as possible without making a copy. The default
74      * state of the flag is false which will cause Document.getText(position,length,Segment)
75      * to provide the same return behavior it always had, which may or may not
76      * make a copy of the text depending upon the request.
77      *
78      * @param p whether or not partial returns are valid.
79      * @since 1.4
80      */

81     public void setPartialReturn(boolean p) {
82     partialReturn = p;
83     }
84
85     /**
86      * Flag to indicate that partial returns are valid.
87      *
88      * @return whether or not partial returns are valid.
89      * @since 1.4
90      */

91     public boolean isPartialReturn() {
92     return partialReturn;
93     }
94
95     /**
96      * Converts a segment into a String.
97      *
98      * @return the string
99      */

100     public String JavaDoc toString() {
101     if (array != null) {
102         return new String JavaDoc(array, offset, count);
103     }
104     return new String JavaDoc();
105     }
106
107     // --- CharacterIterator methods -------------------------------------
108

109     /**
110      * Sets the position to getBeginIndex() and returns the character at that
111      * position.
112      * @return the first character in the text, or DONE if the text is empty
113      * @see #getBeginIndex
114      */

115     public char first() {
116     pos = offset;
117     if (count != 0) {
118         return array[pos];
119     }
120     return DONE;
121     }
122
123     /**
124      * Sets the position to getEndIndex()-1 (getEndIndex() if the text is empty)
125      * and returns the character at that position.
126      * @return the last character in the text, or DONE if the text is empty
127      * @see #getEndIndex
128      */

129     public char last() {
130     pos = offset + count;
131     if (count != 0) {
132         pos -= 1;
133         return array[pos];
134     }
135     return DONE;
136     }
137
138     /**
139      * Gets the character at the current position (as returned by getIndex()).
140      * @return the character at the current position or DONE if the current
141      * position is off the end of the text.
142      * @see #getIndex
143      */

144     public char current() {
145     if (count != 0 && pos < offset + count) {
146         return array[pos];
147     }
148     return DONE;
149     }
150
151     /**
152      * Increments the iterator's index by one and returns the character
153      * at the new index. If the resulting index is greater or equal
154      * to getEndIndex(), the current index is reset to getEndIndex() and
155      * a value of DONE is returned.
156      * @return the character at the new position or DONE if the new
157      * position is off the end of the text range.
158      */

159     public char next() {
160     pos += 1;
161     int end = offset + count;
162     if (pos >= end) {
163         pos = end;
164         return DONE;
165     }
166     return current();
167     }
168
169     /**
170      * Decrements the iterator's index by one and returns the character
171      * at the new index. If the current index is getBeginIndex(), the index
172      * remains at getBeginIndex() and a value of DONE is returned.
173      * @return the character at the new position or DONE if the current
174      * position is equal to getBeginIndex().
175      */

176     public char previous() {
177     if (pos == offset) {
178         return DONE;
179     }
180     pos -= 1;
181     return current();
182     }
183
184     /**
185      * Sets the position to the specified position in the text and returns that
186      * character.
187      * @param position the position within the text. Valid values range from
188      * getBeginIndex() to getEndIndex(). An IllegalArgumentException is thrown
189      * if an invalid value is supplied.
190      * @return the character at the specified position or DONE if the specified position is equal to getEndIndex()
191      */

192     public char setIndex(int position) {
193     int end = offset + count;
194     if ((position < offset) || (position > end)) {
195         throw new IllegalArgumentException JavaDoc("bad position: " + position);
196     }
197     pos = position;
198     if ((pos != end) && (count != 0)) {
199         return array[pos];
200     }
201     return DONE;
202     }
203
204     /**
205      * Returns the start index of the text.
206      * @return the index at which the text begins.
207      */

208     public int getBeginIndex() {
209     return offset;
210     }
211
212     /**
213      * Returns the end index of the text. This index is the index of the first
214      * character following the end of the text.
215      * @return the index after the last character in the text
216      */

217     public int getEndIndex() {
218     return offset + count;
219     }
220
221     /**
222      * Returns the current index.
223      * @return the current index.
224      */

225     public int getIndex() {
226     return pos;
227     }
228
229     /**
230      * Creates a shallow copy.
231      *
232      * @return the copy
233      */

234     public Object JavaDoc clone() {
235     Object JavaDoc o;
236     try {
237         o = super.clone();
238     } catch (CloneNotSupportedException JavaDoc cnse) {
239         o = null;
240     }
241     return o;
242     }
243
244     private int pos;
245
246
247 }
248
249
250
Popular Tags