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