]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/classes/core/src/com/ibm/icu/impl/UCharacterIteratorWrapper.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / classes / core / src / com / ibm / icu / impl / UCharacterIteratorWrapper.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 1996-2010, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7  \r
8 package com.ibm.icu.impl;\r
9 \r
10 import java.text.CharacterIterator;\r
11 \r
12 import com.ibm.icu.text.UCharacterIterator;\r
13 \r
14 /**\r
15  * This class is a wrapper around UCharacterIterator and implements the \r
16  * CharacterIterator protocol\r
17  * @author ram\r
18  */\r
19 public class UCharacterIteratorWrapper implements CharacterIterator{\r
20     \r
21     public UCharacterIteratorWrapper(UCharacterIterator iter){\r
22         this.iterator = iter;\r
23     }\r
24     \r
25     private UCharacterIterator iterator;\r
26 \r
27 \r
28     /**\r
29      * Sets the position to getBeginIndex() and returns the character at that\r
30      * position.\r
31      * @return the first character in the text, or DONE if the text is empty\r
32      * @see #getBeginIndex()\r
33      */\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
38     }\r
39 \r
40     /**\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
45      */\r
46     public char last(){\r
47         iterator.setToLimit();\r
48         return (char)iterator.previous();\r
49     }\r
50 \r
51     /**\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
55      * @see #getIndex()\r
56      */\r
57     public char current(){\r
58         return (char) iterator.current();\r
59     }\r
60 \r
61     /**\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
68      */\r
69     public char next(){\r
70         //pre-increment\r
71         iterator.next();\r
72         return (char) iterator.current();\r
73     }\r
74 \r
75     /**\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
81      */\r
82     public char previous(){\r
83         //pre-decrement\r
84         return (char) iterator.previous();\r
85     }\r
86 \r
87     /**\r
88      * Sets the position to the specified position in the text and returns that\r
89      * character.\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
94      */\r
95     public char setIndex(int position){\r
96         iterator.setIndex(position);\r
97         return (char) iterator.current();\r
98     }\r
99 \r
100     /**\r
101      * Returns the start index of the text.\r
102      * @return the index at which the text begins.\r
103      */\r
104     public int getBeginIndex(){\r
105         //UCharacterIterator always starts from 0\r
106         return 0;\r
107     }\r
108 \r
109     /**\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
113      */\r
114     public int getEndIndex(){\r
115         return iterator.getLength();\r
116     }\r
117 \r
118     /**\r
119      * Returns the current index.\r
120      * @return the current index.\r
121      */\r
122     public int getIndex(){\r
123         return iterator.getIndex();\r
124     }\r
125 \r
126     /**\r
127      * Create a copy of this iterator\r
128      * @return A copy of this\r
129      */\r
130     public Object clone(){\r
131         try {\r
132             UCharacterIteratorWrapper result = (UCharacterIteratorWrapper) super.clone();\r
133             result.iterator = (UCharacterIterator)this.iterator.clone();\r
134             return result;\r
135         } catch (CloneNotSupportedException e) {      \r
136             return null; // only invoked if bad underlying character iterator\r
137         }\r
138     }   \r
139 \r
140 }\r
141 \r