Remove NativePluralRules since we are using ICU4J directly.
The following benchmark shows the time taken for ICU4C to
run quantityForInt(5) on the US locale as _native, and _java
shows the time taken for the ICU4J PluralRules.select(5)
method to be run on the US locale.
benchmark us linear runtime
_java 2.27 ==============================
_native 1.89 =========================
vm: app_process
Change-Id: I2ca9a6636ea48bf922eceedce1cf3ef357cc4b99
diff --git a/luni/src/main/java/libcore/icu/NativePluralRules.java b/luni/src/main/java/libcore/icu/NativePluralRules.java
deleted file mode 100644
index f9fe74b..0000000
--- a/luni/src/main/java/libcore/icu/NativePluralRules.java
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * Copyright (C) 2010 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.
- */
-
-package libcore.icu;
-
-import java.util.Locale;
-
-/**
- * Provides access to ICU's
- * <a href="http://icu-project.org/apiref/icu4c/classPluralRules.html">PluralRules</a> class.
- * This is not necessary for Java API, but is used by frameworks/base's resources system to
- * ease localization of strings to languages with complex grammatical rules regarding number.
- */
-public final class NativePluralRules {
- public static final int ZERO = 0;
- public static final int ONE = 1;
- public static final int TWO = 2;
- public static final int FEW = 3;
- public static final int MANY = 4;
- public static final int OTHER = 5;
-
- private final long address;
-
- private NativePluralRules(long address) {
- this.address = address;
- }
-
- @Override protected void finalize() throws Throwable {
- try {
- finalizeImpl(address);
- } finally {
- super.finalize();
- }
- }
-
- public static NativePluralRules forLocale(Locale locale) {
- return new NativePluralRules(forLocaleImpl(locale.toString()));
- }
-
- /**
- * Returns the constant defined in this class corresponding
- * to the first rule that matches the given value.
- */
- public int quantityForInt(int value) {
- // Pre-L compatibility. http://b/18429565.
- if (value < 0) {
- return OTHER;
- }
- return quantityForIntImpl(address, value);
- }
-
- private static native void finalizeImpl(long address);
- private static native long forLocaleImpl(String localeName);
- private static native int quantityForIntImpl(long address, int value);
-}
diff --git a/luni/src/main/native/Register.cpp b/luni/src/main/native/Register.cpp
index c1142aa..d0230ee 100644
--- a/luni/src/main/native/Register.cpp
+++ b/luni/src/main/native/Register.cpp
@@ -61,7 +61,6 @@
REGISTER(register_libcore_icu_NativeConverter);
REGISTER(register_libcore_icu_NativeDecimalFormat);
REGISTER(register_libcore_icu_NativeIDN);
- REGISTER(register_libcore_icu_NativePluralRules);
REGISTER(register_libcore_icu_TimeZoneNames);
REGISTER(register_libcore_icu_Transliterator);
REGISTER(register_libcore_io_AsynchronousCloseMonitor);
diff --git a/luni/src/main/native/libcore_icu_NativePluralRules.cpp b/luni/src/main/native/libcore_icu_NativePluralRules.cpp
deleted file mode 100644
index f278485..0000000
--- a/luni/src/main/native/libcore_icu_NativePluralRules.cpp
+++ /dev/null
@@ -1,82 +0,0 @@
-/*
- * Copyright (C) 2010 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 "NativePluralRules"
-
-#include "IcuUtilities.h"
-#include "JNIHelp.h"
-#include "JniConstants.h"
-#include "JniException.h"
-#include "ScopedUtfChars.h"
-#include "unicode/plurrule.h"
-
-#include <string>
-
-static icu::PluralRules* toPluralRules(jlong address) {
- return reinterpret_cast<icu::PluralRules*>(static_cast<uintptr_t>(address));
-}
-
-static void NativePluralRules_finalizeImpl(JNIEnv*, jclass, jlong address) {
- delete toPluralRules(address);
-}
-
-static jlong NativePluralRules_forLocaleImpl(JNIEnv* env, jclass, jstring javaLocaleName) {
- // The icu4c PluralRules returns a "other: n" default rule for the deprecated locales Java uses.
- // Work around this by translating back to the current language codes.
- std::string localeName(ScopedUtfChars(env, javaLocaleName).c_str());
- if (localeName[0] == 'i' && localeName[1] == 'w') {
- localeName[0] = 'h';
- localeName[1] = 'e';
- } else if (localeName[0] == 'i' && localeName[1] == 'n') {
- localeName[0] = 'i';
- localeName[1] = 'd';
- } else if (localeName[0] == 'j' && localeName[1] == 'i') {
- localeName[0] = 'y';
- localeName[1] = 'i';
- }
-
- icu::Locale locale = icu::Locale::createFromName(localeName.c_str());
- UErrorCode status = U_ZERO_ERROR;
- icu::PluralRules* result = icu::PluralRules::forLocale(locale, status);
- maybeThrowIcuException(env, "PluralRules::forLocale", status);
- return reinterpret_cast<uintptr_t>(result);
-}
-
-static jint NativePluralRules_quantityForIntImpl(JNIEnv*, jclass, jlong address, jint value) {
- icu::UnicodeString keyword = toPluralRules(address)->select(value);
- if (keyword == "zero") {
- return 0;
- } else if (keyword == "one") {
- return 1;
- } else if (keyword == "two") {
- return 2;
- } else if (keyword == "few") {
- return 3;
- } else if (keyword == "many") {
- return 4;
- } else {
- return 5;
- }
-}
-
-static JNINativeMethod gMethods[] = {
- NATIVE_METHOD(NativePluralRules, finalizeImpl, "(J)V"),
- NATIVE_METHOD(NativePluralRules, forLocaleImpl, "(Ljava/lang/String;)J"),
- NATIVE_METHOD(NativePluralRules, quantityForIntImpl, "(JI)I"),
-};
-void register_libcore_icu_NativePluralRules(JNIEnv* env) {
- jniRegisterNativeMethods(env, "libcore/icu/NativePluralRules", gMethods, NELEM(gMethods));
-}
diff --git a/luni/src/main/native/sub.mk b/luni/src/main/native/sub.mk
index 84141db..1906185 100644
--- a/luni/src/main/native/sub.mk
+++ b/luni/src/main/native/sub.mk
@@ -41,7 +41,6 @@
libcore_icu_NativeConverter.cpp \
libcore_icu_NativeDecimalFormat.cpp \
libcore_icu_NativeIDN.cpp \
- libcore_icu_NativePluralRules.cpp \
libcore_icu_TimeZoneNames.cpp \
libcore_icu_Transliterator.cpp \
libcore_io_AsynchronousCloseMonitor.cpp \
diff --git a/luni/src/test/java/libcore/icu/NativePluralRulesTest.java b/luni/src/test/java/libcore/icu/NativePluralRulesTest.java
deleted file mode 100644
index 76179b4..0000000
--- a/luni/src/test/java/libcore/icu/NativePluralRulesTest.java
+++ /dev/null
@@ -1,83 +0,0 @@
-/*
- * Copyright (C) 2009 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.
- */
-
-package libcore.icu;
-
-import java.util.Locale;
-
-public class NativePluralRulesTest extends junit.framework.TestCase {
- public void testNegatives() throws Exception {
- // icu4c's behavior changed, but we prefer to preserve compatibility.
- NativePluralRules en_US = NativePluralRules.forLocale(new Locale("en", "US"));
- assertEquals(NativePluralRules.OTHER, en_US.quantityForInt(2));
- assertEquals(NativePluralRules.ONE, en_US.quantityForInt(1));
- assertEquals(NativePluralRules.OTHER, en_US.quantityForInt(0));
- assertEquals(NativePluralRules.OTHER, en_US.quantityForInt(-1));
- assertEquals(NativePluralRules.OTHER, en_US.quantityForInt(-2));
-
- NativePluralRules ar = NativePluralRules.forLocale(new Locale("ar"));
- assertEquals(NativePluralRules.ZERO, ar.quantityForInt(0));
- assertEquals(NativePluralRules.OTHER, ar.quantityForInt(-1)); // Not ONE.
- assertEquals(NativePluralRules.OTHER, ar.quantityForInt(-2)); // Not TWO.
- assertEquals(NativePluralRules.OTHER, ar.quantityForInt(-3)); // Not FEW.
- assertEquals(NativePluralRules.OTHER, ar.quantityForInt(-11)); // Not MANY.
- assertEquals(NativePluralRules.OTHER, ar.quantityForInt(-100));
- }
-
- public void testEnglish() throws Exception {
- NativePluralRules npr = NativePluralRules.forLocale(new Locale("en", "US"));
- assertEquals(NativePluralRules.OTHER, npr.quantityForInt(0));
- assertEquals(NativePluralRules.ONE, npr.quantityForInt(1));
- assertEquals(NativePluralRules.OTHER, npr.quantityForInt(2));
- }
-
- public void testCzech() throws Exception {
- NativePluralRules npr = NativePluralRules.forLocale(new Locale("cs", "CZ"));
- assertEquals(NativePluralRules.OTHER, npr.quantityForInt(0));
- assertEquals(NativePluralRules.ONE, npr.quantityForInt(1));
- assertEquals(NativePluralRules.FEW, npr.quantityForInt(2));
- assertEquals(NativePluralRules.FEW, npr.quantityForInt(3));
- assertEquals(NativePluralRules.FEW, npr.quantityForInt(4));
- assertEquals(NativePluralRules.OTHER, npr.quantityForInt(5));
- }
-
- public void testArabic() throws Exception {
- NativePluralRules npr = NativePluralRules.forLocale(new Locale("ar"));
- assertEquals(NativePluralRules.ZERO, npr.quantityForInt(0));
- assertEquals(NativePluralRules.ONE, npr.quantityForInt(1));
- assertEquals(NativePluralRules.TWO, npr.quantityForInt(2));
- for (int i = 3; i <= 10; ++i) {
- assertEquals(NativePluralRules.FEW, npr.quantityForInt(i));
- }
- assertEquals(NativePluralRules.MANY, npr.quantityForInt(11));
- assertEquals(NativePluralRules.MANY, npr.quantityForInt(99));
- assertEquals(NativePluralRules.OTHER, npr.quantityForInt(100));
- assertEquals(NativePluralRules.OTHER, npr.quantityForInt(101));
- assertEquals(NativePluralRules.OTHER, npr.quantityForInt(102));
- assertEquals(NativePluralRules.FEW, npr.quantityForInt(103));
- assertEquals(NativePluralRules.MANY, npr.quantityForInt(111));
- }
-
- public void testHebrew() throws Exception {
- // java.util.Locale will translate "he" to the deprecated "iw".
- NativePluralRules he = NativePluralRules.forLocale(new Locale("he"));
- assertEquals(NativePluralRules.ONE, he.quantityForInt(1));
- assertEquals(NativePluralRules.TWO, he.quantityForInt(2));
- assertEquals(NativePluralRules.OTHER, he.quantityForInt(3));
- assertEquals(NativePluralRules.OTHER, he.quantityForInt(10));
- }
-}
-