AlphabeticIndex.java revision a7eab938026a211a8065b5c695080a6cdd7055a1
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 * Adds the index characters in the range between the specified start and 57 * end code points, inclusive. 58 */ 59 public synchronized void addLabelRange(int codePointStart, int codePointEnd) { 60 addLabelRange(peer, codePointStart, codePointEnd); 61 } 62 63 /** 64 * Returns the number of the label buckets in this index. 65 */ 66 public synchronized int getBucketCount() { 67 return getBucketCount(peer); 68 } 69 70 /** 71 * Returns the index of the bucket in which 's' should appear. 72 * Function is synchronized because underlying routine walks an iterator 73 * whose state is maintained inside the index object. 74 */ 75 public synchronized int getBucketIndex(String s) { 76 return getBucketIndex(peer, s); 77 } 78 79 /** 80 * Returns the label for the bucket at the given index (as returned by getBucketIndex). 81 */ 82 public String getBucketLabel(int index) { 83 return getBucketLabel(peer, index); 84 } 85 86 private static native long create(String locale); 87 private static native void destroy(long peer); 88 private static native void addLabels(long peer, String locale); 89 private static native void addLabelRange(long peer, int codePointStart, int codePointEnd); 90 private static native int getBucketCount(long peer); 91 private static native int getBucketIndex(long peer, String s); 92 private static native String getBucketLabel(long peer, int index); 93} 94