ICU.java revision e0e567287e4392bebc5f5826b8ef3b1bd8ca166e
1/*
2 * Copyright (C) 2008 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
17package libcore.icu;
18
19import java.util.Locale;
20
21/**
22 * Makes ICU data accessible to Java.
23 */
24public final class ICU {
25    /**
26     * Cache for ISO language names.
27     */
28    private static String[] isoLanguages;
29
30    /**
31     * Cache for ISO country names.
32     */
33    private static String[] isoCountries;
34
35    /**
36     * Returns an array of ISO language names (two-letter codes), fetched either
37     * from ICU's database or from our memory cache.
38     *
39     * @return The array.
40     */
41    public static String[] getISOLanguages() {
42        if (isoLanguages == null) {
43            isoLanguages = getISOLanguagesNative();
44        }
45        return isoLanguages.clone();
46    }
47
48    /**
49     * Returns an array of ISO country names (two-letter codes), fetched either
50     * from ICU's database or from our memory cache.
51     *
52     * @return The array.
53     */
54    public static String[] getISOCountries() {
55        if (isoCountries == null) {
56            isoCountries = getISOCountriesNative();
57        }
58        return isoCountries.clone();
59    }
60
61    /**
62     * Returns the appropriate {@code Locale} given a {@code String} of the form returned
63     * by {@code toString}. This is very lenient, and doesn't care what's between the underscores:
64     * this method can parse strings that {@code Locale.toString} won't produce.
65     * Used to remove duplication.
66     */
67    public static Locale localeFromString(String localeName) {
68        int first = localeName.indexOf('_');
69        int second = localeName.indexOf('_', first + 1);
70        if (first == -1) {
71            // Language only ("ja").
72            return new Locale(localeName);
73        } else if (second == -1) {
74            // Language and country ("ja_JP").
75            return new Locale(localeName.substring(0, first), localeName.substring(first + 1));
76        } else {
77            // Language and country and variant ("ja_JP_TRADITIONAL").
78            return new Locale(localeName.substring(0, first), localeName.substring(first + 1, second), localeName.substring(second + 1));
79        }
80    }
81
82    public static Locale[] localesFromStrings(String[] localeNames) {
83        Locale[] result = new Locale[localeNames.length];
84        for (int i = 0; i < result.length; ++i) {
85            result[i] = localeFromString(localeNames[i]);
86        }
87        return result;
88    }
89
90    private static Locale[] availableLocalesCache;
91    public static Locale[] getAvailableLocales() {
92        if (availableLocalesCache == null) {
93            availableLocalesCache = localesFromStrings(getAvailableLocalesNative());
94        }
95        return availableLocalesCache.clone();
96    }
97
98    public static Locale[] getAvailableBreakIteratorLocales() {
99        return localesFromStrings(getAvailableBreakIteratorLocalesNative());
100    }
101
102    public static Locale[] getAvailableCalendarLocales() {
103        return localesFromStrings(getAvailableCalendarLocalesNative());
104    }
105
106    public static Locale[] getAvailableCollatorLocales() {
107        return localesFromStrings(getAvailableCollatorLocalesNative());
108    }
109
110    public static Locale[] getAvailableDateFormatLocales() {
111        return localesFromStrings(getAvailableDateFormatLocalesNative());
112    }
113
114    public static Locale[] getAvailableDateFormatSymbolsLocales() {
115        return getAvailableDateFormatLocales();
116    }
117
118    public static Locale[] getAvailableDecimalFormatSymbolsLocales() {
119        return getAvailableNumberFormatLocales();
120    }
121
122    public static Locale[] getAvailableNumberFormatLocales() {
123        return localesFromStrings(getAvailableNumberFormatLocalesNative());
124    }
125
126    /**
127     * Returns the ICU version in use. This is "4.4" for gingerbread, for example.
128     */
129    public static native String getIcuVersion();
130
131    /**
132     * Returns the Unicode version our ICU supports. This is "5.2" for gingerbread, for example.
133     */
134    public static native String getUnicodeVersion();
135
136    // --- Case mapping.
137
138    public static native String toLowerCase(String s, String localeName);
139    public static native String toUpperCase(String s, String localeName);
140
141    // --- Native methods accessing ICU's database.
142
143    private static native String[] getAvailableBreakIteratorLocalesNative();
144    private static native String[] getAvailableCalendarLocalesNative();
145    private static native String[] getAvailableCollatorLocalesNative();
146    private static native String[] getAvailableDateFormatLocalesNative();
147    private static native String[] getAvailableLocalesNative();
148    private static native String[] getAvailableNumberFormatLocalesNative();
149
150    public static native String getCurrencyCodeNative(String locale);
151    public static native int getCurrencyFractionDigitsNative(String currencyCode);
152    public static native String getCurrencySymbolNative(String locale, String currencyCode);
153
154    public static native String getDisplayCountryNative(String countryCode, String locale);
155    public static native String getDisplayLanguageNative(String languageCode, String locale);
156    public static native String getDisplayVariantNative(String variantCode, String locale);
157
158    public static native String getISO3CountryNative(String locale);
159    public static native String getISO3LanguageNative(String locale);
160
161    private static native String[] getISOLanguagesNative();
162    private static native String[] getISOCountriesNative();
163
164    static native boolean initLocaleDataImpl(String locale, LocaleData result);
165}
166