/* ******************************************************************************* * Copyright (C) 1996-2010, International Business Machines Corporation and * * others. All Rights Reserved. * ******************************************************************************* */ package com.ibm.icu.dev.test.util; import java.util.Iterator; import java.util.Set; import com.ibm.icu.text.UTF16; /** * UnicodeSetIterator iterates over the contents of a UnicodeSet. It * iterates over either code points or code point ranges. After all * code points or ranges have been returned, it returns the * multicharacter strings of the UnicodSet, if any. * *
To iterate over code points, use a loop like this: *
* UnicodeSetIterator it = new UnicodeSetIterator(set); * while (set.next()) { * if (set.codepoint != UnicodeSetIterator.IS_STRING) { * processCodepoint(set.codepoint); * } else { * processString(set.string); * } * } ** *
To iterate over code point ranges, use a loop like this: *
* UnicodeSetIterator it = new UnicodeSetIterator(set); * while (set.nextRange()) { * if (set.codepoint != UnicodeSetIterator.IS_STRING) { * processCodepointRange(set.codepoint, set.codepointEnd); * } else { * processString(set.string); * } * } ** @author M. Davis */ public class UnicodeMapIterator
The order of iteration is all code points in sorted order, * followed by all strings sorted order. codepointEnd is * undefined after calling this method. string is * undefined unless codepoint == IS_STRING. Do not mix * calls to next() and nextRange() without * calling reset() between them. The results of doing so * are undefined. * * @return true if there was another element in the set and this * object contains the element. */ public boolean next() { if (nextElement <= endElement) { codepoint = codepointEnd = nextElement++; return true; } while (range < endRange) { if (loadRange(++range) == null) { continue; } codepoint = codepointEnd = nextElement++; return true; } // stringIterator == null iff there are no string elements remaining if (stringIterator == null) return false; codepoint = IS_STRING; // signal that value is actually a string string = (String)stringIterator.next(); if (!stringIterator.hasNext()) stringIterator = null; return true; } /** * Returns the next element in the set, either a code point range * or a string. If there are no more elements in the set, return * false. If codepoint == IS_STRING, the value is a * string in the string field. Otherwise the value is a * range of one or more code points from codepoint to * codepointeEnd inclusive. * *
The order of iteration is all code points ranges in sorted
* order, followed by all strings sorted order. Ranges are
* disjoint and non-contiguous. string is undefined
* unless codepoint == IS_STRING. Do not mix calls to
* next() and nextRange() without calling
* reset() between them. The results of doing so are
* undefined.
*
* @return true if there was another element in the set and this
* object contains the element.
*/
public boolean nextRange() {
if (nextElement <= endElement) {
codepointEnd = endElement;
codepoint = nextElement;
nextElement = endElement+1;
return true;
}
while (range < endRange) {
if (loadRange(++range) == null) {
continue;
}
codepointEnd = endElement;
codepoint = nextElement;
nextElement = endElement+1;
return true;
}
// stringIterator == null iff there are no string elements remaining
if (stringIterator == null) return false;
codepoint = IS_STRING; // signal that value is actually a string
string = (String)stringIterator.next();
if (!stringIterator.hasNext()) stringIterator = null;
return true;
}
/**
* Sets this iterator to visit the elements of the given set and
* resets it to the start of that set. The iterator is valid only
* so long as set is valid.
* @param set the set to iterate over.
*/
public void reset(UnicodeMap set) {
this.map = set;
reset();
}
/**
* Resets this iterator to the start of the set.
* @return
*/
public UnicodeMapIterator