blob: bda80c0d15f0fd26483d0562dc821aeb28028171 [file] [log] [blame]
/*
* 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.
*/
#define LOG_TAG "AlphabeticIndex"
#include "IcuUtilities.h"
#include "JNIHelp.h"
#include "JniConstants.h"
#include "JniException.h"
#include "ScopedJavaUnicodeString.h"
#include "ScopedStringChars.h"
#include "unicode/alphaindex.h"
#include "unicode/uniset.h"
static AlphabeticIndex* fromPeer(jlong peer) {
return reinterpret_cast<AlphabeticIndex*>(static_cast<uintptr_t>(peer));
}
static jlong AlphabeticIndex_create(JNIEnv* env, jclass, jstring javaLocale) {
UErrorCode status = U_ZERO_ERROR;
AlphabeticIndex* ai = new AlphabeticIndex(getLocale(env, javaLocale), status);
if (maybeThrowIcuException(env, "AlphabeticIndex", status)) {
return 0;
}
return reinterpret_cast<uintptr_t>(ai);
}
static void AlphabeticIndex_destroy(JNIEnv*, jclass, jlong peer) {
delete fromPeer(peer);
}
static void AlphabeticIndex_addLabels(JNIEnv* env, jclass, jlong peer, jstring javaLocale) {
AlphabeticIndex* ai = fromPeer(peer);
UErrorCode status = U_ZERO_ERROR;
ai->addLabels(getLocale(env, javaLocale), status);
maybeThrowIcuException(env, "AlphabeticIndex::addLabels", status);
}
static void AlphabeticIndex_addLabelRange(JNIEnv* env, jclass, jlong peer,
jint codePointStart, jint codePointEnd) {
AlphabeticIndex* ai = fromPeer(peer);
UErrorCode status = U_ZERO_ERROR;
ai->addLabels(UnicodeSet(codePointStart, codePointEnd), status);
maybeThrowIcuException(env, "AlphabeticIndex::addLabels", status);
}
static jint AlphabeticIndex_getBucketCount(JNIEnv* env, jclass, jlong peer) {
AlphabeticIndex* ai = fromPeer(peer);
UErrorCode status = U_ZERO_ERROR;
jint result = ai->getBucketCount(status);
if (maybeThrowIcuException(env, "AlphabeticIndex::getBucketCount", status)) {
return -1;
}
return result;
}
static jint AlphabeticIndex_getBucketIndex(JNIEnv* env, jclass, jlong peer, jstring javaString) {
AlphabeticIndex* ai = fromPeer(peer);
ScopedJavaUnicodeString string(env, javaString);
if (!string.valid()) {
return -1;
}
UErrorCode status = U_ZERO_ERROR;
jint result = ai->getBucketIndex(string.unicodeString(), status);
if (maybeThrowIcuException(env, "AlphabeticIndex::getBucketIndex", status)) {
return -1;
}
return result;
}
static jstring AlphabeticIndex_getBucketLabel(JNIEnv* env, jclass, jlong peer, jint index) {
if (index < 0) {
jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException", "Invalid index: %d", index);
return NULL;
}
// Iterate to the nth bucket.
AlphabeticIndex* ai = fromPeer(peer);
UErrorCode status = U_ZERO_ERROR;
ai->resetBucketIterator(status);
if (maybeThrowIcuException(env, "AlphabeticIndex::resetBucketIterator", status)) {
return NULL;
}
for (jint i = 0; i <= index; ++i) {
if (!ai->nextBucket(status)) {
jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException", "Invalid index: %d", index);
return NULL;
}
if (maybeThrowIcuException(env, "AlphabeticIndex::nextBucket", status)) {
return NULL;
}
}
// Return "" for the underflow/inflow/overflow buckets.
if (ai->getBucketLabelType() != U_ALPHAINDEX_NORMAL) {
return env->NewStringUTF("");
}
const UnicodeString& label(ai->getBucketLabel());
return env->NewString(label.getBuffer(), label.length());
}
static JNINativeMethod gMethods[] = {
NATIVE_METHOD(AlphabeticIndex, create, "(Ljava/lang/String;)J"),
NATIVE_METHOD(AlphabeticIndex, destroy, "(J)V"),
NATIVE_METHOD(AlphabeticIndex, addLabels, "(JLjava/lang/String;)V"),
NATIVE_METHOD(AlphabeticIndex, addLabelRange, "(JII)V"),
NATIVE_METHOD(AlphabeticIndex, getBucketCount, "(J)I"),
NATIVE_METHOD(AlphabeticIndex, getBucketIndex, "(JLjava/lang/String;)I"),
NATIVE_METHOD(AlphabeticIndex, getBucketLabel, "(JI)Ljava/lang/String;"),
};
void register_libcore_icu_AlphabeticIndex(JNIEnv* env) {
jniRegisterNativeMethods(env, "libcore/icu/AlphabeticIndex", gMethods, NELEM(gMethods));
}