summaryrefslogtreecommitdiffstats
path: root/icu/src/main/java
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2010-02-12 15:33:53 -0800
committerElliott Hughes <enh@google.com>2010-02-12 15:33:53 -0800
commitaa3919543be785a76742c5ae2a753178cb2d928a (patch)
treec1403aa6d52faa5e8a08ccf24cda52e0b8f802d8 /icu/src/main/java
parent4b7692c57a135501e0494cfbfd905e084d2fb79f (diff)
downloadlibcore-aa3919543be785a76742c5ae2a753178cb2d928a.zip
libcore-aa3919543be785a76742c5ae2a753178cb2d928a.tar.gz
libcore-aa3919543be785a76742c5ae2a753178cb2d928a.tar.bz2
Use one method to create a Locale from a String.
Bug: 2392157
Diffstat (limited to 'icu/src/main/java')
-rw-r--r--icu/src/main/java/com/ibm/icu4jni/text/BreakIterator.java32
-rw-r--r--icu/src/main/java/com/ibm/icu4jni/text/Collator.java36
-rw-r--r--icu/src/main/java/com/ibm/icu4jni/util/Resources.java29
3 files changed, 34 insertions, 63 deletions
diff --git a/icu/src/main/java/com/ibm/icu4jni/text/BreakIterator.java b/icu/src/main/java/com/ibm/icu4jni/text/BreakIterator.java
index 5ef1161..aa925aa 100644
--- a/icu/src/main/java/com/ibm/icu4jni/text/BreakIterator.java
+++ b/icu/src/main/java/com/ibm/icu4jni/text/BreakIterator.java
@@ -16,6 +16,7 @@
package com.ibm.icu4jni.text;
+import com.ibm.icu4jni.util.Resources;
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import java.util.Locale;
@@ -30,36 +31,7 @@ public abstract class BreakIterator implements Cloneable
protected int type = 0;
public static Locale[] getAvailableLocales() {
-
- String[] locales = NativeBreakIterator.getAvailableLocalesImpl();
-
- Locale[] result = new Locale[locales.length];
-
- String locale;
-
- int index, index2;
-
- for(int i = 0; i < locales.length; i++) {
- locale = locales[i];
-
- index = locale.indexOf('_');
- index2 = locale.lastIndexOf('_');
-
- if(index == -1) {
- result[i] = new Locale(locales[i]);
- } else if(index > 0 && index == index2) {
- result[i] = new Locale(
- locale.substring(0,index),
- locale.substring(index+1));
- } else if(index > 0 && index2 > index) {
- result[i] = new Locale(
- locale.substring(0,index),
- locale.substring(index+1,index2),
- locale.substring(index2+1));
- }
- }
-
- return result;
+ return Resources.localesFromStrings(NativeBreakIterator.getAvailableLocalesImpl());
}
public static BreakIterator getCharacterInstance() {
diff --git a/icu/src/main/java/com/ibm/icu4jni/text/Collator.java b/icu/src/main/java/com/ibm/icu4jni/text/Collator.java
index 4a7e1bf..0963b81 100644
--- a/icu/src/main/java/com/ibm/icu4jni/text/Collator.java
+++ b/icu/src/main/java/com/ibm/icu4jni/text/Collator.java
@@ -10,8 +10,9 @@
package com.ibm.icu4jni.text;
-import java.util.Locale;
import com.ibm.icu4jni.text.RuleBasedCollator;
+import com.ibm.icu4jni.util.Resources;
+import java.util.Locale;
/**
* Abstract class handling locale specific collation via JNI and ICU.
@@ -394,38 +395,7 @@ public abstract class Collator implements Cloneable
*/
public abstract int hashCode();
- // BEGIN android-added
public static Locale[] getAvailableLocales() {
-
- String[] locales = NativeCollation.getAvailableLocalesImpl();
-
- Locale[] result = new Locale[locales.length];
-
- String locale;
-
- int index, index2;
-
- for(int i = 0; i < locales.length; i++) {
- locale = locales[i];
-
- index = locale.indexOf('_');
- index2 = locale.lastIndexOf('_');
-
- if(index == -1) {
- result[i] = new Locale(locales[i]);
- } else if(index == 2 && index == index2) {
- result[i] = new Locale(
- locale.substring(0,2),
- locale.substring(3,5));
- } else if(index == 2 && index2 > index) {
- result[i] = new Locale(
- locale.substring(0,index),
- locale.substring(index + 1,index2),
- locale.substring(index2 + 1));
- }
- }
-
- return result;
+ return Resources.localesFromStrings(NativeCollation.getAvailableLocalesImpl());
}
- // END android-added
}
diff --git a/icu/src/main/java/com/ibm/icu4jni/util/Resources.java b/icu/src/main/java/com/ibm/icu4jni/util/Resources.java
index cbad9a5..66bfdfb 100644
--- a/icu/src/main/java/com/ibm/icu4jni/util/Resources.java
+++ b/icu/src/main/java/com/ibm/icu4jni/util/Resources.java
@@ -285,6 +285,35 @@ public class Resources {
return result;
}
+ /**
+ * Returns the appropriate {@code Locale} given a {@code String} of the form returned
+ * by {@code toString}. This is very lenient, and doesn't care what's between the underscores:
+ * this method can parse strings that {@code Locale.toString} won't produce.
+ * Used to remove duplication.
+ */
+ public static Locale localeFromString(String localeName) {
+ int first = localeName.indexOf('_');
+ int second = localeName.indexOf('_', first + 1);
+ if (first == -1) {
+ // Language only ("ja").
+ return new Locale(localeName);
+ } else if (second == -1) {
+ // Language and country ("ja_JP").
+ return new Locale(localeName.substring(0, first), localeName.substring(first + 1));
+ } else {
+ // Language and country and variant ("ja_JP_TRADITIONAL").
+ return new Locale(localeName.substring(0, first), localeName.substring(first + 1, second), localeName.substring(second + 1));
+ }
+ }
+
+ public static Locale[] localesFromStrings(String[] localeNames) {
+ Locale[] result = new Locale[localeNames.length];
+ for (int i = 0; i < result.length; ++i) {
+ result[i] = localeFromString(localeNames[i]);
+ }
+ return result;
+ }
+
// --- Native methods accessing ICU's database ----------------------------
public static native String getDisplayCountryNative(String countryCode, String locale);