2 *******************************************************************************
3 * Copyright (C) 1996-2010, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
8 package com.ibm.icu.impl;
10 import java.text.CharacterIterator;
12 import com.ibm.icu.text.UCharacterIterator;
15 * This class is a wrapper around UCharacterIterator and implements the
16 * CharacterIterator protocol
19 public class UCharacterIteratorWrapper implements CharacterIterator{
21 public UCharacterIteratorWrapper(UCharacterIterator iter){
25 private UCharacterIterator iterator;
29 * Sets the position to getBeginIndex() and returns the character at that
31 * @return the first character in the text, or DONE if the text is empty
32 * @see #getBeginIndex()
35 //UCharacterIterator always iterates from 0 to length
36 iterator.setToStart();
37 return (char)iterator.current();
41 * Sets the position to getEndIndex()-1 (getEndIndex() if the text is empty)
42 * and returns the character at that position.
43 * @return the last character in the text, or DONE if the text is empty
47 iterator.setToLimit();
48 return (char)iterator.previous();
52 * Gets the character at the current position (as returned by getIndex()).
53 * @return the character at the current position or DONE if the current
54 * position is off the end of the text.
57 public char current(){
58 return (char) iterator.current();
62 * Increments the iterator's index by one and returns the character
63 * at the new index. If the resulting index is greater or equal
64 * to getEndIndex(), the current index is reset to getEndIndex() and
65 * a value of DONE is returned.
66 * @return the character at the new position or DONE if the new
67 * position is off the end of the text range.
72 return (char) iterator.current();
76 * Decrements the iterator's index by one and returns the character
77 * at the new index. If the current index is getBeginIndex(), the index
78 * remains at getBeginIndex() and a value of DONE is returned.
79 * @return the character at the new position or DONE if the current
80 * position is equal to getBeginIndex().
82 public char previous(){
84 return (char) iterator.previous();
88 * Sets the position to the specified position in the text and returns that
90 * @param position the position within the text. Valid values range from
91 * getBeginIndex() to getEndIndex(). An IllegalArgumentException is thrown
92 * if an invalid value is supplied.
93 * @return the character at the specified position or DONE if the specified position is equal to getEndIndex()
95 public char setIndex(int position){
96 iterator.setIndex(position);
97 return (char) iterator.current();
101 * Returns the start index of the text.
102 * @return the index at which the text begins.
104 public int getBeginIndex(){
105 //UCharacterIterator always starts from 0
110 * Returns the end index of the text. This index is the index of the first
111 * character following the end of the text.
112 * @return the index after the last character in the text
114 public int getEndIndex(){
115 return iterator.getLength();
119 * Returns the current index.
120 * @return the current index.
122 public int getIndex(){
123 return iterator.getIndex();
127 * Create a copy of this iterator
128 * @return A copy of this
130 public Object clone(){
132 UCharacterIteratorWrapper result = (UCharacterIteratorWrapper) super.clone();
133 result.iterator = (UCharacterIterator)this.iterator.clone();
135 } catch (CloneNotSupportedException e) {
136 return null; // only invoked if bad underlying character iterator