APIdia Launch Partner
Top Description Fields Constructors Methods
java.text

public final Class CollationElementIterator

extends Object
Class Inheritance
Imports
java.lang.Character, java.util.Vector, sun.text.CollatorUtilities, jdk.internal.icu.text.NormalizerBase

The CollationElementIterator class is used as an iterator to walk through each character of an international string. Use the iterator to return the ordering priority of the positioned character. The ordering priority of a character, which we refer to as a key, defines how a character is collated in the given collation object.

For example, consider the following in Spanish:

"ca" → the first key is key('c') and second key is key('a').
"cha" → the first key is key('ch') and second key is key('a').
And in German,
"äb" → the first key is key('a'), the second key is key('e'), and
the third key is key('b').
The key of a character is an integer composed of primary order(short), secondary order(byte), and tertiary order(byte). Java strictly defines the size and signedness of its primitive data types. Therefore, the static functions primaryOrder, secondaryOrder, and tertiaryOrder return int, short, and short respectively to ensure the correctness of the key value.

Example of the iterator usage,

String testString = "This is a test"; Collator col = Collator.getInstance(); if (col instanceof RuleBasedCollator ruleBasedCollator) { CollationElementIterator collationElementIterator = ruleBasedCollator.getCollationElementIterator(testString); int primaryOrder = CollationElementIterator.primaryOrder(collationElementIterator.next()); ⋮ }
String testString = "This is a test";
Collator col = Collator.getInstance();
if (col instanceof RuleBasedCollator ruleBasedCollator) {
    CollationElementIterator collationElementIterator = ruleBasedCollator.getCollationElementIterator(testString);
    int primaryOrder = CollationElementIterator.primaryOrder(collationElementIterator.next());
        ⋮
}

CollationElementIterator.next returns the collation order of the next character. A collation order consists of primary order, secondary order and tertiary order. The data type of the collation order is int. The first 16 bits of a collation order is its primary order; the next 8 bits is the secondary order and the last 8 bits is the tertiary order.

Note

CollationElementIterator is a part of RuleBasedCollator implementation. It is only usable with RuleBasedCollator instances.

Author
Helena Shih, Laura Werner, Richard Gillam
Since
1.1
See Also
Collator, RuleBasedCollator

Field Summary

Modifier and TypeField and Description
private int[]
private int
private StringBuilder
public static final int
NULLORDER

Null order which indicates the end of string is reached by the cursor.

private RBCollationTables
private RuleBasedCollator
private int
private NormalizerBase
pack-priv static final int

Constructor Summary

AccessConstructor and Description
pack-priv
CollationElementIterator(String
the source string.
sourceText
,
RuleBasedCollator
the collation object.
owner
)

CollationElementIterator constructor.

pack-priv
CollationElementIterator(CharacterIterator
the source string.
sourceText
,
RuleBasedCollator
the collation object.
owner
)

CollationElementIterator constructor.

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

the maximum length of any expansion sequences ending with the specified order.
getMaxExpansion
(int
a collation order returned by previous or next.
order
)

Return the maximum length of any expansion sequences that end with the specified comparison order.

public int

Returns:

The character offset in the original text corresponding to the collation element that will be returned by the next call to next().
getOffset
()

Returns the character offset in the original text corresponding to the next collation element.

pack-priv static final boolean

Returns:

true if a character is ignorable, false otherwise.
isIgnorable
(int order)

Check if a comparison order is ignorable.

private static final boolean
isLaoBaseConsonant(int ch)

Determine if a character is a Lao base consonant

private static final boolean
isLaoPreVowel(int ch)

Determine if a character is a Lao vowel (which sorts after its base consonant).

private static final boolean
isThaiBaseConsonant(int ch)

Determine if a character is a Thai base consonant

private static final boolean
isThaiPreVowel(int ch)

Determine if a character is a Thai vowel (which sorts after its base consonant).

private int[]
makeReorderedBuffer(int colFirst, int lastValue, int[] lastExpansion, boolean forward)

This method produces a buffer which contains the collation elements for the two characters, with colFirst's values preceding another character's.

public int

Returns:

the next collation element
next
()

Get the next collation element in the string.

private int

Returns:

the next contracting character's ordering. Returns NULLORDER if the end of string is reached.
nextContractChar
(int
the starting character of a contracting character token
ch
)

Get the ordering priority of the next contracting character in the string.

private int

Returns:

the next contracting character's ordering. Returns NULLORDER if the end of string is reached.
prevContractChar
(int
the starting character of a contracting character token
ch
)

Get the ordering priority of the previous contracting character in the string.

public int

Returns:

the previous collation element
previous
()

Get the previous collation element in the string.

public static final int

Returns:

the element's primary component
primaryOrder
(int
the collation element
order
)

Return the primary component of a collation element.

public void
reset()

Resets the cursor to the beginning of the string.

public static final short

Returns:

the element's secondary component
secondaryOrder
(int
the collation element
order
)

Return the secondary component of a collation element.

public void
setOffset(int
The new character offset into the original text.
newOffset
)

Sets the iterator to point to the collation element corresponding to the specified character (the parameter is a CHARACTER offset in the original string, not an offset into its corresponding sequence of collation elements).

public void
setText(String
the new source text
source
)

Set a new string over which to iterate.

public void
setText(CharacterIterator
the new source text.
source
)

Set a new string over which to iterate.

pack-priv final int
strengthOrder(int
The order value
order
)

Get the comparison order in the desired strength.

public static final short

Returns:

the element's tertiary component
tertiaryOrder
(int
the collation element
order
)

Return the tertiary component of a collation element.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

bufferback to summary
private int[] buffer
expIndexback to summary
private int expIndex
keyback to summary
private StringBuilder key
NULLORDERback to summary
public static final int NULLORDER

Null order which indicates the end of string is reached by the cursor.

orderingback to summary
private RBCollationTables ordering
ownerback to summary
private RuleBasedCollator owner
swapOrderback to summary
private int swapOrder
textback to summary
private NormalizerBase text
UNMAPPEDCHARVALUEback to summary
pack-priv static final int UNMAPPEDCHARVALUE

Constructor Detail

CollationElementIteratorback to summary
pack-priv CollationElementIterator(String sourceText, RuleBasedCollator owner)

CollationElementIterator constructor. This takes the source string and the collation object. The cursor will walk thru the source string based on the predefined collation rules. If the source string is empty, NULLORDER will be returned on the calls to next().

Parameters
sourceText:String

the source string.

owner:RuleBasedCollator

the collation object.

CollationElementIteratorback to summary
pack-priv CollationElementIterator(CharacterIterator sourceText, RuleBasedCollator owner)

CollationElementIterator constructor. This takes the source string and the collation object. The cursor will walk thru the source string based on the predefined collation rules. If the source string is empty, NULLORDER will be returned on the calls to next().

Parameters
sourceText:CharacterIterator

the source string.

owner:RuleBasedCollator

the collation object.

Method Detail

getMaxExpansionback to summary
public int getMaxExpansion(int order)

Return the maximum length of any expansion sequences that end with the specified comparison order.

Parameters
order:int

a collation order returned by previous or next.

Returns:int

the maximum length of any expansion sequences ending with the specified order.

Since
1.2
getOffsetback to summary
public int getOffset()

Returns the character offset in the original text corresponding to the next collation element. (That is, getOffset() returns the position in the text corresponding to the collation element that will be returned by the next call to next().) This value will always be the index of the FIRST character corresponding to the collation element (a contracting character sequence is when two or more characters all correspond to the same collation element). This means if you do setOffset(x) followed immediately by getOffset(), getOffset() won't necessarily return x.

Returns:int

The character offset in the original text corresponding to the collation element that will be returned by the next call to next().

Since
1.2
isIgnorableback to summary
pack-priv static final boolean isIgnorable(int order)

Check if a comparison order is ignorable.

Returns:boolean

true if a character is ignorable, false otherwise.

isLaoBaseConsonantback to summary
private static final boolean isLaoBaseConsonant(int ch)

Determine if a character is a Lao base consonant

isLaoPreVowelback to summary
private static final boolean isLaoPreVowel(int ch)

Determine if a character is a Lao vowel (which sorts after its base consonant).

isThaiBaseConsonantback to summary
private static final boolean isThaiBaseConsonant(int ch)

Determine if a character is a Thai base consonant

isThaiPreVowelback to summary
private static final boolean isThaiPreVowel(int ch)

Determine if a character is a Thai vowel (which sorts after its base consonant).

makeReorderedBufferback to summary
private int[] makeReorderedBuffer(int colFirst, int lastValue, int[] lastExpansion, boolean forward)

This method produces a buffer which contains the collation elements for the two characters, with colFirst's values preceding another character's. Presumably, the other character precedes colFirst in logical order (otherwise you wouldn't need this method would you?). The assumption is that the other char's value(s) have already been computed. If this char has a single element it is passed to this method as lastValue, and lastExpansion is null. If it has an expansion it is passed in lastExpansion, and colLastValue is ignored.

nextback to summary
public int next()

Get the next collation element in the 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. previous() updates the pointer first and then returns the element. This means that when you change direction while iterating (i.e., call next() and then call previous(), or call previous() and then call next()), you'll get back the same element twice.

Returns:int

the next collation element

nextContractCharback to summary
private int nextContractChar(int ch)

Get the ordering priority of the next contracting character in the string.

Parameters
ch:int

the starting character of a contracting character token

Returns:int

the next contracting character's ordering. Returns NULLORDER if the end of string is reached.

prevContractCharback to summary
private int prevContractChar(int ch)

Get the ordering priority of the previous contracting character in the string.

Parameters
ch:int

the starting character of a contracting character token

Returns:int

the next contracting character's ordering. Returns NULLORDER if the end of string is reached.

previousback to summary
public int previous()

Get the previous collation element in the 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. This means that when you change direction while iterating (i.e., call next() and then call previous(), or call previous() and then call next()), you'll get back the same element twice.

Returns:int

the previous collation element

Since
1.2
primaryOrderback to summary
public static final int primaryOrder(int order)

Return the primary component of a collation element.

Parameters
order:int

the collation element

Returns:int

the element's primary component

resetback to summary
public void reset()

Resets the cursor to the beginning of the string. The next call to next() will return the first collation element in the string.

secondaryOrderback to summary
public static final short secondaryOrder(int order)

Return the secondary component of a collation element.

Parameters
order:int

the collation element

Returns:short

the element's secondary component

setOffsetback to summary
public void setOffset(int newOffset)

Sets the iterator to point to the collation element corresponding to the specified character (the parameter is a CHARACTER offset in the original string, not an offset into its corresponding sequence of collation elements). The value returned by the next call to next() will be the collation element corresponding to the specified position in the text. If that position is in the middle of a contracting character sequence, the result of the next call to next() is the collation element for that sequence. This means that getOffset() is not guaranteed to return the same value as was passed to a preceding call to setOffset().

Parameters
newOffset:int

The new character offset into the original text.

Annotations
@SuppressWarnings:deprecation
Since
1.2
setTextback to summary
public void setText(String source)

Set a new string over which to iterate.

Parameters
source:String

the new source text

Since
1.2
setTextback to summary
public void setText(CharacterIterator source)

Set a new string over which to iterate.

Parameters
source:CharacterIterator

the new source text.

Since
1.2
strengthOrderback to summary
pack-priv final int strengthOrder(int order)

Get the comparison order in the desired strength. Ignore the other differences.

Parameters
order:int

The order value

tertiaryOrderback to summary
public static final short tertiaryOrder(int order)

Return the tertiary component of a collation element.

Parameters
order:int

the collation element

Returns:short

the element's tertiary component