2 *******************************************************************************
3 * Copyright (C) 1996-2006, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.impl;
9 import com.ibm.icu.text.UCharacterIterator;
12 * Used by Collation. UCharacterIterator on Strings. Can't use
13 * ReplaceableUCharacterIterator because it is not easy to do a fast setText.
16 // TODO: Investivate if setText is a feature required by users so that we can
17 // move this method to the base class!
18 public final class StringUCharacterIterator extends UCharacterIterator
21 // public constructor ------------------------------------------------------
25 * @param str text which the iterator will be based on
27 public StringUCharacterIterator(String str)
30 throw new IllegalArgumentException();
37 * Public default constructor
39 public StringUCharacterIterator()
45 // public methods ----------------------------------------------------------
48 * Creates a copy of this iterator, does not clone the underlying
49 * <code>String</code>object
50 * @return copy of this iterator
57 } catch (CloneNotSupportedException e) {
58 return null; // never invoked
63 * Returns the current UTF16 character.
64 * @return current UTF16 character
68 if (m_currentIndex_ < m_text_.length()) {
69 return m_text_.charAt(m_currentIndex_);
76 * Returns the length of the text
77 * @return length of the text
79 public int getLength()
81 return m_text_.length();
85 * Gets the current currentIndex in text.
86 * @return current currentIndex in text.
90 return m_currentIndex_;
94 * Returns next UTF16 character and increments the iterator's currentIndex
96 * If the resulting currentIndex is greater or equal to the text length,
97 * the currentIndex is reset to the text length and a value of DONE is
99 * @return next UTF16 character in text or DONE if the new currentIndex is
100 * off the end of the text range.
104 if (m_currentIndex_ < m_text_.length())
106 return m_text_.charAt(m_currentIndex_ ++);
113 * Returns previous UTF16 character and decrements the iterator's
115 * If the resulting currentIndex is less than 0, the currentIndex is reset
116 * to 0 and a value of DONE is returned.
117 * @return next UTF16 character in text or DONE if the new currentIndex is
118 * off the start of the text range.
120 public int previous()
122 if (m_currentIndex_ > 0) {
123 return m_text_.charAt(-- m_currentIndex_);
129 * <p>Sets the currentIndex to the specified currentIndex in the text and
130 * returns that single UTF16 character at currentIndex.
131 * This assumes the text is stored as 16-bit code units.</p>
132 * @param currentIndex the currentIndex within the text.
133 * @exception IndexOutOfBoundsException is thrown if an invalid currentIndex
134 * is supplied. i.e. currentIndex is out of bounds.
136 public void setIndex(int currentIndex) throws IndexOutOfBoundsException
138 if (currentIndex < 0 || currentIndex > m_text_.length()) {
139 throw new IndexOutOfBoundsException();
141 m_currentIndex_ = currentIndex;
145 * Fills the buffer with the underlying text storage of the iterator
146 * If the buffer capacity is not enough a exception is thrown. The capacity
147 * of the fill in buffer should at least be equal to length of text in the
148 * iterator obtained by calling <code>getLength()</code).
153 * UChacterIterator iter = new UCharacterIterator.getInstance(text);
154 * char[] buf = new char[iter.getLength()];
158 * char[] buf= new char[1];
162 * len = iter.getText(buf);
164 * }catch(IndexOutOfBoundsException e){
165 * buf = new char[iter.getLength()];
171 * @param fillIn an array of chars to fill with the underlying UTF-16 code
173 * @param offset the position within the array to start putting the data.
174 * @return the number of code units added to fillIn, as a convenience
175 * @exception IndexOutOfBoundsException exception if there is not enough
176 * room after offset in the array, or if offset < 0.
179 public int getText(char[] fillIn, int offset)
181 int length = m_text_.length();
182 if (offset < 0 || offset + length > fillIn.length) {
183 throw new IndexOutOfBoundsException(Integer.toString(length));
185 m_text_.getChars(0, length, fillIn, offset);
190 * Convenience method for returning the underlying text storage as as
192 * @return the underlying text storage in the iterator as a string
194 public String getText()
200 * Reset this iterator to point to a new string. This method is used by
201 * other classes that want to avoid allocating new
202 * ReplaceableCharacterIterator objects every time their setText method
204 * @param text The String to be iterated over
206 public void setText(String text)
209 throw new NullPointerException();
215 // private data members ----------------------------------------------------
220 private String m_text_;
222 * Current currentIndex
224 private int m_currentIndex_;