KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > impl > CharacterIteratorWrapper


1 /*
2  *******************************************************************************
3  * Copyright (C) 1996-2004, International Business Machines Corporation and *
4  * others. All Rights Reserved. *
5  *******************************************************************************
6  */

7 package com.ibm.icu.impl;
8
9 import java.text.CharacterIterator JavaDoc;
10
11 import com.ibm.icu.text.*;
12
13 /**
14  * This class is a wrapper around CharacterIterator and implements the
15  * UCharacterIterator protocol
16  * @author ram
17  */

18
19 public class CharacterIteratorWrapper extends UCharacterIterator {
20     
21     private CharacterIterator JavaDoc iterator;
22     
23     
24     public CharacterIteratorWrapper(CharacterIterator JavaDoc iter){
25         if(iter==null){
26             throw new IllegalArgumentException JavaDoc();
27         }
28         iterator = iter;
29     }
30
31     /**
32      * @see UCharacterIterator#current()
33      */

34     public int current() {
35         int c = iterator.current();
36         if(c==CharacterIterator.DONE){
37           return DONE;
38         }
39         return c;
40     }
41
42     /**
43      * @see UCharacterIterator#getLength()
44      */

45     public int getLength() {
46         return (iterator.getEndIndex() - iterator.getBeginIndex());
47     }
48
49     /**
50      * @see UCharacterIterator#getIndex()
51      */

52     public int getIndex() {
53         return iterator.getIndex();
54     }
55
56     /**
57      * @see UCharacterIterator#next()
58      */

59     public int next() {
60         int i = iterator.current();
61         iterator.next();
62         if(i==CharacterIterator.DONE){
63           return DONE;
64         }
65         return i;
66     }
67
68     /**
69      * @see UCharacterIterator#previous()
70      */

71     public int previous() {
72         int i = iterator.previous();
73         if(i==CharacterIterator.DONE){
74             return DONE;
75         }
76         return i;
77     }
78
79     /**
80      * @see UCharacterIterator#setIndex(int)
81      */

82     public void setIndex(int index) {
83         try{
84             iterator.setIndex(index);
85         }catch(IllegalArgumentException JavaDoc e){
86             throw new IndexOutOfBoundsException JavaDoc();
87         }
88     }
89
90     /**
91      * @see UCharacterIterator#setToLimit()
92      */

93     public void setToLimit() {
94         iterator.setIndex(iterator.getEndIndex());
95     }
96
97     /**
98      * @see UCharacterIterator#getText(char[])
99      */

100     public int getText(char[] fillIn, int offset){
101         int length =iterator.getEndIndex() - iterator.getBeginIndex();
102         int currentIndex = iterator.getIndex();
103         if(offset < 0 || offset + length > fillIn.length){
104             throw new IndexOutOfBoundsException JavaDoc(Integer.toString(length));
105         }
106     
107         for (char ch = iterator.first(); ch != CharacterIterator.DONE; ch = iterator.next()) {
108             fillIn[offset++] = ch;
109         }
110         iterator.setIndex(currentIndex);
111
112         return length;
113     }
114
115     /**
116      * Creates a clone of this iterator. Clones the underlying character iterator.
117      * @see UCharacterIterator#clone()
118      */

119     public Object JavaDoc clone(){
120         try {
121             CharacterIteratorWrapper result = (CharacterIteratorWrapper) super.clone();
122             result.iterator = (CharacterIterator JavaDoc)this.iterator.clone();
123             return result;
124         } catch (CloneNotSupportedException JavaDoc e) {
125             return null; // only invoked if bad underlying character iterator
126
}
127     }
128     
129
130     public int moveIndex(int delta){
131         int length = iterator.getEndIndex() - iterator.getBeginIndex();
132         int idx = iterator.getIndex()+delta;
133         
134         if(idx < 0) {
135             idx = 0;
136         } else if(idx > length) {
137             idx = length;
138         }
139         return iterator.setIndex(idx);
140     }
141     
142     /**
143      * @see UCharacterIterator#getCharacterIterator()
144      */

145     public CharacterIterator JavaDoc getCharacterIterator(){
146         return (CharacterIterator JavaDoc)iterator.clone();
147     }
148 }
149
Popular Tags