FileDocCategorySizeDatePackage
Resources.javaAPI DocAndroid 1.5 API18322Wed May 06 22:41:04 BST 2009com.ibm.icu4jni.util

Resources

public class Resources extends Object
Helper class that delivers ResourceBundle instances expected by Harmony, but with the data taken from ICU's database. This approach has a couple of advantages:
  1. We have less classes in the overall system, since we use different instances for different ResourceBundles.
  2. We don't have these classes that consists of monstrous static arrays with anymore.
  3. We have control over which values we load at which time or even cache for later use.
  4. There is only one central place left in the system where I18N data needs to be configured, namely ICU.
Since we're mimicking the original Harmony ResourceBundle structures, most of the Harmony code can stay the same. We basically just need to change the ResourceBundle instantiation. Only the special case of the Locale bundles needs some more tweaking, since we don't want to keep several hundred timezone names in memory.

Fields Summary
private static String[]
isoLanguages
Cache for ISO language names.
private static String[]
isoCountries
Cache for ISO country names.
private static String[]
availableLocales
Available locales cache.
private static String[]
availableTimezones
Available timezones cache.
private static final String
initialLocale
Name of default locale at the time this class was initialized.
private static String[]
defaultTimezoneNames
Names of time zones for the default locale.
Constructors Summary
Methods Summary
private static java.lang.String[][]createTimeZoneNamesFor(java.lang.String locale)
Creates array of time zone names for the given locale. This method takes about 2s to run on a 400mhz ARM11.


                              
         
        long start = System.currentTimeMillis();

        /*
         * The following code is optimized for fast native response (the time a
         * method call can be in native code is limited). It prepares an empty
         * array to keep native code from having to create new Java objects. It
         * also fill in the time zone IDs to speed things up a bit. There's one
         * array for each time zone name type. (standard/long, standard/short,
         * daylight/long, daylight/short) The native method that fetches these
         * strings is faster if it can do all entries of one type, before having
         * to change to the next type. That's why the array passed down to
         * native has 5 entries, each providing space for all time zone names of
         * one type. Likely this access to the fields is much faster in the
         * native code because there's less array access overhead.
         */
        String[][] arrayToFill = new String[5][];
        arrayToFill[0] = getKnownTimezones();
        arrayToFill[1] = new String[availableTimezones.length];
        arrayToFill[2] = new String[availableTimezones.length];
        arrayToFill[3] = new String[availableTimezones.length];
        arrayToFill[4] = new String[availableTimezones.length];

        /*
         * Fill in the zone names in native.
         */
        getTimeZonesNative(arrayToFill, locale);

        /*
         * Finally we need to reorder the entries so we get the expected result.
         */
        String[][] result = new String[availableTimezones.length][5];
        for (int i = 0; i < availableTimezones.length; i++) {
            result[i][0] = arrayToFill[0][i];
            result[i][1] = arrayToFill[1][i];
            result[i][2] = arrayToFill[2][i];
            result[i][3] = arrayToFill[3][i];
            result[i][4] = arrayToFill[4][i];
        }

        Logger.getLogger(Resources.class.getSimpleName()).info(
                "Loaded time zone names for " + locale + " in "
                + (System.currentTimeMillis() - start) + "ms.");

        return result;
    
public static java.lang.String[]getAvailableLocales()
Returns an array of names of locales that are available in the system, fetched either from ICU's database or from our memory cache.

return
The array.

        if (availableLocales == null) {
            availableLocales = getAvailableLocalesNative();
        }

        return availableLocales;
    
private static native java.lang.String[]getAvailableLocalesNative()

private static native java.lang.Object[][]getContentImpl(java.lang.String locale, boolean needsTimeZones)

private static native java.lang.StringgetCurrencyCodeNative(java.lang.String locale)

private static native java.lang.StringgetCurrencySymbolNative(java.lang.String locale, java.lang.String currencyCode)

private static java.lang.StringgetDefaultLocaleName()
Gets the name of the default locale.

        return java.util.Locale.getDefault().toString();
    
private static native java.lang.StringgetDisplayCountryNative(java.lang.String countryCode, java.lang.String locale)

private static native java.lang.StringgetDisplayLanguageNative(java.lang.String languageCode, java.lang.String locale)

public static java.lang.StringgetDisplayTimeZone(java.lang.String id, boolean isDST, int style, java.lang.String locale)
Returns the display name for the given time zone using the given locale.

param
id The time zone ID, for example "Europe/Berlin"
param
isDST Indicates whether daylight savings is in use
param
style The style, 0 for long, 1 for short
param
locale The locale name, for example "en_US".
return
The desired display name

        return getDisplayTimeZoneNative(id, isDST, style, locale);
    
private static native java.lang.StringgetDisplayTimeZoneNative(java.lang.String id, boolean isDST, int style, java.lang.String locale)

public static java.lang.String[][]getDisplayTimeZones(java.lang.String locale)
Returns the display names for all given timezones using the given locale.

return
An array of time zone strings. Each row represents one time zone. The first columns holds the ID of the time zone, for example "Europe/Berlin". The other columns then hold for each row the four time zone names with and without daylight savings and in long and short format. It's exactly the array layout required by the TomeZone class.

        // Note: Defer loading DefaultTimeZones as long as possible.

        String defaultLocaleName = getDefaultLocaleName();
        if (locale == null) {
            locale = defaultLocaleName;
        }

        // If locale == default and the default locale hasn't changed since
        // DefaultTimeZones loaded, return the cached names.
        // TODO: We should force a reboot if the default locale changes.
        if (defaultLocaleName.equals(locale)
                && initialLocale.equals(defaultLocaleName)) {
            if (defaultTimezoneNames == null) {
                defaultTimezoneNames = createTimeZoneNamesFor(locale);
            }
            return defaultTimezoneNames;
        }
        
        return createTimeZoneNamesFor(locale);
    
private static native java.lang.StringgetDisplayVariantNative(java.lang.String variantCode, java.lang.String locale)

private static native intgetFractionDigitsNative(java.lang.String currencyCode)

private static native java.lang.StringgetISO3CountryNative(java.lang.String locale)

private static native java.lang.StringgetISO3LanguageNative(java.lang.String locale)

public static java.lang.String[]getISOCountries()
Returns an array of ISO country names (two-letter codes), fetched either from ICU's database or from our memory cache.

return
The array.

        if (isoCountries == null) {
            isoCountries = getISOCountriesNative();
        }

        return isoCountries;
    
private static native java.lang.String[]getISOCountriesNative()

public static java.lang.String[]getISOLanguages()
Returns an array of ISO language names (two-letter codes), fetched either from ICU's database or from our memory cache.

return
The array.

        if (isoLanguages == null) {
            isoLanguages = getISOLanguagesNative();
        }

        return isoLanguages;
    
private static native java.lang.String[]getISOLanguagesNative()

public static java.util.ResourceBundlegetInstance(java.lang.String bundleName, java.lang.String locale)
Creates ResourceBundle instance and fills it with ICU data.

param
bundleName The name of the requested Harmony resource bundle, excluding the package name.
param
locale The locale to use for the resources. A null value denotes the default locale as configured in Java.
return
The new ResourceBundle, or null, if no ResourceBundle was created.


                                                                                              
           
        if (locale == null) {
            locale = java.util.Locale.getDefault().toString();
        }

        if (bundleName.startsWith("Locale")) {
            return new Locale(locale);
        } else if (bundleName.startsWith("Country")) {
            return new Country(locale);
        } else if (bundleName.startsWith("Currency")) {
            return new Currency(locale);
        } else if (bundleName.startsWith("Language")) {
            return new Language(locale);
        } else if (bundleName.startsWith("Variant")) {
            return new Variant(locale);
        } else if (bundleName.equals("ISO3Countries")) {
            return new ISO3Countries();
        } else if (bundleName.equals("ISO3Languages")) {
            return new ISO3Languages();
        } else if (bundleName.equals("ISO4CurrenciesToDigits")) {
            return new ISO4CurrenciesToDigits();
        } else if (bundleName.equals("ISO4Currencies")) {
            return new ISO4Currencies();
        }

        return null;
    
public static java.lang.String[]getKnownTimezones()
Returns an array of names of timezones that are available in the system, fetched either from the TimeZone class or from our memory cache.

return
The array.

        // TODO Drop the Linux ZoneInfo stuff in favor of ICU.
        if (availableTimezones == null) {
            availableTimezones = TimeZone.getAvailableIDs();
        }

        return availableTimezones;
    
private static native voidgetTimeZonesNative(java.lang.String[][] arrayToFill, java.lang.String locale)