KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

19 public class UCharacterIteratorWrapper implements CharacterIterator JavaDoc{
20     
21     public UCharacterIteratorWrapper(UCharacterIterator iter){
22         this.iterator = iter;
23     }
24     
25     private UCharacterIterator iterator;
26
27
28     /**
29      * Sets the position to getBeginIndex() and returns the character at that
30      * position.
31      * @return the first character in the text, or DONE if the text is empty
32      * @see #getBeginIndex()
33      */

34     public char first(){
35         //UCharacterIterator always iterates from 0 to length
36
iterator.setToStart();
37         return (char)iterator.current();
38     }
39
40     /**
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
44      * @see #getEndIndex()
45      */

46     public char last(){
47         iterator.setToLimit();
48         return (char)iterator.previous();
49     }
50
51     /**
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.
55      * @see #getIndex()
56      */

57     public char current(){
58         return (char) iterator.current();
59     }
60
61     /**
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.
68      */

69     public char next(){
70         //pre-increment
71
iterator.next();
72         return (char) iterator.current();
73     }
74
75     /**
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().
81      */

82     public char previous(){
83         //pre-decrement
84
return (char) iterator.previous();
85     }
86
87     /**
88      * Sets the position to the specified position in the text and returns that
89      * character.
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()
94      */

95     public char setIndex(int position){
96         iterator.setIndex(position);
97         return (char) iterator.current();
98     }
99
100     /**
101      * Returns the start index of the text.
102      * @return the index at which the text begins.
103      */

104     public int getBeginIndex(){
105         //UCharacterIterator always starts from 0
106
return 0;
107     }
108
109     /**
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
113      */

114     public int getEndIndex(){
115         return iterator.getLength();
116     }
117
118     /**
119      * Returns the current index.
120      * @return the current index.
121      */

122     public int getIndex(){
123         return iterator.getIndex();
124     }
125
126     /**
127      * Create a copy of this iterator
128      * @return A copy of this
129      */

130     public Object JavaDoc clone(){
131         try {
132             UCharacterIteratorWrapper result = (UCharacterIteratorWrapper) super.clone();
133             result.iterator = (UCharacterIterator)this.iterator.clone();
134             return result;
135         } catch (CloneNotSupportedException JavaDoc e) {
136             return null; // only invoked if bad underlying character iterator
137
}
138     }
139
140 }
141
142
Popular Tags