]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/tests/translit/src/com/ibm/icu/dev/test/util/UnicodeMapIterator.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / tests / translit / src / com / ibm / icu / dev / test / util / UnicodeMapIterator.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.dev.test.util;\r
8 \r
9 import java.util.Iterator;\r
10 import java.util.Set;\r
11 \r
12 import com.ibm.icu.text.UTF16;\r
13 \r
14 /**\r
15  * UnicodeSetIterator iterates over the contents of a UnicodeSet.  It\r
16  * iterates over either code points or code point ranges.  After all\r
17  * code points or ranges have been returned, it returns the\r
18  * multicharacter strings of the UnicodSet, if any.\r
19  *\r
20  * <p>To iterate over code points, use a loop like this:\r
21  * <pre>\r
22  * UnicodeSetIterator it = new UnicodeSetIterator(set);\r
23  * while (set.next()) {\r
24  *   if (set.codepoint != UnicodeSetIterator.IS_STRING) {\r
25  *     processCodepoint(set.codepoint);\r
26  *   } else {\r
27  *     processString(set.string);\r
28  *   }\r
29  * }\r
30  * </pre>\r
31  *\r
32  * <p>To iterate over code point ranges, use a loop like this:\r
33  * <pre>\r
34  * UnicodeSetIterator it = new UnicodeSetIterator(set);\r
35  * while (set.nextRange()) {\r
36  *   if (set.codepoint != UnicodeSetIterator.IS_STRING) {\r
37  *     processCodepointRange(set.codepoint, set.codepointEnd);\r
38  *   } else {\r
39  *     processString(set.string);\r
40  *   }\r
41  * }\r
42  * </pre>\r
43  * @author M. Davis\r
44  */\r
45 public class UnicodeMapIterator<T> {\r
46 \r
47     /**\r
48      * Value of <tt>codepoint</tt> if the iterator points to a string.\r
49      * If <tt>codepoint == IS_STRING</tt>, then examine\r
50      * <tt>string</tt> for the current iteration result.\r
51      */\r
52     public static int IS_STRING = -1;\r
53 \r
54     /**\r
55      * Current code point, or the special value <tt>IS_STRING</tt>, if\r
56      * the iterator points to a string.\r
57      */\r
58     public int codepoint;\r
59 \r
60     /**\r
61      * When iterating over ranges using <tt>nextRange()</tt>,\r
62      * <tt>codepointEnd</tt> contains the inclusive end of the\r
63      * iteration range, if <tt>codepoint != IS_STRING</tt>.  If\r
64      * iterating over code points using <tt>next()</tt>, or if\r
65      * <tt>codepoint == IS_STRING</tt>, then the value of\r
66      * <tt>codepointEnd</tt> is undefined.\r
67      */\r
68     public int codepointEnd;\r
69 \r
70     /**\r
71      * If <tt>codepoint == IS_STRING</tt>, then <tt>string</tt> points\r
72      * to the current string.  If <tt>codepoint != IS_STRING</tt>, the\r
73      * value of <tt>string</tt> is undefined.\r
74      */\r
75     public String string;\r
76 \r
77     /**\r
78      * The value associated with this element or range.\r
79      */\r
80     public T value;\r
81 \r
82     /**\r
83      * Create an iterator over the given set.\r
84      * @param set set to iterate over\r
85      */\r
86     public UnicodeMapIterator(UnicodeMap set) {\r
87         reset(set);\r
88     }\r
89 \r
90     /**\r
91      * Create an iterator over nothing.  <tt>next()</tt> and\r
92      * <tt>nextRange()</tt> return false. This is a convenience\r
93      * constructor allowing the target to be set later.\r
94      */\r
95     public UnicodeMapIterator() {\r
96         reset(new UnicodeMap());\r
97     }\r
98 \r
99     /**\r
100      * Returns the next element in the set, either a single code point\r
101      * or a string.  If there are no more elements in the set, return\r
102      * false.  If <tt>codepoint == IS_STRING</tt>, the value is a\r
103      * string in the <tt>string</tt> field.  Otherwise the value is a\r
104      * single code point in the <tt>codepoint</tt> field.\r
105      * \r
106      * <p>The order of iteration is all code points in sorted order,\r
107      * followed by all strings sorted order.  <tt>codepointEnd</tt> is\r
108      * undefined after calling this method.  <tt>string</tt> is\r
109      * undefined unless <tt>codepoint == IS_STRING</tt>.  Do not mix\r
110      * calls to <tt>next()</tt> and <tt>nextRange()</tt> without\r
111      * calling <tt>reset()</tt> between them.  The results of doing so\r
112      * are undefined.\r
113      *\r
114      * @return true if there was another element in the set and this\r
115      * object contains the element.\r
116      */\r
117     public boolean next() {\r
118         if (nextElement <= endElement) {\r
119             codepoint = codepointEnd = nextElement++;\r
120             return true;\r
121         }\r
122         while (range < endRange) {\r
123             if (loadRange(++range) == null) {\r
124                 continue;\r
125             }\r
126             codepoint = codepointEnd = nextElement++;\r
127             return true;\r
128         }\r
129 \r
130         // stringIterator == null iff there are no string elements remaining\r
131 \r
132         if (stringIterator == null) return false;\r
133         codepoint = IS_STRING; // signal that value is actually a string\r
134         string = (String)stringIterator.next();\r
135         if (!stringIterator.hasNext()) stringIterator = null;\r
136         return true;\r
137     }\r
138 \r
139     /**\r
140      * Returns the next element in the set, either a code point range\r
141      * or a string.  If there are no more elements in the set, return\r
142      * false.  If <tt>codepoint == IS_STRING</tt>, the value is a\r
143      * string in the <tt>string</tt> field.  Otherwise the value is a\r
144      * range of one or more code points from <tt>codepoint</tt> to\r
145      * <tt>codepointeEnd</tt> inclusive.\r
146      * \r
147      * <p>The order of iteration is all code points ranges in sorted\r
148      * order, followed by all strings sorted order.  Ranges are\r
149      * disjoint and non-contiguous.  <tt>string</tt> is undefined\r
150      * unless <tt>codepoint == IS_STRING</tt>.  Do not mix calls to\r
151      * <tt>next()</tt> and <tt>nextRange()</tt> without calling\r
152      * <tt>reset()</tt> between them.  The results of doing so are\r
153      * undefined.\r
154      *\r
155      * @return true if there was another element in the set and this\r
156      * object contains the element.\r
157      */\r
158     public boolean nextRange() {\r
159         if (nextElement <= endElement) {\r
160             codepointEnd = endElement;\r
161             codepoint = nextElement;\r
162             nextElement = endElement+1;\r
163             return true;\r
164         }\r
165         while (range < endRange) {\r
166             if (loadRange(++range) == null) {\r
167                 continue;\r
168             }\r
169             codepointEnd = endElement;\r
170             codepoint = nextElement;\r
171             nextElement = endElement+1;\r
172             return true;\r
173         }\r
174 \r
175         // stringIterator == null iff there are no string elements remaining\r
176 \r
177         if (stringIterator == null) return false;\r
178         codepoint = IS_STRING; // signal that value is actually a string\r
179         string = (String)stringIterator.next();\r
180         if (!stringIterator.hasNext()) stringIterator = null;\r
181         return true;\r
182     }\r
183 \r
184     /**\r
185      * Sets this iterator to visit the elements of the given set and\r
186      * resets it to the start of that set.  The iterator is valid only\r
187      * so long as <tt>set</tt> is valid.\r
188      * @param set the set to iterate over.\r
189      */\r
190     public void reset(UnicodeMap set) {\r
191         this.map = set;\r
192         reset();\r
193     }\r
194 \r
195     /**\r
196      * Resets this iterator to the start of the set.\r
197      * @return \r
198      */\r
199     public UnicodeMapIterator<T> reset() {\r
200         endRange = map.getRangeCount() - 1;\r
201         // both next*() methods will test: if (nextElement <= endElement)\r
202         // we set them to fail this test, which will cause them to load the first range\r
203         nextElement = 0; \r
204         endElement = -1;\r
205         range = -1;\r
206 \r
207         stringIterator = null;\r
208         Set<String> strings = map.getNonRangeStrings();\r
209         if (strings != null) {\r
210             stringIterator = strings.iterator();\r
211             if (!stringIterator.hasNext()) stringIterator = null;\r
212         }\r
213         value = null;\r
214         return this;\r
215     }\r
216 \r
217     /**\r
218      * Gets the current string from the iterator. Only use after calling next(), not nextRange().\r
219      */\r
220     public String getString() {\r
221         if (codepoint != IS_STRING) {\r
222             return UTF16.valueOf(codepoint);\r
223         }\r
224         return string;\r
225     }\r
226 \r
227     // ======================= PRIVATES ===========================\r
228 \r
229     private UnicodeMap<T> map;\r
230     private int endRange = 0;\r
231     private int range = 0;\r
232     private Iterator<String> stringIterator = null;\r
233     protected int endElement;\r
234     protected int nextElement;\r
235 \r
236     /*\r
237      * Invariant: stringIterator is null when there are no (more) strings remaining\r
238      */\r
239 \r
240     protected T loadRange(int range) {\r
241         nextElement = map.getRangeStart(range);\r
242         endElement = map.getRangeEnd(range);\r
243         value = map.getRangeValue(range);\r
244         return value;\r
245     }\r
246 }\r