2 *******************************************************************************
3 * Copyright (C) 1996-2012, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.dev.util;
9 import java.util.Iterator;
12 import com.ibm.icu.text.UTF16;
15 * UnicodeSetIterator iterates over the contents of a UnicodeSet. It
16 * iterates over either code points or code point ranges. After all
17 * code points or ranges have been returned, it returns the
18 * multicharacter strings of the UnicodSet, if any.
20 * <p>To iterate over code points, use a loop like this:
22 * UnicodeSetIterator it = new UnicodeSetIterator(set);
23 * while (set.next()) {
24 * if (set.codepoint != UnicodeSetIterator.IS_STRING) {
25 * processCodepoint(set.codepoint);
27 * processString(set.string);
32 * <p>To iterate over code point ranges, use a loop like this:
34 * UnicodeSetIterator it = new UnicodeSetIterator(set);
35 * while (set.nextRange()) {
36 * if (set.codepoint != UnicodeSetIterator.IS_STRING) {
37 * processCodepointRange(set.codepoint, set.codepointEnd);
39 * processString(set.string);
45 public class UnicodeMapIterator<T> {
48 * Value of <tt>codepoint</tt> if the iterator points to a string.
49 * If <tt>codepoint == IS_STRING</tt>, then examine
50 * <tt>string</tt> for the current iteration result.
52 public static int IS_STRING = -1;
55 * Current code point, or the special value <tt>IS_STRING</tt>, if
56 * the iterator points to a string.
61 * When iterating over ranges using <tt>nextRange()</tt>,
62 * <tt>codepointEnd</tt> contains the inclusive end of the
63 * iteration range, if <tt>codepoint != IS_STRING</tt>. If
64 * iterating over code points using <tt>next()</tt>, or if
65 * <tt>codepoint == IS_STRING</tt>, then the value of
66 * <tt>codepointEnd</tt> is undefined.
68 public int codepointEnd;
71 * If <tt>codepoint == IS_STRING</tt>, then <tt>string</tt> points
72 * to the current string. If <tt>codepoint != IS_STRING</tt>, the
73 * value of <tt>string</tt> is undefined.
78 * The value associated with this element or range.
83 * Create an iterator over the given set.
84 * @param set set to iterate over
86 public UnicodeMapIterator(UnicodeMap set) {
91 * Create an iterator over nothing. <tt>next()</tt> and
92 * <tt>nextRange()</tt> return false. This is a convenience
93 * constructor allowing the target to be set later.
95 public UnicodeMapIterator() {
96 reset(new UnicodeMap());
100 * Returns the next element in the set, either a single code point
101 * or a string. If there are no more elements in the set, return
102 * false. If <tt>codepoint == IS_STRING</tt>, the value is a
103 * string in the <tt>string</tt> field. Otherwise the value is a
104 * single code point in the <tt>codepoint</tt> field.
106 * <p>The order of iteration is all code points in sorted order,
107 * followed by all strings sorted order. <tt>codepointEnd</tt> is
108 * undefined after calling this method. <tt>string</tt> is
109 * undefined unless <tt>codepoint == IS_STRING</tt>. Do not mix
110 * calls to <tt>next()</tt> and <tt>nextRange()</tt> without
111 * calling <tt>reset()</tt> between them. The results of doing so
114 * @return true if there was another element in the set and this
115 * object contains the element.
117 public boolean next() {
118 if (nextElement <= endElement) {
119 codepoint = codepointEnd = nextElement++;
122 while (range < endRange) {
123 if (loadRange(++range) == null) {
126 codepoint = codepointEnd = nextElement++;
130 // stringIterator == null iff there are no string elements remaining
132 if (stringIterator == null) return false;
133 codepoint = IS_STRING; // signal that value is actually a string
134 string = (String)stringIterator.next();
135 if (!stringIterator.hasNext()) stringIterator = null;
140 * Returns the next element in the set, either a code point range
141 * or a string. If there are no more elements in the set, return
142 * false. If <tt>codepoint == IS_STRING</tt>, the value is a
143 * string in the <tt>string</tt> field. Otherwise the value is a
144 * range of one or more code points from <tt>codepoint</tt> to
145 * <tt>codepointeEnd</tt> inclusive.
147 * <p>The order of iteration is all code points ranges in sorted
148 * order, followed by all strings sorted order. Ranges are
149 * disjoint and non-contiguous. <tt>string</tt> is undefined
150 * unless <tt>codepoint == IS_STRING</tt>. Do not mix calls to
151 * <tt>next()</tt> and <tt>nextRange()</tt> without calling
152 * <tt>reset()</tt> between them. The results of doing so are
155 * @return true if there was another element in the set and this
156 * object contains the element.
158 public boolean nextRange() {
159 if (nextElement <= endElement) {
160 codepointEnd = endElement;
161 codepoint = nextElement;
162 nextElement = endElement+1;
165 while (range < endRange) {
166 if (loadRange(++range) == null) {
169 codepointEnd = endElement;
170 codepoint = nextElement;
171 nextElement = endElement+1;
175 // stringIterator == null iff there are no string elements remaining
177 if (stringIterator == null) return false;
178 codepoint = IS_STRING; // signal that value is actually a string
179 string = (String)stringIterator.next();
180 if (!stringIterator.hasNext()) stringIterator = null;
185 * Sets this iterator to visit the elements of the given set and
186 * resets it to the start of that set. The iterator is valid only
187 * so long as <tt>set</tt> is valid.
188 * @param set the set to iterate over.
190 public void reset(UnicodeMap set) {
196 * Resets this iterator to the start of the set.
199 public UnicodeMapIterator<T> reset() {
200 endRange = map.getRangeCount() - 1;
201 // both next*() methods will test: if (nextElement <= endElement)
202 // we set them to fail this test, which will cause them to load the first range
207 stringIterator = null;
208 Set<String> strings = map.getNonRangeStrings();
209 if (strings != null) {
210 stringIterator = strings.iterator();
211 if (!stringIterator.hasNext()) stringIterator = null;
218 * Gets the current string from the iterator. Only use after calling next(), not nextRange().
220 public String getString() {
221 if (codepoint != IS_STRING) {
222 return UTF16.valueOf(codepoint);
227 // ======================= PRIVATES ===========================
229 private UnicodeMap<T> map;
230 private int endRange = 0;
231 private int range = 0;
232 private Iterator<String> stringIterator = null;
233 protected int endElement;
234 protected int nextElement;
237 * Invariant: stringIterator is null when there are no (more) strings remaining
240 protected T loadRange(int range) {
241 nextElement = map.getRangeStart(range);
242 endElement = map.getRangeEnd(range);
243 value = map.getRangeValue(range);