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 #define LOG_TAG "AlphabeticIndex" 18 19 #include "IcuUtilities.h" 20 #include "JNIHelp.h" 21 #include "JniConstants.h" 22 #include "JniException.h" 23 #include "ScopedJavaUnicodeString.h" 24 #include "unicode/alphaindex.h" 25 #include "unicode/uniset.h" 26 27 static AlphabeticIndex* fromPeer(jlong peer) { 28 return reinterpret_cast<AlphabeticIndex*>(static_cast<uintptr_t>(peer)); 29 } 30 31 static jlong AlphabeticIndex_create(JNIEnv* env, jclass, jstring javaLocale) { 32 UErrorCode status = U_ZERO_ERROR; 33 AlphabeticIndex* ai = new AlphabeticIndex(getLocale(env, javaLocale), status); 34 if (maybeThrowIcuException(env, "AlphabeticIndex", status)) { 35 return 0; 36 } 37 return reinterpret_cast<uintptr_t>(ai); 38 } 39 40 static void AlphabeticIndex_destroy(JNIEnv*, jclass, jlong peer) { 41 delete fromPeer(peer); 42 } 43 44 static jint AlphabeticIndex_getMaxLabelCount(JNIEnv*, jclass, jlong peer) { 45 AlphabeticIndex* ai = fromPeer(peer); 46 return ai->getMaxLabelCount(); 47 } 48 49 static void AlphabeticIndex_setMaxLabelCount(JNIEnv* env, jclass, jlong peer, jint count) { 50 AlphabeticIndex* ai = fromPeer(peer); 51 UErrorCode status = U_ZERO_ERROR; 52 ai->setMaxLabelCount(count, status); 53 maybeThrowIcuException(env, "AlphabeticIndex::setMaxLabelCount", status); 54 } 55 56 static void AlphabeticIndex_addLabels(JNIEnv* env, jclass, jlong peer, jstring javaLocale) { 57 AlphabeticIndex* ai = fromPeer(peer); 58 UErrorCode status = U_ZERO_ERROR; 59 ai->addLabels(getLocale(env, javaLocale), status); 60 maybeThrowIcuException(env, "AlphabeticIndex::addLabels", status); 61 } 62 63 static void AlphabeticIndex_addLabelRange(JNIEnv* env, jclass, jlong peer, 64 jint codePointStart, jint codePointEnd) { 65 AlphabeticIndex* ai = fromPeer(peer); 66 UErrorCode status = U_ZERO_ERROR; 67 ai->addLabels(UnicodeSet(codePointStart, codePointEnd), status); 68 maybeThrowIcuException(env, "AlphabeticIndex::addLabels", status); 69 } 70 71 static jint AlphabeticIndex_getBucketCount(JNIEnv* env, jclass, jlong peer) { 72 AlphabeticIndex* ai = fromPeer(peer); 73 UErrorCode status = U_ZERO_ERROR; 74 jint result = ai->getBucketCount(status); 75 if (maybeThrowIcuException(env, "AlphabeticIndex::getBucketCount", status)) { 76 return -1; 77 } 78 return result; 79 } 80 81 static jint AlphabeticIndex_getBucketIndex(JNIEnv* env, jclass, jlong peer, jstring javaString) { 82 AlphabeticIndex* ai = fromPeer(peer); 83 ScopedJavaUnicodeString string(env, javaString); 84 if (!string.valid()) { 85 return -1; 86 } 87 UErrorCode status = U_ZERO_ERROR; 88 jint result = ai->getBucketIndex(string.unicodeString(), status); 89 if (maybeThrowIcuException(env, "AlphabeticIndex::getBucketIndex", status)) { 90 return -1; 91 } 92 return result; 93 } 94 95 static jstring AlphabeticIndex_getBucketLabel(JNIEnv* env, jclass, jlong peer, jint index) { 96 if (index < 0) { 97 jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException", "Invalid index: %d", index); 98 return NULL; 99 } 100 101 // Iterate to the nth bucket. 102 AlphabeticIndex* ai = fromPeer(peer); 103 UErrorCode status = U_ZERO_ERROR; 104 ai->resetBucketIterator(status); 105 if (maybeThrowIcuException(env, "AlphabeticIndex::resetBucketIterator", status)) { 106 return NULL; 107 } 108 for (jint i = 0; i <= index; ++i) { 109 if (!ai->nextBucket(status)) { 110 jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException", "Invalid index: %d", index); 111 return NULL; 112 } 113 if (maybeThrowIcuException(env, "AlphabeticIndex::nextBucket", status)) { 114 return NULL; 115 } 116 } 117 118 // Return "" for the underflow/inflow/overflow buckets. 119 if (ai->getBucketLabelType() != U_ALPHAINDEX_NORMAL) { 120 return env->NewStringUTF(""); 121 } 122 123 const UnicodeString& label(ai->getBucketLabel()); 124 return env->NewString(label.getBuffer(), label.length()); 125 } 126 127 static jlong AlphabeticIndex_buildImmutableIndex(JNIEnv* env, jclass, jlong peer) { 128 AlphabeticIndex* ai = fromPeer(peer); 129 UErrorCode status = U_ZERO_ERROR; 130 AlphabeticIndex::ImmutableIndex* ii = ai->buildImmutableIndex(status); 131 if (maybeThrowIcuException(env, "AlphabeticIndex::buildImmutableIndex", status)) { 132 return 0; 133 } 134 return reinterpret_cast<uintptr_t>(ii); 135 } 136 137 static AlphabeticIndex::ImmutableIndex* immutableIndexFromPeer(jlong peer) { 138 return reinterpret_cast<AlphabeticIndex::ImmutableIndex*>(static_cast<uintptr_t>(peer)); 139 } 140 141 static jint ImmutableIndex_getBucketCount(JNIEnv*, jclass, jlong peer) { 142 AlphabeticIndex::ImmutableIndex* ii = immutableIndexFromPeer(peer); 143 return ii->getBucketCount(); 144 } 145 146 static jint ImmutableIndex_getBucketIndex(JNIEnv* env, jclass, jlong peer, jstring javaString) { 147 AlphabeticIndex::ImmutableIndex* ii = immutableIndexFromPeer(peer); 148 ScopedJavaUnicodeString string(env, javaString); 149 if (!string.valid()) { 150 return -1; 151 } 152 UErrorCode status = U_ZERO_ERROR; 153 jint result = ii->getBucketIndex(string.unicodeString(), status); 154 if (maybeThrowIcuException(env, "AlphabeticIndex::ImmutableIndex::getBucketIndex", status)) { 155 return -1; 156 } 157 return result; 158 } 159 160 static jstring ImmutableIndex_getBucketLabel(JNIEnv* env, jclass, jlong peer, jint index) { 161 AlphabeticIndex::ImmutableIndex* ii = immutableIndexFromPeer(peer); 162 const AlphabeticIndex::Bucket* bucket = ii->getBucket(index); 163 if (bucket == NULL) { 164 jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException", "Invalid index: %d", index); 165 return NULL; 166 } 167 168 // Return "" for the underflow/inflow/overflow buckets. 169 if (bucket->getLabelType() != U_ALPHAINDEX_NORMAL) { 170 return env->NewStringUTF(""); 171 } 172 173 const UnicodeString& label(bucket->getLabel()); 174 return env->NewString(label.getBuffer(), label.length()); 175 } 176 177 static JNINativeMethod gMethods[] = { 178 NATIVE_METHOD(AlphabeticIndex, create, "(Ljava/lang/String;)J"), 179 NATIVE_METHOD(AlphabeticIndex, destroy, "(J)V"), 180 NATIVE_METHOD(AlphabeticIndex, getMaxLabelCount, "(J)I"), 181 NATIVE_METHOD(AlphabeticIndex, setMaxLabelCount, "(JI)V"), 182 NATIVE_METHOD(AlphabeticIndex, addLabels, "(JLjava/lang/String;)V"), 183 NATIVE_METHOD(AlphabeticIndex, addLabelRange, "(JII)V"), 184 NATIVE_METHOD(AlphabeticIndex, getBucketCount, "(J)I"), 185 NATIVE_METHOD(AlphabeticIndex, getBucketIndex, "(JLjava/lang/String;)I"), 186 NATIVE_METHOD(AlphabeticIndex, getBucketLabel, "(JI)Ljava/lang/String;"), 187 NATIVE_METHOD(AlphabeticIndex, buildImmutableIndex, "(J)J"), 188 }; 189 static JNINativeMethod gImmutableIndexMethods[] = { 190 NATIVE_METHOD(ImmutableIndex, getBucketCount, "(J)I"), 191 NATIVE_METHOD(ImmutableIndex, getBucketIndex, "(JLjava/lang/String;)I"), 192 NATIVE_METHOD(ImmutableIndex, getBucketLabel, "(JI)Ljava/lang/String;"), 193 }; 194 void register_libcore_icu_AlphabeticIndex(JNIEnv* env) { 195 jniRegisterNativeMethods(env, "libcore/icu/AlphabeticIndex", gMethods, NELEM(gMethods)); 196 jniRegisterNativeMethods(env, "libcore/icu/AlphabeticIndex$ImmutableIndex", gImmutableIndexMethods, NELEM(gImmutableIndexMethods)); 197 } 198