From 1824b6f66cce4b1094e892212780e78f72ae3522 Mon Sep 17 00:00:00 2001 From: Markus Scherer Date: Wed, 18 May 2016 06:59:08 +0000 Subject: [PATCH] ICU-12551 replace state-changing ICUResourceBundle.loadingStatus with isRoot() and OpenType.LOCALE_ONLY X-SVN-Rev: 38750 --- .../com/ibm/icu/impl/ICUResourceBundle.java | 64 ++++++--------- .../ibm/icu/impl/ResourceBundleWrapper.java | 11 +-- .../core/src/com/ibm/icu/util/LocaleData.java | 6 +- .../src/com/ibm/icu/util/UResourceBundle.java | 12 --- .../impl/ICUCurrencyDisplayInfoProvider.java | 24 +++--- .../dev/test/util/ICUResourceBundleTest.java | 77 ------------------- 6 files changed, 42 insertions(+), 152 deletions(-) diff --git a/icu4j/main/classes/core/src/com/ibm/icu/impl/ICUResourceBundle.java b/icu4j/main/classes/core/src/com/ibm/icu/impl/ICUResourceBundle.java index 0e2b94392ec..635e9841641 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/impl/ICUResourceBundle.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/impl/ICUResourceBundle.java @@ -46,35 +46,6 @@ public class ICUResourceBundle extends UResourceBundle { */ protected static final String INSTALLED_LOCALES = "InstalledLocales"; - public static final int FROM_FALLBACK = 1, FROM_ROOT = 2, FROM_DEFAULT = 3, FROM_LOCALE = 4; - - private int loadingStatus = -1; - - public void setLoadingStatus(int newStatus) { - loadingStatus = newStatus; - } - /** - * Returns the loading status of a particular resource. - * - * @return FROM_FALLBACK if the resource is fetched from fallback bundle - * FROM_ROOT if the resource is fetched from root bundle. - * FROM_DEFAULT if the resource is fetched from the default locale. - */ - public int getLoadingStatus() { - return loadingStatus; - } - - public void setLoadingStatus(String requestedLocale){ - String locale = getLocaleID(); - if(locale.equals("root")) { - setLoadingStatus(FROM_ROOT); - } else if(locale.equals(requestedLocale)) { - setLoadingStatus(FROM_LOCALE); - } else { - setLoadingStatus(FROM_FALLBACK); - } - } - /** * Fields for a whole bundle, rather than any specific resource in the bundle. * Corresponds roughly to ICU4C/source/common/uresimp.h struct UResourceDataEntry. @@ -872,7 +843,6 @@ public class ICUResourceBundle extends UResourceBundle { } if (depth == keys.length) { // We found it. - sub.setLoadingStatus(((ICUResourceBundle)requested).getLocaleID()); return sub; } base = sub; @@ -1109,6 +1079,17 @@ public class ICUResourceBundle extends UResourceBundle { * such as case mappings, collation, and segmentation (BreakIterator). */ LOCALE_ROOT, + /** + * Open a resource bundle for the locale; + * if there is not even a base language bundle, then fail; + * never fall back to the default locale nor to the root locale. + * + *

This is used when fallback to another language is not desired + * and the root locale is not generally useful. + * For example, {@link com.ibm.icu.util.LocaleData#setNoSubstitute(boolean)} + * or currency display names for {@link com.ibm.icu.text.LocaleDisplayNames}. + */ + LOCALE_ONLY, /** * Open a resource bundle for the exact bundle name as requested; * no fallbacks, do not load parent bundles. @@ -1205,23 +1186,18 @@ public class ICUResourceBundle extends UResourceBundle { if(b == null){ int i = localeName.lastIndexOf('_'); if (i != -1) { + // Chop off the last underscore and the subtag after that. String temp = localeName.substring(0, i); b = (ICUResourceBundle)instantiateBundle(baseName, temp, root, openType); - if(b!=null && b.getULocale().getName().equals(temp)){ - b.setLoadingStatus(ICUResourceBundle.FROM_FALLBACK); - } }else{ + // No underscore, only a base language subtag. if(openType == OpenType.LOCALE_DEFAULT_ROOT && !defaultLocale.getLanguage().equals(localeName)) { + // Go to the default locale before root. b = (ICUResourceBundle)instantiateBundle(baseName, defaultID, root, openType); - if(b!=null){ - b.setLoadingStatus(ICUResourceBundle.FROM_DEFAULT); - } - }else if(rootLocale.length()!=0){ + } else if(openType != OpenType.LOCALE_ONLY && !rootLocale.isEmpty()) { + // Ultimately go to root. b = ICUResourceBundle.createBundle(baseName, rootLocale, root); - if(b!=null){ - b.setLoadingStatus(ICUResourceBundle.FROM_ROOT); - } } } }else{ @@ -1263,7 +1239,6 @@ public class ICUResourceBundle extends UResourceBundle { + aKey, this.getClass().getName(), aKey); } } - obj.setLoadingStatus(((ICUResourceBundle)requested).getLocaleID()); return obj; } @@ -1337,6 +1312,13 @@ public class ICUResourceBundle extends UResourceBundle { return wholeBundle.ulocale; } + /** + * Returns true if this is the root bundle, or an item in the root bundle. + */ + public boolean isRoot() { + return wholeBundle.localeID.isEmpty() || wholeBundle.localeID.equals("root"); + } + public UResourceBundle getParent() { return (UResourceBundle) parent; } diff --git a/icu4j/main/classes/core/src/com/ibm/icu/impl/ResourceBundleWrapper.java b/icu4j/main/classes/core/src/com/ibm/icu/impl/ResourceBundleWrapper.java index 3ec4b8c531d..3c4419ad100 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/impl/ResourceBundleWrapper.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/impl/ResourceBundleWrapper.java @@ -1,7 +1,7 @@ /* ****************************************************************************** -* Copyright (C) 2004-2015, International Business Machines Corporation and * -* others. All Rights Reserved. * +* Copyright (C) 2004-2016, International Business Machines Corporation and +* others. All Rights Reserved. ****************************************************************************** */ @@ -29,16 +29,11 @@ public class ResourceBundleWrapper extends UResourceBundle { private String localeID = null; private String baseName = null; private List keys = null; -// private int loadingStatus = -1; - + private ResourceBundleWrapper(ResourceBundle bundle){ this.bundle=bundle; } - protected void setLoadingStatus(int newStatus){ -// loadingStatus = newStatus; - } - protected Object handleGetObject(String aKey){ ResourceBundleWrapper current = this; Object obj = null; diff --git a/icu4j/main/classes/core/src/com/ibm/icu/util/LocaleData.java b/icu4j/main/classes/core/src/com/ibm/icu/util/LocaleData.java index 95b4e762d51..9780b73d410 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/util/LocaleData.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/util/LocaleData.java @@ -199,7 +199,7 @@ public final class LocaleData { final String aKey = exemplarSetTypes[extype]; // will throw an out-of-bounds exception ICUResourceBundle stringBundle = (ICUResourceBundle) bundle.get(aKey); - if ( noSubstitute && (stringBundle.getLoadingStatus() == ICUResourceBundle.FROM_ROOT) ) { + if (noSubstitute && !bundle.isRoot() && stringBundle.isRoot()) { return null; } String unicodeSetPattern = stringBundle.getString(); @@ -284,9 +284,9 @@ public final class LocaleData { // Only some of the quotation marks may be here. So we make sure that we do a multilevel fallback. ICUResourceBundle stringBundle = delimitersBundle.getWithFallback(DELIMITER_TYPES[type]); - if ( noSubstitute && (stringBundle.getLoadingStatus() == ICUResourceBundle.FROM_ROOT) ) + if (noSubstitute && !bundle.isRoot() && stringBundle.isRoot()) { return null; - + } return stringBundle.getString(); } diff --git a/icu4j/main/classes/core/src/com/ibm/icu/util/UResourceBundle.java b/icu4j/main/classes/core/src/com/ibm/icu/util/UResourceBundle.java index 4b7b3e29922..7456590ec53 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/util/UResourceBundle.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/util/UResourceBundle.java @@ -659,7 +659,6 @@ public abstract class UResourceBundle extends ResourceBundle { for (UResourceBundle res = this; res != null; res = res.getParent()) { UResourceBundle obj = res.handleGet(aKey, null, this); if (obj != null) { - ((ICUResourceBundle) obj).setLoadingStatus(getLocaleID()); return obj; } } @@ -705,7 +704,6 @@ public abstract class UResourceBundle extends ResourceBundle { + this.getClass().getName() + ", key " + getKey(), this.getClass().getName(), getKey()); } - ((ICUResourceBundle)obj).setLoadingStatus(getLocaleID()); return obj; } @@ -730,7 +728,6 @@ public abstract class UResourceBundle extends ResourceBundle { for (UResourceBundle res = this; res != null; res = res.getParent()) { UResourceBundle obj = res.handleGet(index, null, this); if (obj != null) { - ((ICUResourceBundle) obj).setLoadingStatus(getLocaleID()); return obj; } } @@ -1024,15 +1021,6 @@ public abstract class UResourceBundle extends ResourceBundle { return obj; } - /** - * This method is for setting the loading status of the resource. - * The status is analogous to the warning status in ICU4C. - * @internal - * @deprecated This API is ICU internal only. - */ - @Deprecated - protected abstract void setLoadingStatus(int newStatus); - /** * Is this a top-level resource, that is, a whole bundle? * @return true if this is a top-level resource diff --git a/icu4j/main/classes/currdata/src/com/ibm/icu/impl/ICUCurrencyDisplayInfoProvider.java b/icu4j/main/classes/currdata/src/com/ibm/icu/impl/ICUCurrencyDisplayInfoProvider.java index d2d46e63f83..af4eb1b9365 100644 --- a/icu4j/main/classes/currdata/src/com/ibm/icu/impl/ICUCurrencyDisplayInfoProvider.java +++ b/icu4j/main/classes/currdata/src/com/ibm/icu/impl/ICUCurrencyDisplayInfoProvider.java @@ -11,6 +11,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Map; +import java.util.MissingResourceException; import java.util.Set; import java.util.TreeMap; @@ -18,6 +19,7 @@ import com.ibm.icu.impl.CurrencyData.CurrencyDisplayInfo; import com.ibm.icu.impl.CurrencyData.CurrencyDisplayInfoProvider; import com.ibm.icu.impl.CurrencyData.CurrencyFormatInfo; import com.ibm.icu.impl.CurrencyData.CurrencySpacingInfo; +import com.ibm.icu.impl.ICUResourceBundle.OpenType; import com.ibm.icu.util.ULocale; import com.ibm.icu.util.UResourceBundle; @@ -26,11 +28,15 @@ public class ICUCurrencyDisplayInfoProvider implements CurrencyDisplayInfoProvid } public CurrencyDisplayInfo getInstance(ULocale locale, boolean withFallback) { - ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle.getBundleInstance( - ICUData.ICU_CURR_BASE_NAME, locale); - if (!withFallback) { - int status = rb.getLoadingStatus(); - if (status == ICUResourceBundle.FROM_DEFAULT || status == ICUResourceBundle.FROM_ROOT) { + ICUResourceBundle rb; + if (withFallback) { + rb = (ICUResourceBundle) ICUResourceBundle.getBundleInstance( + ICUData.ICU_CURR_BASE_NAME, locale, OpenType.LOCALE_DEFAULT_ROOT); + } else { + try { + rb = (ICUResourceBundle) ICUResourceBundle.getBundleInstance( + ICUData.ICU_CURR_BASE_NAME, locale, OpenType.LOCALE_ONLY); + } catch (MissingResourceException e) { return null; } } @@ -75,12 +81,8 @@ public class ICUCurrencyDisplayInfoProvider implements CurrencyDisplayInfoProvid if (currencies != null) { ICUResourceBundle result = currencies.findWithFallback(isoCode); if (result != null) { - if (!fallback) { - int status = result.getLoadingStatus(); - if (status == ICUResourceBundle.FROM_DEFAULT || - status == ICUResourceBundle.FROM_ROOT) { - return null; - } + if (!fallback && !rb.isRoot() && result.isRoot()) { + return null; } return result.getString(symbolName ? 0 : 1); } diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/util/ICUResourceBundleTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/util/ICUResourceBundleTest.java index 25499c7d511..05fce2ff69e 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/util/ICUResourceBundleTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/util/ICUResourceBundleTest.java @@ -829,83 +829,7 @@ public final class ICUResourceBundleTest extends TestFmwk { } } - private String getLSString(int status){ - switch(status){ - case ICUResourceBundle.FROM_FALLBACK: - return "FROM_FALLBACK"; - case ICUResourceBundle.FROM_DEFAULT: - return "FROM_DEFAULT"; - case ICUResourceBundle.FROM_ROOT: - return "FROM_ROOT"; - case ICUResourceBundle.FROM_LOCALE: - return "FROM_LOCALE"; - default: - return "UNKNOWN"; - } - } - - private void assertEqualLoadingStatus(String msg, int target, int result) { - if (result != target) { - errln(msg + " expected: "+ getLSString(target) - + " got: " + getLSString(result)); - } - } - - @SuppressWarnings("unused") - private void assertDefaultLoadingStatus(String msg, int result) { - assertEqualLoadingStatus(msg, ICUResourceBundle.FROM_DEFAULT, result); - } - - private void assertFallbackLoadingStatus(String msg, int result) { - assertEqualLoadingStatus(msg, ICUResourceBundle.FROM_FALLBACK, result); - } - - private void assertRootLoadingStatus(String msg, int result) { - assertEqualLoadingStatus(msg, ICUResourceBundle.FROM_ROOT, result); - } - - private void assertLocaleLoadingStatus(String msg, int result) { - assertEqualLoadingStatus(msg, ICUResourceBundle.FROM_LOCALE, result); - } - - public void TestLoadingStatus(){ - ICUResourceBundle bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, "yi_IL"); - assertFallbackLoadingStatus("base/yi_IL", bundle.getLoadingStatus()); - bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, "eo_DE"); - assertFallbackLoadingStatus("base/eo_DE", bundle.getLoadingStatus()); - - logln("Test to verify loading status of get(String)"); - bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_LANG_BASE_NAME, "zh_Hant_TW"); - ICUResourceBundle countries = (ICUResourceBundle) bundle.get("Languages"); - assertFallbackLoadingStatus("lang/Languages/zh_Hant_TW", countries.getLoadingStatus()); - - /* - UResourceBundle auxExemplar = bundle.get("AuxExemplarCharacters"); - status = auxExemplar.getLoadingStatus(); - if(status != UResourceBundle.FROM_ROOT){ - errln("Did not get the expected value for loading status. Expected "+ getLSString(UResourceBundle.FROM_ROOT) - + " Got: " + getLSString(status)); - } - */ - - logln("Test to verify root loading status of get()"); - bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, "te_IN"); - ICUResourceBundle ms = (ICUResourceBundle) bundle.get("layout"); - assertRootLoadingStatus("base/layout/te_IN", ms.getLoadingStatus()); - - logln("Test to verify loading status of getwithFallback"); - bundle = (ICUResourceBundle) UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", "sh_YU", testLoader); - ICUResourceBundle temp = (ICUResourceBundle) bundle.getWithFallback("a/a2"); - assertLocaleLoadingStatus("testdata/a/a2/sh_YU", temp.getLoadingStatus()); - - temp = bundle.getWithFallback("a/a1"); - assertFallbackLoadingStatus("testdata/a/a1/sh_YU", temp.getLoadingStatus()); - - temp = bundle.getWithFallback("a/a4"); - assertRootLoadingStatus("testdata/a/a4/sh_YU", temp.getLoadingStatus()); - } - public void TestCoverage(){ UResourceBundle bundle; bundle = UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME); @@ -926,7 +850,6 @@ public final class ICUResourceBundleTest extends TestFmwk { protected String getLocaleID() {return null;} protected String getBaseName() {return null;} protected UResourceBundle getParent() {return null;} - protected void setLoadingStatus(int newStatus) {} public Enumeration getKeys() {return null;} protected Object handleGetObject(String aKey) {return null;} }