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
|
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package libcore.icu;
import java.util.Locale;
/**
* Exposes icu4c's AlphabeticIndex.
*/
public final class AlphabeticIndex {
/**
* Exposes icu4c's ImmutableIndex (new to icu 51). This exposes a read-only,
* thread safe snapshot view of an AlphabeticIndex at the moment it was
* created, and allows for random access to buckets by index.
*/
public static final class ImmutableIndex {
private long peer;
private ImmutableIndex(long peer) {
this.peer = peer;
}
@Override protected synchronized void finalize() throws Throwable {
try {
destroy(peer);
peer = 0;
} finally {
super.finalize();
}
}
/**
* Returns the number of the label buckets in this index.
*/
public int getBucketCount() {
return getBucketCount(peer);
}
/**
* Returns the index of the bucket in which 's' should appear.
* Function is synchronized because underlying routine walks an iterator
* whose state is maintained inside the index object.
*/
public int getBucketIndex(String s) {
return getBucketIndex(peer, s);
}
/**
* Returns the label for the bucket at the given index (as returned by getBucketIndex).
*/
public String getBucketLabel(int index) {
return getBucketLabel(peer, index);
}
private static native int getBucketCount(long peer);
private static native int getBucketIndex(long peer, String s);
private static native String getBucketLabel(long peer, int index);
}
private long peer;
/**
* Creates a new AlphabeticIndex for the given locale.
*/
public AlphabeticIndex(Locale locale) {
peer = create(locale.toString());
}
@Override protected synchronized void finalize() throws Throwable {
try {
destroy(peer);
peer = 0;
} finally {
super.finalize();
}
}
/**
* Returns the max number of the label buckets allowed in this index.
*/
public synchronized int getMaxLabelCount() {
return getMaxLabelCount(peer);
}
/**
* Sets the max number of the label buckets in this index.
* (ICU 51 default is 99)
*/
public synchronized AlphabeticIndex setMaxLabelCount(int count) {
setMaxLabelCount(peer, count);
return this;
}
/**
* Adds the index characters from the given locale to the index.
* The labels are added to those that are already in the index;
* they do not replace the existing index characters.
* The collation order for this index is not changed;
* it remains that of the locale that was originally specified
* when creating this index.
*/
public synchronized AlphabeticIndex addLabels(Locale locale) {
addLabels(peer, locale.toString());
return this;
}
/**
* Adds the index characters in the range between the specified start and
* end code points, inclusive.
*/
public synchronized AlphabeticIndex addLabelRange(int codePointStart, int codePointEnd) {
addLabelRange(peer, codePointStart, codePointEnd);
return this;
}
/**
* Returns the number of the label buckets in this index.
*/
public synchronized int getBucketCount() {
return getBucketCount(peer);
}
/**
* Returns the index of the bucket in which 's' should appear.
* Function is synchronized because underlying routine walks an iterator
* whose state is maintained inside the index object.
*/
public synchronized int getBucketIndex(String s) {
return getBucketIndex(peer, s);
}
/**
* Returns the label for the bucket at the given index (as returned by getBucketIndex).
*/
public synchronized String getBucketLabel(int index) {
return getBucketLabel(peer, index);
}
/**
* Returns an ImmutableIndex created from this AlphabeticIndex.
*/
public synchronized ImmutableIndex getImmutableIndex() {
return new ImmutableIndex(buildImmutableIndex(peer));
}
private static native long create(String locale);
private static native void destroy(long peer);
private static native int getMaxLabelCount(long peer);
private static native void setMaxLabelCount(long peer, int count);
private static native void addLabels(long peer, String locale);
private static native void addLabelRange(long peer, int codePointStart, int codePointEnd);
private static native int getBucketCount(long peer);
private static native int getBucketIndex(long peer, String s);
private static native String getBucketLabel(long peer, int index);
private static native long buildImmutableIndex(long peer);
}
|