public final class CollationElementIterator
extends Object
| java.lang.Object | |
| java.text.CollationElementIterator | |
CollationElementIterator类用作遍历国际字符串的每个字符的迭代器。 使用迭代器返回定位字符的排序优先级。 字符的排序优先级(我们称之为键)定义了字符在给定排序对象中的排列方式。
例如,考虑西班牙语的以下内容:
"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,
""ab"-> 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.
迭代器使用的示例,
String testString = "This is a test";
Collator col = Collator.getInstance();
if (col instanceof RuleBasedCollator) {
RuleBasedCollator ruleBasedCollator = (RuleBasedCollator)col;
CollationElementIterator collationElementIterator = ruleBasedCollator.getCollationElementIterator(testString);
int primaryOrder = CollationElementIterator.primaryOrder(collationElementIterator.next());
:
}
CollationElementIterator.next返回下一个字符的整理顺序。 整理顺序由主要订单,二级订单和三级订单组成。 整理顺序的数据类型是int 。 排序顺序的前16位是其主要顺序; 接下来的8位是次级,最后8位是三级。
注意: CollationElementIterator是RuleBasedCollator实现的一部分。 它只能用于RuleBasedCollator实例。
也可以看看:
常量(Constants) |
|
|---|---|
int |
NULLORDER 指示字符串结尾的空指令由游标到达。 |
公共方法(Public methods) |
|
|---|---|
int |
getMaxExpansion(int order) 返回以指定比较顺序结束的所有扩展序列的最大长度。 |
int |
getOffset() 返回原始文本中与下一个整理元素对应的字符偏移量。 |
int |
next() 获取字符串中的下一个整理元素。 |
int |
previous() 获取字符串中以前的整理元素。 |
static final int |
primaryOrder(int order) 返回排序规则元素的主要组件。 |
void |
reset() 将光标重置到字符串的开头。 |
static final short |
secondaryOrder(int order) 返回排序规则元素的辅助组件。 |
void |
setOffset(int newOffset) 设置迭代器指向与指定字符对应的排序规则元素(该参数是原始字符串中的CHARACTER偏移量,而不是其对应的排序元素序列的偏移量)。 |
void |
setText(String source) 设置一个迭代的新字符串。 |
void |
setText(CharacterIterator source) 设置一个迭代的新字符串。 |
static final short |
tertiaryOrder(int order) 返回排序规则元素的第三个组件。 |
继承方法(Inherited methods) |
|
|---|---|
java.lang.Object
|
|
int getMaxExpansion (int 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. |
int getOffset ()
返回原始文本中与下一个整理元素对应的字符偏移量。 (也就是说,getOffset()返回对应于排序规则元素的文本中下一次调用next()时将返回的位置。)此值将始终是排序规则元素对应的第一个字符的索引(a订约字符序列是两个或更多字符全部对应于相同的整理元素时)。 这意味着,如果您立即执行setOffset(x)后面的getOffset(),则getOffset()不一定会返回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(). |
int next ()
获取字符串中的下一个整理元素。
此迭代器迭代从字符串构建的一系列校对元素。 因为不一定有从字符到归类元素的一对一映射,所以这不意味着与“返回字符串中下一个字符的归类元素[或排序优先级]”相同。
该函数返回迭代器当前指向的排序规则元素,然后更新内部指针指向下一个元素。 previous()首先更新指针,然后返回该元素。 这意味着当你在迭代时改变方向(例如,调用next()然后调用previous(),或调用previous()然后调用next())时,你将返回相同的元素两次。
| 返回(Returns) | |
|---|---|
int |
|
int previous ()
获取字符串中以前的整理元素。
此迭代器迭代从字符串构建的一系列校对元素。 因为不一定是从字符到归类元素的一对一映射,所以这不意味着与“返回字符串中前一个字符的归类元素[或排序优先级]”相同。
该函数将迭代器的内部指针更新为指向它当前指向的元素之前的排序规则元素,然后返回该元素,而next()返回当前元素,然后更新指针。 这意味着当你在迭代时改变方向(例如,调用next()然后调用previous(),或调用previous()然后调用next())时,你将返回相同的元素两次。
| 返回(Returns) | |
|---|---|
int |
|
int primaryOrder (int order)
返回排序规则元素的主要组件。
| 参数(Parameters) | |
|---|---|
order |
int: the collation element |
| 返回(Returns) | |
|---|---|
int |
the element's primary component |
short secondaryOrder (int order)
返回排序规则元素的辅助组件。
| 参数(Parameters) | |
|---|---|
order |
int: the collation element |
| 返回(Returns) | |
|---|---|
short |
the element's secondary component |
void setOffset (int newOffset)
设置迭代器指向与指定字符对应的排序规则元素(该参数是原始字符串中的CHARACTER偏移量,而不是其对应的排序元素序列的偏移量)。 下一次调用next()所返回的值将是与文本中指定位置相对应的排序规则元素。 如果该位置位于订约字符序列的中间,则next()的下一个调用结果是该序列的排序规则元素。 这意味着getOffset()不能保证返回与传递给setOffset()的前一个调用相同的值。
| 参数(Parameters) | |
|---|---|
newOffset |
int: The new character offset into the original text. |
void setText (String source)
设置一个迭代的新字符串。
| 参数(Parameters) | |
|---|---|
source |
String: the new source text |
void setText (CharacterIterator source)
设置一个迭代的新字符串。
| 参数(Parameters) | |
|---|---|
source |
CharacterIterator: the new source text. |
short tertiaryOrder (int order)
返回排序规则元素的第三个组件。
| 参数(Parameters) | |
|---|---|
order |
int: the collation element |
| 返回(Returns) | |
|---|---|
short |
the element's tertiary component |