1 // 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html#License 3 /* 4 ******************************************************************************* 5 * Copyright (C) 1996-2010, International Business Machines Corporation and * 6 * others. All Rights Reserved. * 7 ******************************************************************************* 8 */ 9 10 package com.ibm.icu.impl; 11 12 import java.text.CharacterIterator; 13 14 import com.ibm.icu.text.UCharacterIterator; 15 16 /** 17 * This class is a wrapper around UCharacterIterator and implements the 18 * CharacterIterator protocol 19 * @author ram 20 */ 21 public class UCharacterIteratorWrapper implements CharacterIterator{ 22 23 public UCharacterIteratorWrapper(UCharacterIterator iter){ 24 this.iterator = iter; 25 } 26 27 private UCharacterIterator iterator; 28 29 30 /** 31 * Sets the position to getBeginIndex() and returns the character at that 32 * position. 33 * @return the first character in the text, or DONE if the text is empty 34 * @see #getBeginIndex() 35 */ 36 @Override 37 public char first(){ 38 //UCharacterIterator always iterates from 0 to length 39 iterator.setToStart(); 40 return (char)iterator.current(); 41 } 42 43 /** 44 * Sets the position to getEndIndex()-1 (getEndIndex() if the text is empty) 45 * and returns the character at that position. 46 * @return the last character in the text, or DONE if the text is empty 47 * @see #getEndIndex() 48 */ 49 @Override 50 public char last(){ 51 iterator.setToLimit(); 52 return (char)iterator.previous(); 53 } 54 55 /** 56 * Gets the character at the current position (as returned by getIndex()). 57 * @return the character at the current position or DONE if the current 58 * position is off the end of the text. 59 * @see #getIndex() 60 */ 61 @Override 62 public char current(){ 63 return (char) iterator.current(); 64 } 65 66 /** 67 * Increments the iterator's index by one and returns the character 68 * at the new index. If the resulting index is greater or equal 69 * to getEndIndex(), the current index is reset to getEndIndex() and 70 * a value of DONE is returned. 71 * @return the character at the new position or DONE if the new 72 * position is off the end of the text range. 73 */ 74 @Override 75 public char next(){ 76 //pre-increment 77 iterator.next(); 78 return (char) iterator.current(); 79 } 80 81 /** 82 * Decrements the iterator's index by one and returns the character 83 * at the new index. If the current index is getBeginIndex(), the index 84 * remains at getBeginIndex() and a value of DONE is returned. 85 * @return the character at the new position or DONE if the current 86 * position is equal to getBeginIndex(). 87 */ 88 @Override 89 public char previous(){ 90 //pre-decrement 91 return (char) iterator.previous(); 92 } 93 94 /** 95 * Sets the position to the specified position in the text and returns that 96 * character. 97 * @param position the position within the text. Valid values range from 98 * getBeginIndex() to getEndIndex(). An IllegalArgumentException is thrown 99 * if an invalid value is supplied. 100 * @return the character at the specified position or DONE if the specified position is equal to getEndIndex() 101 */ 102 @Override 103 public char setIndex(int position){ 104 iterator.setIndex(position); 105 return (char) iterator.current(); 106 } 107 108 /** 109 * Returns the start index of the text. 110 * @return the index at which the text begins. 111 */ 112 @Override 113 public int getBeginIndex(){ 114 //UCharacterIterator always starts from 0 115 return 0; 116 } 117 118 /** 119 * Returns the end index of the text. This index is the index of the first 120 * character following the end of the text. 121 * @return the index after the last character in the text 122 */ 123 @Override 124 public int getEndIndex(){ 125 return iterator.getLength(); 126 } 127 128 /** 129 * Returns the current index. 130 * @return the current index. 131 */ 132 @Override 133 public int getIndex(){ 134 return iterator.getIndex(); 135 } 136 137 /** 138 * Create a copy of this iterator 139 * @return A copy of this 140 */ 141 @Override 142 public Object clone(){ 143 try { 144 UCharacterIteratorWrapper result = (UCharacterIteratorWrapper) super.clone(); 145 result.iterator = (UCharacterIterator)this.iterator.clone(); 146 return result; 147 } catch (CloneNotSupportedException e) { 148 return null; // only invoked if bad underlying character iterator 149 } 150 } 151 152 } 153 154