svn commit: r1100127 - /ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/ResourceBundleMapWrapper.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1100127 - /ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/ResourceBundleMapWrapper.java

jleroux@apache.org
Author: jleroux
Date: Fri May  6 08:17:17 2011
New Revision: 1100127

URL: http://svn.apache.org/viewvc?rev=1100127&view=rev
Log:
A patch from Philippe Mouawad "ResourceBundleMapWrapper memory usage improvement" https://issues.apache.org/jira/browse/OFBIZ-4072

Patch that creates the Map only when it is required

Modified:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/ResourceBundleMapWrapper.java

Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/ResourceBundleMapWrapper.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/ResourceBundleMapWrapper.java?rev=1100127&r1=1100126&r2=1100127&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/ResourceBundleMapWrapper.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/ResourceBundleMapWrapper.java Fri May  6 08:17:17 2011
@@ -20,7 +20,6 @@ package org.ofbiz.base.util.collections;
 
 import java.io.Serializable;
 import java.util.Collection;
-import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.MissingResourceException;
@@ -154,47 +153,74 @@ public class ResourceBundleMapWrapper im
     public static class InternalRbmWrapper implements Map<String, Object>, Serializable {
         protected ResourceBundle resourceBundle;
         protected Map<String, Object> topLevelMap;
-
+        private boolean isMapInitialized = false;
+        
         public InternalRbmWrapper(ResourceBundle resourceBundle) {
             if (resourceBundle == null) {
                 throw new IllegalArgumentException("Cannot create InternalRbmWrapper with a null ResourceBundle.");
             }
             this.resourceBundle = resourceBundle;
-            topLevelMap = new HashMap<String, Object>();
-            // NOTE: this does NOT return all keys, ie keys from parent ResourceBundles, so we keep the resourceBundle object to look at when the main Map doesn't have a certain value
+        }
+
+        /**
+         * Creates the topLevelMap only when it is required
+         */
+        private void createMapWhenNeeded() {
+            if (isMapInitialized) {
+                return;
+            }
+            // NOTE: this does NOT return all keys, ie keys from parent
+            // ResourceBundles, so we keep the resourceBundle object to look at
+            // when the main Map doesn't have a certain value
             if (resourceBundle != null) {
-                Enumeration<String> keyNum = resourceBundle.getKeys();
-                while (keyNum.hasMoreElements()) {
-                    String key = keyNum.nextElement();
-                    //resourceBundleMap.put(key, bundle.getObject(key));
+                Set<String> set = resourceBundle.keySet();
+                topLevelMap = new HashMap<String, Object>(set.size());
+                for (String key : set) {
                     Object value = resourceBundle.getObject(key);
                     topLevelMap.put(key, value);
                 }
+            } else {
+                topLevelMap = new HashMap<String, Object>(1);
             }
             topLevelMap.put("_RESOURCE_BUNDLE_", resourceBundle);
+            isMapInitialized = true;
         }
 
+
         /* (non-Javadoc)
          * @see java.util.Map#size()
          */
-        public int size() {
-            // this is an approximate size, won't include elements from parent bundles
-            return topLevelMap.size() - 1;
+        public int size() {            
+            if(isMapInitialized)
+            {
+                // this is an approximate size, won't include elements from parent bundles
+                return topLevelMap.size() -1;
+            }
+            else
+            {
+                return resourceBundle.keySet().size();                        
+            }
         }
 
         /* (non-Javadoc)
          * @see java.util.Map#isEmpty()
          */
         public boolean isEmpty() {
-            return topLevelMap.isEmpty();
+            if (isMapInitialized) {
+                return topLevelMap.isEmpty();
+            } else {
+                return resourceBundle.keySet().size() == 0;
+            }
         }
 
         /* (non-Javadoc)
          * @see java.util.Map#containsKey(java.lang.Object)
          */
         public boolean containsKey(Object arg0) {
-            if (topLevelMap.containsKey(arg0)) {
-                return true;
+            if (isMapInitialized) {
+                if (topLevelMap.containsKey(arg0)) {
+                    return true;
+                }
             } else {
                 try {
                     if (this.resourceBundle.getObject((String) arg0) != null) {
@@ -203,7 +229,8 @@ public class ResourceBundleMapWrapper im
                 } catch (NullPointerException e) {
                     // happens when arg0 is null
                 } catch (MissingResourceException e) {
-                    // nope, not found... nothing, will automatically return false below
+                    // nope, not found... nothing, will automatically return
+                    // false below
                 }
             }
             return false;
@@ -220,9 +247,14 @@ public class ResourceBundleMapWrapper im
          * @see java.util.Map#get(java.lang.Object)
          */
         public Object get(Object arg0) {
-            Object value = this.topLevelMap.get(arg0);
-            if (resourceBundle != null) {
-                if (value == null) {
+                Object value = null;
+                if(isMapInitialized)
+                {
+                        value = this.topLevelMap.get(arg0);
+                }
+                
+                if (resourceBundle != null) {
+                        if (value == null) {
                     try {
                         value = this.resourceBundle.getObject((String) arg0);
                     } catch (MissingResourceException mre) {
@@ -277,6 +309,7 @@ public class ResourceBundleMapWrapper im
          * @see java.util.Map#keySet()
          */
         public Set<String> keySet() {
+                createMapWhenNeeded();
             return this.topLevelMap.keySet();
         }
 
@@ -284,6 +317,7 @@ public class ResourceBundleMapWrapper im
          * @see java.util.Map#values()
          */
         public Collection<Object> values() {
+                createMapWhenNeeded();
             return this.topLevelMap.values();
         }
 
@@ -291,6 +325,7 @@ public class ResourceBundleMapWrapper im
          * @see java.util.Map#entrySet()
          */
         public Set<Map.Entry<String, Object>> entrySet() {
+                createMapWhenNeeded();
             return this.topLevelMap.entrySet();
         }