Home | History | Annotate | Download | only in native
      1 /*
      2  * Copyright (C) 2010 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 "NativePluralRules"
     18 
     19 #include "JNIHelp.h"
     20 #include "JniConstants.h"
     21 #include "JniException.h"
     22 #include "ScopedUtfChars.h"
     23 #include "unicode/plurrule.h"
     24 
     25 static PluralRules* toPluralRules(jint address) {
     26     return reinterpret_cast<PluralRules*>(static_cast<uintptr_t>(address));
     27 }
     28 
     29 static void NativePluralRules_finalizeImpl(JNIEnv*, jclass, jint address) {
     30     delete toPluralRules(address);
     31 }
     32 
     33 static jint NativePluralRules_forLocaleImpl(JNIEnv* env, jclass, jstring localeName) {
     34     Locale locale = Locale::createFromName(ScopedUtfChars(env, localeName).c_str());
     35     UErrorCode status = U_ZERO_ERROR;
     36     PluralRules* result = PluralRules::forLocale(locale, status);
     37     maybeThrowIcuException(env, status);
     38     return reinterpret_cast<uintptr_t>(result);
     39 }
     40 
     41 static jint NativePluralRules_quantityForIntImpl(JNIEnv*, jclass, jint address, jint value) {
     42     UnicodeString keyword = toPluralRules(address)->select(value);
     43     if (keyword == "zero") {
     44         return 0;
     45     } else if (keyword == "one") {
     46         return 1;
     47     } else if (keyword == "two") {
     48         return 2;
     49     } else if (keyword == "few") {
     50         return 3;
     51     } else if (keyword == "many") {
     52         return 4;
     53     } else {
     54         return 5;
     55     }
     56 }
     57 
     58 static JNINativeMethod gMethods[] = {
     59     NATIVE_METHOD(NativePluralRules, finalizeImpl, "(I)V"),
     60     NATIVE_METHOD(NativePluralRules, forLocaleImpl, "(Ljava/lang/String;)I"),
     61     NATIVE_METHOD(NativePluralRules, quantityForIntImpl, "(II)I"),
     62 };
     63 void register_libcore_icu_NativePluralRules(JNIEnv* env) {
     64     jniRegisterNativeMethods(env, "libcore/icu/NativePluralRules", gMethods, NELEM(gMethods));
     65 }
     66