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