Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / javax / swing / text / Segment.java
blob875d9966c1fa1917475536923a3ee6321acad31d
1 /* Segment.java --
2 Copyright (C) 2002, 2004, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package javax.swing.text;
40 import java.text.CharacterIterator;
42 /**
43 * A text fragment represented by a sequence of characters stored in an array.
45 public class Segment implements Cloneable, CharacterIterator
47 private boolean partialReturn;
49 /** The current index. */
50 private int current;
52 /** Storage for the characters (may contain additional characters). */
53 public char[] array;
55 /** The number of characters in the segment. */
56 public int count;
58 /** The offset of the first character in the segment. */
59 public int offset;
61 /**
62 * Creates a new <code>Segment</code>.
64 public Segment()
66 // Nothing to do here.
69 /**
70 * Creates a new <code>Segment</code>.
72 * @param array the underlying character data.
73 * @param offset the offset of the first character in the segment.
74 * @param count the number of characters in the segment.
76 public Segment(char[] array, int offset, int count)
78 this.array = array;
79 this.offset = offset;
80 this.count = count;
83 /**
84 * Clones the segment (note that the underlying character array is not cloned,
85 * just the reference to it).
87 * @return A clone of the segment.
89 public Object clone()
91 try
93 return super.clone();
95 catch (CloneNotSupportedException e)
97 return null;
102 * Returns the character at the current index. If the segment consists of
103 * zero characters, or the current index has passed the end of the
104 * characters in the segment, this method returns {@link #DONE}.
106 * @return The character at the current index.
108 public char current()
110 if (count == 0
111 || current >= getEndIndex())
112 return DONE;
114 return array[current];
118 * Sets the current index to the first character in the segment and returns
119 * that character. If the segment contains zero characters, this method
120 * returns {@link #DONE}.
122 * @return The first character in the segment, or {@link #DONE} if the
123 * segment contains zero characters.
125 public char first()
127 if (count == 0)
128 return DONE;
130 current = getBeginIndex();
131 return array[current];
135 * Returns the index of the first character in the segment.
137 * @return The index of the first character.
139 public int getBeginIndex()
141 return offset;
145 * Returns the end index for the segment (one position beyond the last
146 * character in the segment - note that this can be outside the range of the
147 * underlying character array).
149 * @return The end index for the segment.
151 public int getEndIndex()
153 return offset + count;
157 * Returns the index of the current character in the segment.
159 * @return The index of the current character.
161 public int getIndex()
163 return current;
167 * Sets the current index to point to the last character in the segment and
168 * returns that character. If the segment contains zero characters, this
169 * method returns {@link #DONE}.
171 * @return The last character in the segment, or {@link #DONE} if the
172 * segment contains zero characters.
174 public char last()
176 if (count == 0)
177 return DONE;
179 current = getEndIndex() - 1;
180 return array[current];
184 * Sets the current index to point to the next character in the segment and
185 * returns that character. If the next character position is past the end of
186 * the segment, the index is set to {@link #getEndIndex()} and the method
187 * returns {@link #DONE}. If the segment contains zero characters, this
188 * method returns {@link #DONE}.
190 * @return The next character in the segment or {@link #DONE} (if the next
191 * character position is past the end of the segment or if the
192 * segment contains zero characters).
194 public char next()
196 if (count == 0)
197 return DONE;
199 if ((current + 1) >= getEndIndex())
201 current = getEndIndex();
202 return DONE;
205 current++;
206 return array[current];
210 * Sets the current index to point to the previous character in the segment
211 * and returns that character. If the current index is equal to
212 * {@link #getBeginIndex()}, or if the segment contains zero characters, this
213 * method returns {@link #DONE}.
215 * @return The previous character in the segment or {@link #DONE} (if the
216 * current character position is at the beginning of the segment or
217 * if the segment contains zero characters).
219 public char previous()
221 if (count == 0
222 || current == getBeginIndex())
223 return DONE;
225 current--;
226 return array[current];
230 * Sets the current index and returns the character at that position (or
231 * {@link #DONE} if the index is equal to {@link #getEndIndex()}.
233 * @param position the current position.
235 * @return The character at the specified <code>position</code>, or
236 * {@link #DONE} if <code>position</code> is equal to
237 * {@link #getEndIndex()}.
239 * @throws IllegalArgumentException if <code>position</code> is not in the
240 * range {@link #getBeginIndex()} to {@link #getEndIndex()}.
242 public char setIndex(int position)
244 if (position < getBeginIndex()
245 || position > getEndIndex())
246 throw new IllegalArgumentException("position: " + position
247 + ", beginIndex: " + getBeginIndex()
248 + ", endIndex: " + getEndIndex());
250 current = position;
252 if (position == getEndIndex())
253 return DONE;
255 return array[current];
259 * Returns a <code>String</code> containing the same characters as this
260 * <code>Segment</code>.
262 * @return A <code>String</code> containing the same characters as this
263 * <code>Segment</code>.
265 public String toString()
267 return new String(array, offset, count);
271 * Sets the partial return flag.
273 * @param p the new value of the flag.
275 * @since 1.4
277 public void setPartialReturn(boolean p)
279 partialReturn = p;
283 * Returns the partial return flag.
285 * @return The partial return flag.
286 * @since 1.4
288 public boolean isPartialReturn()
290 return partialReturn;