CollationElementIterator is an iterator created by
a RuleBasedCollator to walk through a string. The return result of
each iteration is a 32-bit collation element (CE) that defines the
ordering priority of the next character or sequence of characters
in the source string.
For illustration, consider the following in Slovak and in traditional Spanish collation:
"ca" -> the first collation element is CE('c') and the second
collation element is CE('a').
"cha" -> the first collation element is CE('ch') and the second
collation element is CE('a').
And in German phonebook collation,
Since the character 'æ' is a composed character of 'a' and 'e', the
iterator returns two collation elements for the single character 'æ'
"æb" -> the first collation element is collation_element('a'), the
second collation element is collation_element('e'), and the
third collation element is collation_element('b').
For collation ordering comparison, the collation element results
can not be compared simply by using basic arithmetic operators,
e.g. <, == or >, further processing has to be done. Details
can be found in the ICU
User Guide. An example of using the CollationElementIterator
for collation ordering comparison is the class
com..
To construct a CollationElementIterator object, users call the method getCollationElementIterator() on a RuleBasedCollator that defines the desired sorting order.
Example:
String testString = "This is a test";
RuleBasedCollator rbc = new RuleBasedCollator("&a<b");
CollationElementIterator iterator = rbc.getCollationElementIterator(testString);
int primaryOrder = iterator.IGNORABLE;
while (primaryOrder != iterator.NULLORDER) {
int order = iterator.next();
if (order != iterator.IGNORABLE &&
order != iterator.NULLORDER) {
// order is valid, not ignorable and we have not passed the end
// of the iteration, we do something
primaryOrder = CollationElementIterator.primaryOrder(order);
System.out.println("Next primary order 0x" +
Integer.toHexString(primaryOrder));
}
}
The method next() returns the collation order of the next character based on the comparison level of the collator. The method previous() returns the collation order of the previous character based on the comparison level of the collator. The Collation Element Iterator moves only in one direction between calls to reset(), setOffset(), or setText(). That is, next() and previous() can not be inter-used. Whenever previous() is to be called after next() or vice versa, reset(), setOffset() or setText() has to be called first to reset the status, shifting current position to either the end or the start of the string (reset() or setText()), or the specified position (setOffset()). Hence at the next call of next() or previous(), the first or last collation order, or collation order at the specified position will be returned. If a change of direction is done without one of these calls, the result is undefined.
This class is not subclassable.
Collator, RuleBasedCollator, StringSearch
| Modifier and Type | Class and Description |
|---|---|
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| private byte | dir_
<0: backwards; 0: just after reset() (previous() begins from end); 1: just after setOffset(); >1: forward |
| public static final int | IGNORABLE
This constant is returned by the iterator in the methods next() and previous() when a collation element result is to be ignored. |
| private CollationIterator | |
| public static final int | NULLORDER
This constant is returned by the iterator in the methods next() and previous() when the end or the beginning of the source string has been reached, and there are no more valid collation elements to return. |
| private UVector32 | offsets_
Stores offsets from expansions and from unsafe-backwards iteration, so that getOffset() returns intermediate offsets for the CEs that are consistent with forward iteration. |
| private int | |
| private RuleBasedCollator | |
| private String |
| Access | Constructor and Description |
|---|---|
| private | |
| pack-priv | CollationElementIterator(String
the source string. source, RuleBasedCollator the RuleBasedCollator collator)CollationElementIterator constructor. |
| pack-priv | CollationElementIterator(CharacterIterator
the source string iterator. source, RuleBasedCollator the RuleBasedCollator collator)CollationElementIterator constructor. |
| pack-priv | CollationElementIterator(UCharacterIterator
the source string iterator. source, RuleBasedCollator the RuleBasedCollator collator)CollationElementIterator constructor. |
| Modifier and Type | Method and Description |
|---|---|
| private static final boolean | |
| pack-priv static final Map | |
| public boolean | equals(Object
object to test if it is equals to this
CollationElementIterator that)Overrides java. |
| private static final int | |
| public int | Returns: the maximum length of any expansion sequence ending with the specified collation element.a collation element returned by previous() or next(). ce)Returns the maximum length of any expansion sequence that ends with the specified collation element. |
| pack-priv static int | |
| public int | Returns: The character offset in the source string corresponding to the collation element that will be returned by the next call to next() or previous().Returns the character offset in the source string corresponding to the next collation element. |
| public RuleBasedCollator | |
| private static final int | |
| public int | |
| public int | Returns: the next collation element or NULLORDER if the end of the iteration has been reached.Get the next collation element in the source string. |
| private byte | |
| public int | Returns: the previous collation element, or NULLORDER when the start of the iteration has been reached.Get the previous collation element in the source string. |
| public static final int | Returns: the element's 16 bits primary order.the collation element ce)Return the primary order of the specified collation element, i.e. the first 16 bits. |
| public void | |
| public static final int | Returns: the element's 8 bits secondary orderthe collation element ce)Return the secondary order of the specified collation element, i.e. the 16th to 23th bits, inclusive. |
| public void | setOffset(int
the character offset into the original source string to
set. Note that this is not an offset into the corresponding
sequence of collation elements. newOffset)Sets the iterator to point to the collation element corresponding to the character at the specified offset. |
| public void | |
| public void | setText(UCharacterIterator
the new source string iterator for iteration. source)Set a new source string iterator for iteration, and reset the offset to the beginning of the text. |
| public void | setText(CharacterIterator
the new source string iterator for iteration. source)Set a new source string iterator for iteration, and reset the offset to the beginning of the text. |
| public static final int | Returns: the element's 8 bits tertiary orderthe collation element ce)Return the tertiary order of the specified collation element, i.e. the last 8 bits. |
| dir_ | back to summary |
|---|---|
| private byte dir_ <0: backwards; 0: just after reset() (previous() begins from end); 1: just after setOffset(); >1: forward | |
| IGNORABLE | back to summary |
|---|---|
| public static final int IGNORABLE This constant is returned by the iterator in the methods next() and previous() when a collation element result is to be ignored. See class documentation for an example of use. | |
| iter_ | back to summary |
|---|---|
| private CollationIterator iter_ | |
| NULLORDER | back to summary |
|---|---|
| public static final int NULLORDER This constant is returned by the iterator in the methods next() and previous() when the end or the beginning of the source string has been reached, and there are no more valid collation elements to return. See class documentation for an example of use. | |
| offsets_ | back to summary |
|---|---|
| private UVector32 offsets_ Stores offsets from expansions and from unsafe-backwards iteration, so that getOffset() returns intermediate offsets for the CEs that are consistent with forward iteration. | |
| otherHalf_ | back to summary |
|---|---|
| private int otherHalf_ | |
| rbc_ | back to summary |
|---|---|
| private RuleBasedCollator rbc_ | |
| string_ | back to summary |
|---|---|
| private String string_ | |
| CollationElementIterator | back to summary |
|---|---|
| private CollationElementIterator(RuleBasedCollator collator) | |
| CollationElementIterator | back to summary |
|---|---|
| pack-priv CollationElementIterator(String source, RuleBasedCollator collator) CollationElementIterator constructor. This takes a source string and a RuleBasedCollator. The iterator will walk through the source string based on the rules defined by the collator. If the source string is empty, NULLORDER will be returned on the first call to next().
| |
| CollationElementIterator | back to summary |
|---|---|
| pack-priv CollationElementIterator(CharacterIterator source, RuleBasedCollator collator) CollationElementIterator constructor. This takes a source character iterator and a RuleBasedCollator. The iterator will walk through the source string based on the rules defined by the collator. If the source string is empty, NULLORDER will be returned on the first call to next().
| |
| CollationElementIterator | back to summary |
|---|---|
| pack-priv CollationElementIterator(UCharacterIterator source, RuleBasedCollator collator) CollationElementIterator constructor. This takes a source character iterator and a RuleBasedCollator. The iterator will walk through the source string based on the rules defined by the collator. If the source string is empty, NULLORDER will be returned on the first call to next().
| |
| ceNeedsTwoParts | back to summary |
|---|---|
| private static final boolean ceNeedsTwoParts(long ce) | |
| computeMaxExpansions | back to summary |
|---|---|
| pack-priv static final Map | |
| equals | back to summary |
|---|---|
| public boolean equals(Object that) Overrides java. Tests that argument object is equals to this CollationElementIterator. Iterators are equal if the objects uses the same RuleBasedCollator, the same source text and have the same current position in iteration.
| |
| getFirstHalf | back to summary |
|---|---|
| private static final int getFirstHalf(long p, int lower32) | |
| getMaxExpansion | back to summary |
|---|---|
| public int getMaxExpansion(int ce) Returns the maximum length of any expansion sequence that ends with the specified collation element. If there is no expansion with this collation element as the last element, returns 1.
| |
| getMaxExpansion | back to summary |
|---|---|
| pack-priv static int getMaxExpansion(Map<Integer, Integer> maxExpansions, int order) | |
| getOffset | back to summary |
|---|---|
| public int getOffset() Returns the character offset in the source string corresponding to the next collation element. I.e., getOffset() returns the position in the source string corresponding to the collation element that will be returned by the next call to next() or previous(). This value could be any of:
| |
| getRuleBasedCollator | back to summary |
|---|---|
| public RuleBasedCollator getRuleBasedCollator()
Deprecated This API is ICU internal only.
| |
| getSecondHalf | back to summary |
|---|---|
| private static final int getSecondHalf(long p, int lower32) | |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Mock implementation of hashCode(). This implementation always returns a constant value. When Java assertion is enabled, this method triggers an assertion failure.
| |
| next | back to summary |
|---|---|
| public int next() Get the next collation element in the source string. This iterator iterates over a sequence of collation elements that were built from the string. Because there isn't necessarily a one-to-one mapping from characters to collation elements, this doesn't mean the same thing as "return the collation element [or ordering priority] of the next character in the string". This function returns the collation element that the iterator is currently pointing to, and then updates the internal pointer to point to the next element.
| |
| normalizeDir | back to summary |
|---|---|
| private byte normalizeDir() Normalizes dir_=1 (just after setOffset()) to dir_=0 (just after reset()). | |
| previous | back to summary |
|---|---|
| public int previous() Get the previous collation element in the source string. This iterator iterates over a sequence of collation elements that were built from the string. Because there isn't necessarily a one-to-one mapping from characters to collation elements, this doesn't mean the same thing as "return the collation element [or ordering priority] of the previous character in the string". This function updates the iterator's internal pointer to point to the collation element preceding the one it's currently pointing to and then returns that element, while next() returns the current element and then updates the pointer.
| |
| primaryOrder | back to summary |
|---|---|
| public static final int primaryOrder(int ce) Return the primary order of the specified collation element, i.e. the first 16 bits. This value is unsigned.
| |
| reset | back to summary |
|---|---|
| public void reset() Resets the cursor to the beginning of the string. The next call to next() or previous() will return the first and last collation element in the string, respectively. If the RuleBasedCollator used by this iterator has had its attributes changed, calling reset() will reinitialize the iterator to use the new attributes.
| |
| secondaryOrder | back to summary |
|---|---|
| public static final int secondaryOrder(int ce) Return the secondary order of the specified collation element, i.e. the 16th to 23th bits, inclusive. This value is unsigned.
| |
| setOffset | back to summary |
|---|---|
| public void setOffset(int newOffset) Sets the iterator to point to the collation element corresponding to the character at the specified offset. The value returned by the next call to next() will be the collation element corresponding to the characters at offset. If offset is in the middle of a contracting character sequence, the iterator is adjusted to the start of the contracting sequence. This means that getOffset() is not guaranteed to return the same value set by this method. If the decomposition mode is on, and offset is in the middle of a decomposible range of source text, the iterator may not return a correct result for the next forwards or backwards iteration. The user must ensure that the offset is not in the middle of a decomposible range.
| |
| setText | back to summary |
|---|---|
| public void setText(String source) Set a new source string for iteration, and reset the offset to the beginning of the text. | |
| setText | back to summary |
|---|---|
| public void setText(UCharacterIterator source) Set a new source string iterator for iteration, and reset the offset to the beginning of the text. The source iterator's integrity will be preserved since a new copy will be created for use.
| |
| setText | back to summary |
|---|---|
| public void setText(CharacterIterator source) Set a new source string iterator for iteration, and reset the offset to the beginning of the text.
| |
| tertiaryOrder | back to summary |
|---|---|
| public static final int tertiaryOrder(int ce) Return the tertiary order of the specified collation element, i.e. the last 8 bits. This value is unsigned.
| |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public void | handleExpansion(long[] ces, int start, int length)
Implements com. |
| maxExpansions | back to summary |
|---|---|
| private Map<Integer, Integer> maxExpansions | |
| MaxExpSink | back to summary |
|---|---|
| pack-priv MaxExpSink(Map<Integer, Integer> h) | |
| handleCE | back to summary |
|---|---|
| public void handleCE(long ce) Implements com.
| |
| handleExpansion | back to summary |
|---|---|
| public void handleExpansion(long[] ces, int start, int length) Implements com.
| |