001: /*
002: *******************************************************************************
003: * Copyright (C) 1996-2004, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007: package com.ibm.icu.impl;
008:
009: import java.text.CharacterIterator;
010:
011: import com.ibm.icu.text.*;
012:
013: /**
014: * This class is a wrapper around CharacterIterator and implements the
015: * UCharacterIterator protocol
016: * @author ram
017: */
018:
019: public class CharacterIteratorWrapper extends UCharacterIterator {
020:
021: private CharacterIterator iterator;
022:
023: public CharacterIteratorWrapper(CharacterIterator iter) {
024: if (iter == null) {
025: throw new IllegalArgumentException();
026: }
027: iterator = iter;
028: }
029:
030: /**
031: * @see UCharacterIterator#current()
032: */
033: public int current() {
034: int c = iterator.current();
035: if (c == CharacterIterator.DONE) {
036: return DONE;
037: }
038: return c;
039: }
040:
041: /**
042: * @see UCharacterIterator#getLength()
043: */
044: public int getLength() {
045: return (iterator.getEndIndex() - iterator.getBeginIndex());
046: }
047:
048: /**
049: * @see UCharacterIterator#getIndex()
050: */
051: public int getIndex() {
052: return iterator.getIndex();
053: }
054:
055: /**
056: * @see UCharacterIterator#next()
057: */
058: public int next() {
059: int i = iterator.current();
060: iterator.next();
061: if (i == CharacterIterator.DONE) {
062: return DONE;
063: }
064: return i;
065: }
066:
067: /**
068: * @see UCharacterIterator#previous()
069: */
070: public int previous() {
071: int i = iterator.previous();
072: if (i == CharacterIterator.DONE) {
073: return DONE;
074: }
075: return i;
076: }
077:
078: /**
079: * @see UCharacterIterator#setIndex(int)
080: */
081: public void setIndex(int index) {
082: try {
083: iterator.setIndex(index);
084: } catch (IllegalArgumentException e) {
085: throw new IndexOutOfBoundsException();
086: }
087: }
088:
089: /**
090: * @see UCharacterIterator#setToLimit()
091: */
092: public void setToLimit() {
093: iterator.setIndex(iterator.getEndIndex());
094: }
095:
096: /**
097: * @see UCharacterIterator#getText(char[])
098: */
099: public int getText(char[] fillIn, int offset) {
100: int length = iterator.getEndIndex() - iterator.getBeginIndex();
101: int currentIndex = iterator.getIndex();
102: if (offset < 0 || offset + length > fillIn.length) {
103: throw new IndexOutOfBoundsException(Integer
104: .toString(length));
105: }
106:
107: for (char ch = iterator.first(); ch != CharacterIterator.DONE; ch = iterator
108: .next()) {
109: fillIn[offset++] = ch;
110: }
111: iterator.setIndex(currentIndex);
112:
113: return length;
114: }
115:
116: /**
117: * Creates a clone of this iterator. Clones the underlying character iterator.
118: * @see UCharacterIterator#clone()
119: */
120: public Object clone() {
121: try {
122: CharacterIteratorWrapper result = (CharacterIteratorWrapper) super
123: .clone();
124: result.iterator = (CharacterIterator) this .iterator.clone();
125: return result;
126: } catch (CloneNotSupportedException e) {
127: return null; // only invoked if bad underlying character iterator
128: }
129: }
130:
131: public int moveIndex(int delta) {
132: int length = iterator.getEndIndex() - iterator.getBeginIndex();
133: int idx = iterator.getIndex() + delta;
134:
135: if (idx < 0) {
136: idx = 0;
137: } else if (idx > length) {
138: idx = length;
139: }
140: return iterator.setIndex(idx);
141: }
142:
143: /**
144: * @see UCharacterIterator#getCharacterIterator()
145: */
146: public CharacterIterator getCharacterIterator() {
147: return (CharacterIterator) iterator.clone();
148: }
149: }
|