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