AlphabeticIndex.java revision 860b3c5989a8dd9de73639d8057443aa12fe7b16
1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package libcore.icu;
18
19import java.util.Locale;
20
21/**
22 * Exposes icu4c's AlphabeticIndex.
23 */
24public final class AlphabeticIndex {
25  private long peer;
26
27  /**
28   * Creates a new AlphabeticIndex for the given locale.
29   */
30  public AlphabeticIndex(Locale locale) {
31    peer = create(locale.toString());
32  }
33
34  @Override protected synchronized void finalize() throws Throwable {
35    try {
36      destroy(peer);
37      peer = 0;
38    } finally {
39      super.finalize();
40    }
41  }
42
43  /**
44   * Adds the index characters from the given locale to the index.
45   * The labels are added to those that are already in the index;
46   * they do not replace the existing index characters.
47   * The collation order for this index is not changed;
48   * it remains that of the locale that was originally specified
49   * when creating this index.
50   */
51  public synchronized void addLabels(Locale locale) {
52    addLabels(peer, locale.toString());
53  }
54
55  /**
56   * Returns the index of the bucket in which 's' should appear.
57   */
58  public int getBucketIndex(String s) {
59    return getBucketIndex(peer, s);
60  }
61
62  /**
63   * Returns the label for the bucket at the given index (as returned by getBucketIndex).
64   */
65  public String getBucketLabel(int index) {
66    return getBucketLabel(peer, index);
67  }
68
69  private static native long create(String locale);
70  private static native void destroy(long peer);
71  private static native void addLabels(long peer, String locale);
72  private static native int getBucketIndex(long peer, String s);
73  private static native String getBucketLabel(long peer, int index);
74}
75