From 3959df3834545961bd9ccbde5c08c499c33e0da2 Mon Sep 17 00:00:00 2001 From: Yoshito Umaoka Date: Wed, 7 Sep 2011 04:45:20 +0000 Subject: [PATCH] ICU-8769 Brought back ICUResourceBundle cache with improved cache logic. X-SVN-Rev: 30627 --- .../com/ibm/icu/impl/ICUResourceBundle.java | 80 +++++++++++++++++-- 1 file changed, 72 insertions(+), 8 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 50d915b71fc..f3ecee8081d 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 @@ -994,7 +994,75 @@ public class ICUResourceBundle extends UResourceBundle { */ public static final int ARRAY16 = 9; - /** + /* + * Bundle cache stuff + */ + + /** + * Place holder in the bundle cache indicating that the requested bundle is not available + */ + private static final ICUResourceBundle NULL_BUNDLE = + new ICUResourceBundle(null, null, null, 0, null) { + public int hashCode() { + return 0; + } + public boolean equals(Object rhs) { + return this == rhs; + } + }; + + /** + * An object storing information required for creating a bundle, used by + * the bundle cache. + */ + private static class BundleCacheData { + ClassLoader root; + String baseName; + String localeID; + + BundleCacheData(ClassLoader root, String baseName, String localeID) { + this.root = root; + this.baseName = baseName; + this.localeID = localeID; + } + + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!(obj instanceof BundleCacheData)) { + return false; + } + BundleCacheData data = (BundleCacheData)obj; + return this.root.equals(data.root) && + this.baseName.equals(data.baseName) && + this.localeID.equals(data.localeID); + } + + public int hashCode() { + return root.hashCode() ^ baseName.hashCode() ^ localeID.hashCode(); + } + } + + private static class BundleCache extends SoftCache { + + /* (non-Javadoc) + * @see com.ibm.icu.impl.CacheBase#createInstance(java.lang.Object, java.lang.Object) + */ + @Override + protected ICUResourceBundle createInstance(BundleCacheData key, BundleCacheData data) { + String resolvedName = getFullName(data.baseName, data.localeID); + ICUResourceBundleReader reader = ICUResourceBundleReader.getReader(resolvedName, data.root); + if (reader == null) { + return NULL_BUNDLE; + } + return ICUResourceBundle.getBundle(reader, data.baseName, data.localeID, data.root); + } + } + + private static final BundleCache BUNDLE_CACHE = new BundleCache(); + + /** * Create a bundle using a reader. * @param baseName The name for the bundle. * @param localeID The locale identification. @@ -1002,13 +1070,9 @@ public class ICUResourceBundle extends UResourceBundle { * @return the new bundle */ public static ICUResourceBundle createBundle(String baseName, String localeID, ClassLoader root) { - String resolvedName = getFullName(baseName, localeID); - ICUResourceBundleReader reader = ICUResourceBundleReader.getReader(resolvedName, root); - if (reader == null) { - // could not open the .res file - return null; - } - return getBundle(reader, baseName, localeID, root); + BundleCacheData bundleKey = new BundleCacheData(root, baseName, localeID); + ICUResourceBundle b = BUNDLE_CACHE.getInstance(bundleKey, bundleKey); + return b == NULL_BUNDLE ? null : b; } protected String getLocaleID() { -- 2.40.0