2 *******************************************************************************
3 * Copyright (C) 1996-2010, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.impl;
9 import com.ibm.icu.text.Replaceable;
10 import com.ibm.icu.text.ReplaceableString;
11 import com.ibm.icu.text.UCharacterIterator;
12 import com.ibm.icu.text.UTF16;
15 * DLF docs must define behavior when Replaceable is mutated underneath
18 * This and ICUCharacterIterator share some code, maybe they should share
19 * an implementation, or the common state and implementation should be
20 * moved up into UCharacterIterator.
22 * What are first, last, and getBeginIndex doing here?!?!?!
24 public class ReplaceableUCharacterIterator extends UCharacterIterator {
26 // public constructor ------------------------------------------------------
30 * @param replaceable text which the iterator will be based on
32 public ReplaceableUCharacterIterator(Replaceable replaceable){
33 if(replaceable==null){
34 throw new IllegalArgumentException();
36 this.replaceable = replaceable;
37 this.currentIndex = 0;
42 * @param str text which the iterator will be based on
44 public ReplaceableUCharacterIterator(String str){
46 throw new IllegalArgumentException();
48 this.replaceable = new ReplaceableString(str);
49 this.currentIndex = 0;
54 * @param buf buffer of text on which the iterator will be based
56 public ReplaceableUCharacterIterator(StringBuffer buf){
58 throw new IllegalArgumentException();
60 this.replaceable = new ReplaceableString(buf);
61 this.currentIndex = 0;
64 // public methods ----------------------------------------------------------
67 * Creates a copy of this iterator, does not clone the underlying
68 * <code>Replaceable</code>object
69 * @return copy of this iterator
71 public Object clone(){
74 } catch (CloneNotSupportedException e) {
75 return null; // never invoked
80 * Returns the current UTF16 character.
81 * @return current UTF16 character
84 if (currentIndex < replaceable.length()) {
85 return replaceable.charAt(currentIndex);
91 * Returns the current codepoint
92 * @return current codepoint
94 public int currentCodePoint(){
95 // cannot use charAt due to it different
96 // behaviour when index is pointing at a
97 // trail surrogate, check for surrogates
100 if(UTF16.isLeadSurrogate((char)ch)){
101 // advance the index to get the next code point
103 // due to post increment semantics current() after next()
104 // actually returns the next char which is what we want
106 // current should never change the current index so back off
109 if(UTF16.isTrailSurrogate((char)ch2)){
110 // we found a surrogate pair
111 return UCharacterProperty.getRawSupplementary(
120 * Returns the length of the text
121 * @return length of the text
123 public int getLength(){
124 return replaceable.length();
128 * Gets the current currentIndex in text.
129 * @return current currentIndex in text.
131 public int getIndex(){
136 * Returns next UTF16 character and increments the iterator's currentIndex by 1.
137 * If the resulting currentIndex is greater or equal to the text length, the
138 * currentIndex is reset to the text length and a value of DONECODEPOINT is
140 * @return next UTF16 character in text or DONE if the new currentIndex is off the
141 * end of the text range.
144 if (currentIndex < replaceable.length()) {
145 return replaceable.charAt(currentIndex++);
152 * Returns previous UTF16 character and decrements the iterator's currentIndex by
154 * If the resulting currentIndex is less than 0, the currentIndex is reset to 0 and a
155 * value of DONECODEPOINT is returned.
156 * @return next UTF16 character in text or DONE if the new currentIndex is off the
157 * start of the text range.
159 public int previous(){
160 if (currentIndex > 0) {
161 return replaceable.charAt(--currentIndex);
167 * <p>Sets the currentIndex to the specified currentIndex in the text and returns that
168 * single UTF16 character at currentIndex.
169 * This assumes the text is stored as 16-bit code units.</p>
170 * @param currentIndex the currentIndex within the text.
171 * @exception IllegalArgumentException is thrown if an invalid currentIndex is
172 * supplied. i.e. currentIndex is out of bounds.
173 * @returns the character at the specified currentIndex or DONE if the specified
174 * currentIndex is equal to the end of the text.
176 public void setIndex(int currentIndex) throws IndexOutOfBoundsException{
177 if (currentIndex < 0 || currentIndex > replaceable.length()) {
178 throw new IndexOutOfBoundsException();
180 this.currentIndex = currentIndex;
183 public int getText(char[] fillIn, int offset){
184 int length = replaceable.length();
185 if(offset < 0 || offset + length > fillIn.length){
186 throw new IndexOutOfBoundsException(Integer.toString(length));
188 replaceable.getChars(0,length,fillIn,offset);
192 // private data members ----------------------------------------------------
197 private Replaceable replaceable;
199 * Current currentIndex
201 private int currentIndex;