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 17 package libcore.icu; 18 19 import java.util.Locale; 20 21 /** 22 * Exposes icu4c's AlphabeticIndex. 23 */ 24 public final class AlphabeticIndex { 25 26 /** 27 * Exposes icu4c's ImmutableIndex (new to icu 51). This exposes a read-only, 28 * thread safe snapshot view of an AlphabeticIndex at the moment it was 29 * created, and allows for random access to buckets by index. 30 */ 31 public static final class ImmutableIndex { 32 private long peer; 33 34 private ImmutableIndex(long peer) { 35 this.peer = peer; 36 } 37 38 @Override protected synchronized void finalize() throws Throwable { 39 try { 40 destroy(peer); 41 peer = 0; 42 } finally { 43 super.finalize(); 44 } 45 } 46 47 /** 48 * Returns the number of the label buckets in this index. 49 */ 50 public int getBucketCount() { 51 return getBucketCount(peer); 52 } 53 54 /** 55 * Returns the index of the bucket in which 's' should appear. 56 * Function is synchronized because underlying routine walks an iterator 57 * whose state is maintained inside the index object. 58 */ 59 public int getBucketIndex(String s) { 60 return getBucketIndex(peer, s); 61 } 62 63 /** 64 * Returns the label for the bucket at the given index (as returned by getBucketIndex). 65 */ 66 public String getBucketLabel(int index) { 67 return getBucketLabel(peer, index); 68 } 69 70 private static native int getBucketCount(long peer); 71 private static native int getBucketIndex(long peer, String s); 72 private static native String getBucketLabel(long peer, int index); 73 } 74 75 private long peer; 76 77 /** 78 * Creates a new AlphabeticIndex for the given locale. 79 */ 80 public AlphabeticIndex(Locale locale) { 81 peer = create(locale.toString()); 82 } 83 84 @Override protected synchronized void finalize() throws Throwable { 85 try { 86 destroy(peer); 87 peer = 0; 88 } finally { 89 super.finalize(); 90 } 91 } 92 93 /** 94 * Returns the max number of the label buckets allowed in this index. 95 */ 96 public synchronized int getMaxLabelCount() { 97 return getMaxLabelCount(peer); 98 } 99 100 /** 101 * Sets the max number of the label buckets in this index. 102 * (ICU 51 default is 99) 103 */ 104 public synchronized AlphabeticIndex setMaxLabelCount(int count) { 105 setMaxLabelCount(peer, count); 106 return this; 107 } 108 109 /** 110 * Adds the index characters from the given locale to the index. 111 * The labels are added to those that are already in the index; 112 * they do not replace the existing index characters. 113 * The collation order for this index is not changed; 114 * it remains that of the locale that was originally specified 115 * when creating this index. 116 */ 117 public synchronized AlphabeticIndex addLabels(Locale locale) { 118 addLabels(peer, locale.toString()); 119 return this; 120 } 121 122 /** 123 * Adds the index characters in the range between the specified start and 124 * end code points, inclusive. 125 */ 126 public synchronized AlphabeticIndex addLabelRange(int codePointStart, int codePointEnd) { 127 addLabelRange(peer, codePointStart, codePointEnd); 128 return this; 129 } 130 131 /** 132 * Returns the number of the label buckets in this index. 133 */ 134 public synchronized int getBucketCount() { 135 return getBucketCount(peer); 136 } 137 138 /** 139 * Returns the index of the bucket in which 's' should appear. 140 * Function is synchronized because underlying routine walks an iterator 141 * whose state is maintained inside the index object. 142 */ 143 public synchronized int getBucketIndex(String s) { 144 return getBucketIndex(peer, s); 145 } 146 147 /** 148 * Returns the label for the bucket at the given index (as returned by getBucketIndex). 149 */ 150 public synchronized String getBucketLabel(int index) { 151 return getBucketLabel(peer, index); 152 } 153 154 /** 155 * Returns an ImmutableIndex created from this AlphabeticIndex. 156 */ 157 public synchronized ImmutableIndex getImmutableIndex() { 158 return new ImmutableIndex(buildImmutableIndex(peer)); 159 } 160 161 private static native long create(String locale); 162 private static native void destroy(long peer); 163 private static native int getMaxLabelCount(long peer); 164 private static native void setMaxLabelCount(long peer, int count); 165 private static native void addLabels(long peer, String locale); 166 private static native void addLabelRange(long peer, int codePointStart, int codePointEnd); 167 private static native int getBucketCount(long peer); 168 private static native int getBucketIndex(long peer, String s); 169 private static native String getBucketLabel(long peer, int index); 170 private static native long buildImmutableIndex(long peer); 171 } 172