]> granicus.if.org Git - icu/commitdiff
ICU-8769 Brought back ICUResourceBundle cache with improved cache logic.
authorYoshito Umaoka <y.umaoka@gmail.com>
Wed, 7 Sep 2011 04:45:20 +0000 (04:45 +0000)
committerYoshito Umaoka <y.umaoka@gmail.com>
Wed, 7 Sep 2011 04:45:20 +0000 (04:45 +0000)
X-SVN-Rev: 30627

icu4j/main/classes/core/src/com/ibm/icu/impl/ICUResourceBundle.java

index 50d915b71fcb1f8f780b66de6c113308b59a546a..f3ecee8081d29b8f03f23da298f42664e8171ec7 100644 (file)
@@ -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<BundleCacheData, ICUResourceBundle, BundleCacheData> {
+
+        /* (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() {