]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/classes/core/src/com/ibm/icu/impl/ReplaceableUCharacterIterator.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / classes / core / src / com / ibm / icu / impl / ReplaceableUCharacterIterator.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 package com.ibm.icu.impl;\r
8 \r
9 import com.ibm.icu.text.Replaceable;\r
10 import com.ibm.icu.text.ReplaceableString;\r
11 import com.ibm.icu.text.UCharacterIterator;\r
12 import com.ibm.icu.text.UTF16;\r
13 \r
14 /**\r
15  * DLF docs must define behavior when Replaceable is mutated underneath\r
16  * the iterator.\r
17  *\r
18  * This and ICUCharacterIterator share some code, maybe they should share\r
19  * an implementation, or the common state and implementation should be\r
20  * moved up into UCharacterIterator.\r
21  *\r
22  * What are first, last, and getBeginIndex doing here?!?!?!\r
23  */\r
24 public class ReplaceableUCharacterIterator extends UCharacterIterator {\r
25 \r
26     // public constructor ------------------------------------------------------\r
27     \r
28     /**\r
29      * Public constructor\r
30      * @param replaceable text which the iterator will be based on\r
31      */\r
32     public ReplaceableUCharacterIterator(Replaceable replaceable){\r
33         if(replaceable==null){\r
34             throw new IllegalArgumentException();\r
35         }\r
36         this.replaceable  = replaceable;\r
37         this.currentIndex = 0;\r
38     }\r
39     \r
40     /**\r
41      * Public constructor\r
42      * @param str text which the iterator will be based on\r
43      */\r
44     public ReplaceableUCharacterIterator(String str){\r
45         if(str==null){\r
46             throw new IllegalArgumentException();\r
47         }\r
48         this.replaceable  = new ReplaceableString(str);\r
49         this.currentIndex = 0;\r
50     }\r
51     \r
52     /**\r
53      * Public constructor\r
54      * @param buf buffer of text on which the iterator will be based\r
55      */\r
56     public ReplaceableUCharacterIterator(StringBuffer buf){\r
57         if(buf==null){\r
58             throw new IllegalArgumentException();\r
59         }\r
60         this.replaceable  = new ReplaceableString(buf);\r
61         this.currentIndex = 0;\r
62     }\r
63     \r
64     // public methods ----------------------------------------------------------\r
65     \r
66     /**\r
67      * Creates a copy of this iterator, does not clone the underlying \r
68      * <code>Replaceable</code>object\r
69      * @return copy of this iterator\r
70      */\r
71     public Object clone(){\r
72         try {\r
73           return super.clone();\r
74         } catch (CloneNotSupportedException e) {\r
75             return null; // never invoked\r
76         }\r
77     }\r
78     \r
79     /**\r
80      * Returns the current UTF16 character.\r
81      * @return current UTF16 character\r
82      */\r
83     public int current(){\r
84         if (currentIndex < replaceable.length()) {\r
85             return replaceable.charAt(currentIndex);\r
86         }\r
87         return DONE;\r
88     }\r
89     \r
90     /**\r
91      * Returns the current codepoint\r
92      * @return current codepoint\r
93      */\r
94     public int currentCodePoint(){\r
95         // cannot use charAt due to it different \r
96         // behaviour when index is pointing at a\r
97         // trail surrogate, check for surrogates\r
98          \r
99         int ch = current();\r
100         if(UTF16.isLeadSurrogate((char)ch)){\r
101             // advance the index to get the next code point\r
102             next();\r
103             // due to post increment semantics current() after next() \r
104             // actually returns the next char which is what we want\r
105             int ch2 = current();\r
106             // current should never change the current index so back off\r
107             previous();\r
108             \r
109             if(UTF16.isTrailSurrogate((char)ch2)){\r
110                 // we found a surrogate pair\r
111                 return UCharacterProperty.getRawSupplementary(\r
112                                                          (char)ch,(char)ch2\r
113                                                              );\r
114             }\r
115         }\r
116         return ch;\r
117     }\r
118     \r
119     /**\r
120      * Returns the length of the text\r
121      * @return length of the text\r
122      */\r
123     public int getLength(){\r
124         return replaceable.length();\r
125     }\r
126     \r
127     /**\r
128      * Gets the current currentIndex in text.\r
129      * @return current currentIndex in text.\r
130      */\r
131     public int getIndex(){\r
132         return currentIndex;\r
133     }\r
134         \r
135     /**\r
136      * Returns next UTF16 character and increments the iterator's currentIndex by 1. \r
137      * If the resulting currentIndex is greater or equal to the text length, the \r
138      * currentIndex is reset to the text length and a value of DONECODEPOINT is \r
139      * returned. \r
140      * @return next UTF16 character in text or DONE if the new currentIndex is off the \r
141      *         end of the text range.\r
142      */\r
143     public int next(){\r
144         if (currentIndex < replaceable.length()) {\r
145             return replaceable.charAt(currentIndex++);\r
146         }\r
147         return DONE;\r
148     }\r
149     \r
150                 \r
151     /**\r
152      * Returns previous UTF16 character and decrements the iterator's currentIndex by \r
153      * 1. \r
154      * If the resulting currentIndex is less than 0, the currentIndex is reset to 0 and a \r
155      * value of DONECODEPOINT is returned. \r
156      * @return next UTF16 character in text or DONE if the new currentIndex is off the \r
157      *         start of the text range.\r
158      */\r
159     public int previous(){\r
160         if (currentIndex > 0) {\r
161             return replaceable.charAt(--currentIndex);\r
162         }\r
163         return DONE;\r
164     }\r
165 \r
166     /**\r
167      * <p>Sets the currentIndex to the specified currentIndex in the text and returns that \r
168      * single UTF16 character at currentIndex. \r
169      * This assumes the text is stored as 16-bit code units.</p>\r
170      * @param currentIndex the currentIndex within the text. \r
171      * @exception IllegalArgumentException is thrown if an invalid currentIndex is \r
172      *            supplied. i.e. currentIndex is out of bounds.\r
173      * @returns the character at the specified currentIndex or DONE if the specified \r
174      *         currentIndex is equal to the end of the text.\r
175      */\r
176     public void setIndex(int currentIndex) throws IndexOutOfBoundsException{\r
177         if (currentIndex < 0 || currentIndex > replaceable.length()) {\r
178             throw new IndexOutOfBoundsException();\r
179         }\r
180         this.currentIndex = currentIndex;\r
181     }\r
182     \r
183     public int getText(char[] fillIn, int offset){\r
184         int length = replaceable.length();\r
185         if(offset < 0 || offset + length > fillIn.length){\r
186             throw new IndexOutOfBoundsException(Integer.toString(length));\r
187         }\r
188         replaceable.getChars(0,length,fillIn,offset);\r
189         return length;\r
190     }       \r
191         \r
192     // private data members ----------------------------------------------------\r
193     \r
194     /**\r
195      * Replacable object\r
196      */\r
197     private Replaceable replaceable;\r
198     /**\r
199      * Current currentIndex\r
200      */\r
201     private int currentIndex;\r
202 \r
203 }\r