1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
/**
*******************************************************************************
* Copyright (C) 1996-2005, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
*******************************************************************************
*/
package libcore.icu;
import java.text.CharacterIterator;
/**
* Collation element iterator JNI wrapper.
* Iterates over the collation elements of a data string.
* The iterator supports both forward and backwards full iteration, ie if
* backwards iteration is performed in the midst of a forward iteration, the
* result is undefined.
* To perform a backwards iteration in the midst of a forward iteration,
* reset() has to be called.
* This will shift the position to either the start or the last character in the
* data string depending on whether next() is called or previous().
* <pre>
* RuleBasedCollator coll = Collator.getInstance();
* CollationElementIterator iterator = coll.getCollationElementIterator("abc");
* int ce = 0;
* while (ce != CollationElementIterator.NULLORDER) {
* ce = iterator.next();
* }
* iterator.reset();
* while (ce != CollationElementIterator.NULLORDER) {
* ce = iterator.previous();
* }
* </pre>
* @author syn wee quek
* @stable ICU 2.4
*/
public final class CollationElementIteratorICU {
/**
* Reset the collation elements to their initial state.
* This will move the 'cursor' to the beginning of the text.
* @stable ICU 2.4
*/
public void reset() {
NativeCollation.reset(address);
}
/**
* Get the ordering priority of the next collation element in the text.
* A single character may contain more than one collation element.
* @return next collation elements ordering, or NULLORDER if the end of the
* text is reached.
* @stable ICU 2.4
*/
public int next() {
return NativeCollation.next(address);
}
/**
* Get the ordering priority of the previous collation element in the text.
* A single character may contain more than one collation element.
* @return previous collation element ordering, or NULLORDER if the end of
* the text is reached.
* @stable ICU 2.4
*/
public int previous() {
return NativeCollation.previous(address);
}
/**
* Get the maximum length of any expansion sequences that end with the
* specified comparison order.
* @param order collation order returned by previous or next.
* @return maximum size of the expansion sequences ending with the collation
* element or 1 if collation element does not occur at the end of
* any expansion sequence
* @stable ICU 2.4
*/
public int getMaxExpansion(int order) {
return NativeCollation.getMaxExpansion(address, order);
}
/**
* Set the text containing the collation elements.
* @param source text containing the collation elements.
* @stable ICU 2.4
*/
public void setText(String source) {
NativeCollation.setText(address, source);
}
public void setText(CharacterIterator source) {
NativeCollation.setText(address, source.toString());
}
/**
* Get the offset of the current source character.
* This is an offset into the text of the character containing the current
* collation elements.
* @return offset of the current source character.
* @stable ICU 2.4
*/
public int getOffset() {
return NativeCollation.getOffset(address);
}
/**
* Set the offset of the current source character.
* This is an offset into the text of the character to be processed.
* @param offset The desired character offset.
* @stable ICU 2.4
*/
public void setOffset(int offset) {
NativeCollation.setOffset(address, offset);
}
/**
* Gets the primary order of a collation order.
* @param order the collation order
* @return the primary order of a collation order.
* @stable ICU 2.4
*/
public static int primaryOrder(int order) {
return ((order & PRIMARY_ORDER_MASK_) >> PRIMARY_ORDER_SHIFT_) &
UNSIGNED_16_BIT_MASK_;
}
/**
* Gets the secondary order of a collation order.
* @param order the collation order
* @return the secondary order of a collation order.
* @stable ICU 2.4
*/
public static int secondaryOrder(int order) {
return (order & SECONDARY_ORDER_MASK_) >> SECONDARY_ORDER_SHIFT_;
}
/**
* Gets the tertiary order of a collation order.
* @param order the collation order
* @return the tertiary order of a collation order.
* @stable ICU 2.4
*/
public static int tertiaryOrder(int order) {
return order & TERTIARY_ORDER_MASK_;
}
public static CollationElementIteratorICU getInstance(long collatorAddress, String source) {
long iteratorAddress = NativeCollation.getCollationElementIterator(collatorAddress, source);
return new CollationElementIteratorICU(iteratorAddress);
}
private CollationElementIteratorICU(long address) {
this.address = address;
}
// protected methods --------------------------------------------
/**
* Garbage collection.
* Close C collator and reclaim memory.
* @stable ICU 2.4
*/
@Override protected void finalize() throws Throwable {
try {
NativeCollation.closeElements(address);
} finally {
super.finalize();
}
}
// private data members -----------------------------------------
/**
* C collator
*/
private final long address;
/**
* ICU constant primary order mask for collation elements
*/
private static final int PRIMARY_ORDER_MASK_ = 0xffff0000;
/**
* ICU constant secondary order mask for collation elements
*/
private static final int SECONDARY_ORDER_MASK_ = 0x0000ff00;
/**
* ICU constant tertiary order mask for collation elements
*/
private static final int TERTIARY_ORDER_MASK_ = 0x000000ff;
/**
* ICU constant primary order shift for collation elements
*/
private static final int PRIMARY_ORDER_SHIFT_ = 16;
/**
* ICU constant secondary order shift for collation elements
*/
private static final int SECONDARY_ORDER_SHIFT_ = 8;
/**
* Unsigned 16 bit mask
*/
private static final int UNSIGNED_16_BIT_MASK_ = 0x0000FFFF;
}
|