17775409378596fd980dbd576f9738d337bd8a76dElliott Hughes/*
27775409378596fd980dbd576f9738d337bd8a76dElliott Hughes * Copyright (C) 2010 The Android Open Source Project
37775409378596fd980dbd576f9738d337bd8a76dElliott Hughes *
47775409378596fd980dbd576f9738d337bd8a76dElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
57775409378596fd980dbd576f9738d337bd8a76dElliott Hughes * you may not use this file except in compliance with the License.
67775409378596fd980dbd576f9738d337bd8a76dElliott Hughes * You may obtain a copy of the License at
77775409378596fd980dbd576f9738d337bd8a76dElliott Hughes *
87775409378596fd980dbd576f9738d337bd8a76dElliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
97775409378596fd980dbd576f9738d337bd8a76dElliott Hughes *
107775409378596fd980dbd576f9738d337bd8a76dElliott Hughes * Unless required by applicable law or agreed to in writing, software
117775409378596fd980dbd576f9738d337bd8a76dElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
127775409378596fd980dbd576f9738d337bd8a76dElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137775409378596fd980dbd576f9738d337bd8a76dElliott Hughes * See the License for the specific language governing permissions and
147775409378596fd980dbd576f9738d337bd8a76dElliott Hughes * limitations under the License.
157775409378596fd980dbd576f9738d337bd8a76dElliott Hughes */
167775409378596fd980dbd576f9738d337bd8a76dElliott Hughes
177775409378596fd980dbd576f9738d337bd8a76dElliott Hughes#define LOG_TAG "NativePluralRules"
187775409378596fd980dbd576f9738d337bd8a76dElliott Hughes
193aac4ddc4d17c07fa8b4908069d23d5401a77993Elliott Hughes#include "IcuUtilities.h"
207775409378596fd980dbd576f9738d337bd8a76dElliott Hughes#include "JNIHelp.h"
21e22935d3c7040c22b48d53bd18878844f381287cElliott Hughes#include "JniConstants.h"
22bef9ec33e1368f57c731fce63b6a8c61628c64b0Elliott Hughes#include "JniException.h"
237775409378596fd980dbd576f9738d337bd8a76dElliott Hughes#include "ScopedUtfChars.h"
247775409378596fd980dbd576f9738d337bd8a76dElliott Hughes#include "unicode/plurrule.h"
257775409378596fd980dbd576f9738d337bd8a76dElliott Hughes
26d4f2bbad3cf3b479e76f4913241e2d45010fbb60Elliott Hughes#include <string>
27d4f2bbad3cf3b479e76f4913241e2d45010fbb60Elliott Hughes
281e5d730e58d94c3bfa14b7dde5ab3981fe5a170bElliott Hughesstatic PluralRules* toPluralRules(jlong address) {
297775409378596fd980dbd576f9738d337bd8a76dElliott Hughes    return reinterpret_cast<PluralRules*>(static_cast<uintptr_t>(address));
307775409378596fd980dbd576f9738d337bd8a76dElliott Hughes}
317775409378596fd980dbd576f9738d337bd8a76dElliott Hughes
321e5d730e58d94c3bfa14b7dde5ab3981fe5a170bElliott Hughesstatic void NativePluralRules_finalizeImpl(JNIEnv*, jclass, jlong address) {
337775409378596fd980dbd576f9738d337bd8a76dElliott Hughes    delete toPluralRules(address);
347775409378596fd980dbd576f9738d337bd8a76dElliott Hughes}
357775409378596fd980dbd576f9738d337bd8a76dElliott Hughes
361e5d730e58d94c3bfa14b7dde5ab3981fe5a170bElliott Hughesstatic jlong NativePluralRules_forLocaleImpl(JNIEnv* env, jclass, jstring javaLocaleName) {
37d4f2bbad3cf3b479e76f4913241e2d45010fbb60Elliott Hughes    // The icu4c PluralRules returns a "other: n" default rule for the deprecated locales Java uses.
38d4f2bbad3cf3b479e76f4913241e2d45010fbb60Elliott Hughes    // Work around this by translating back to the current language codes.
39d4f2bbad3cf3b479e76f4913241e2d45010fbb60Elliott Hughes    std::string localeName(ScopedUtfChars(env, javaLocaleName).c_str());
40d4f2bbad3cf3b479e76f4913241e2d45010fbb60Elliott Hughes    if (localeName[0] == 'i' && localeName[1] == 'w') {
41d4f2bbad3cf3b479e76f4913241e2d45010fbb60Elliott Hughes        localeName[0] = 'h';
42d4f2bbad3cf3b479e76f4913241e2d45010fbb60Elliott Hughes        localeName[1] = 'e';
43d4f2bbad3cf3b479e76f4913241e2d45010fbb60Elliott Hughes    } else if (localeName[0] == 'i' && localeName[1] == 'n') {
44d4f2bbad3cf3b479e76f4913241e2d45010fbb60Elliott Hughes        localeName[0] = 'i';
45d4f2bbad3cf3b479e76f4913241e2d45010fbb60Elliott Hughes        localeName[1] = 'd';
46d4f2bbad3cf3b479e76f4913241e2d45010fbb60Elliott Hughes    } else if (localeName[0] == 'j' && localeName[1] == 'i') {
47d4f2bbad3cf3b479e76f4913241e2d45010fbb60Elliott Hughes        localeName[0] = 'y';
48d4f2bbad3cf3b479e76f4913241e2d45010fbb60Elliott Hughes        localeName[1] = 'i';
49d4f2bbad3cf3b479e76f4913241e2d45010fbb60Elliott Hughes    }
50d4f2bbad3cf3b479e76f4913241e2d45010fbb60Elliott Hughes
51d4f2bbad3cf3b479e76f4913241e2d45010fbb60Elliott Hughes    Locale locale = Locale::createFromName(localeName.c_str());
527775409378596fd980dbd576f9738d337bd8a76dElliott Hughes    UErrorCode status = U_ZERO_ERROR;
537775409378596fd980dbd576f9738d337bd8a76dElliott Hughes    PluralRules* result = PluralRules::forLocale(locale, status);
545ec69b20ab9b3e2dcbe225d548168b09afbbbac2Elliott Hughes    maybeThrowIcuException(env, "PluralRules::forLocale", status);
557775409378596fd980dbd576f9738d337bd8a76dElliott Hughes    return reinterpret_cast<uintptr_t>(result);
567775409378596fd980dbd576f9738d337bd8a76dElliott Hughes}
577775409378596fd980dbd576f9738d337bd8a76dElliott Hughes
581e5d730e58d94c3bfa14b7dde5ab3981fe5a170bElliott Hughesstatic jint NativePluralRules_quantityForIntImpl(JNIEnv*, jclass, jlong address, jint value) {
597775409378596fd980dbd576f9738d337bd8a76dElliott Hughes    UnicodeString keyword = toPluralRules(address)->select(value);
607775409378596fd980dbd576f9738d337bd8a76dElliott Hughes    if (keyword == "zero") {
617775409378596fd980dbd576f9738d337bd8a76dElliott Hughes        return 0;
627775409378596fd980dbd576f9738d337bd8a76dElliott Hughes    } else if (keyword == "one") {
637775409378596fd980dbd576f9738d337bd8a76dElliott Hughes        return 1;
647775409378596fd980dbd576f9738d337bd8a76dElliott Hughes    } else if (keyword == "two") {
657775409378596fd980dbd576f9738d337bd8a76dElliott Hughes        return 2;
667775409378596fd980dbd576f9738d337bd8a76dElliott Hughes    } else if (keyword == "few") {
677775409378596fd980dbd576f9738d337bd8a76dElliott Hughes        return 3;
687775409378596fd980dbd576f9738d337bd8a76dElliott Hughes    } else if (keyword == "many") {
697775409378596fd980dbd576f9738d337bd8a76dElliott Hughes        return 4;
707775409378596fd980dbd576f9738d337bd8a76dElliott Hughes    } else {
717775409378596fd980dbd576f9738d337bd8a76dElliott Hughes        return 5;
727775409378596fd980dbd576f9738d337bd8a76dElliott Hughes    }
737775409378596fd980dbd576f9738d337bd8a76dElliott Hughes}
747775409378596fd980dbd576f9738d337bd8a76dElliott Hughes
757775409378596fd980dbd576f9738d337bd8a76dElliott Hughesstatic JNINativeMethod gMethods[] = {
761e5d730e58d94c3bfa14b7dde5ab3981fe5a170bElliott Hughes    NATIVE_METHOD(NativePluralRules, finalizeImpl, "(J)V"),
771e5d730e58d94c3bfa14b7dde5ab3981fe5a170bElliott Hughes    NATIVE_METHOD(NativePluralRules, forLocaleImpl, "(Ljava/lang/String;)J"),
781e5d730e58d94c3bfa14b7dde5ab3981fe5a170bElliott Hughes    NATIVE_METHOD(NativePluralRules, quantityForIntImpl, "(JI)I"),
797775409378596fd980dbd576f9738d337bd8a76dElliott Hughes};
807cd6760f7045d771faae8080a8c6150bf678f679Elliott Hughesvoid register_libcore_icu_NativePluralRules(JNIEnv* env) {
817cd6760f7045d771faae8080a8c6150bf678f679Elliott Hughes    jniRegisterNativeMethods(env, "libcore/icu/NativePluralRules", gMethods, NELEM(gMethods));
827775409378596fd980dbd576f9738d337bd8a76dElliott Hughes}
83