Author: julien_viet
Date: 2010-09-30 11:29:38 -0400 (Thu, 30 Sep 2010)
New Revision: 3227
Modified:
kernel/branches/2.2.4-GA-perf/exo.kernel.commons/src/main/java/org/exoplatform/commons/utils/MapResourceBundle.java
Log:
TESTFR-11: Intern strings in MapResourceBundle
Modified:
kernel/branches/2.2.4-GA-perf/exo.kernel.commons/src/main/java/org/exoplatform/commons/utils/MapResourceBundle.java
===================================================================
---
kernel/branches/2.2.4-GA-perf/exo.kernel.commons/src/main/java/org/exoplatform/commons/utils/MapResourceBundle.java 2010-09-30
14:58:48 UTC (rev 3226)
+++
kernel/branches/2.2.4-GA-perf/exo.kernel.commons/src/main/java/org/exoplatform/commons/utils/MapResourceBundle.java 2010-09-30
15:29:38 UTC (rev 3227)
@@ -25,8 +25,6 @@
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
-import java.util.Set;
-import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -36,41 +34,43 @@
public class MapResourceBundle extends ResourceBundle implements Serializable
{
- private final static String REGEXP = "#\\{.*\\}";
+ private final static Pattern PATTERN = Pattern.compile("#\\{.*\\}");
- private Map props = new HashMap();
+ private Map<String, String> props;
private Locale locale;
public MapResourceBundle(Locale l)
{
this.locale = l;
+ this.props = new HashMap<String, String>();
}
public MapResourceBundle(ResourceBundle rB, Locale l)
{
+ Map<String, String> props = new HashMap<String, String>();
+ doMerge(props, rB);
+
+ //
this.locale = l;
- initMap(rB);
+ this.props = props;
}
- private void initMap(ResourceBundle rB)
+ private static void doMerge(Map<String, String> props, ResourceBundle rB)
{
- Enumeration e = rB.getKeys();
+ Enumeration<String> e = rB.getKeys();
while (e.hasMoreElements())
{
- String s = (String)e.nextElement();
- try
+ String key = e.nextElement();
+ if (props.get(key) == null)
{
- if (props.get(s) == null)
+ Object o = rB.getObject(key);
+ if (o instanceof String)
{
- String[] newArray = rB.getStringArray(s);
- props.put(s, newArray);
+ String value = (String)o;
+ props.put(key.intern(), value.intern());
}
}
- catch (ClassCastException ex)
- {
- props.put(s, rB.getObject(s));
- }
}
}
@@ -79,9 +79,20 @@
return props.get(key);
}
- public Enumeration getKeys()
+ public Enumeration<String> getKeys()
{
- return new Vector(props.keySet()).elements();
+ final Iterator<String> i = props.keySet().iterator();
+ return new Enumeration<String>()
+ {
+ public boolean hasMoreElements()
+ {
+ return i.hasNext();
+ }
+ public String nextElement()
+ {
+ return i.next();
+ }
+ };
}
public Locale getLocale()
@@ -89,69 +100,63 @@
return this.locale;
}
- public void add(String key, Object value)
+ public void add(String key, Object o)
{
- props.put(key, value);
+ if (key != null && o instanceof String)
+ {
+ String value = (String)o;
+ props.put(key.intern(), value.intern());
+ }
}
public void remove(String key)
{
- props.remove(key);
+ if (key != null)
+ {
+ props.remove(key);
+ }
}
public void merge(ResourceBundle bundle)
{
- Enumeration e = bundle.getKeys();
- while (e.hasMoreElements())
- {
- String s = (String)e.nextElement();
- Object value = bundle.getObject(s);
- try
- {
- String[] newArray = bundle.getStringArray(s);
- if (props.get(s) == null)
- {
- props.put(s, newArray);
- }
- }
- catch (ClassCastException ex)
- {
- props.put(s, value);
- }
- }
+ doMerge(props, bundle);
}
public void resolveDependencies()
{
- Map tempMap = new HashMap();
- Set keys = props.keySet();
- Pattern pattern = Pattern.compile(REGEXP);
- for (Iterator iter = keys.iterator(); iter.hasNext();)
+ Map<String, String> tempMap = new HashMap<String ,String>();
+ for (String element : props.keySet())
{
- String element = (String)iter.next();
- String value = lookupKey(element, pattern);
- tempMap.put(element, value);
+ String value = lookupKey(element);
+ if (value != null)
+ {
+ tempMap.put(element.intern(), value.intern());
+ }
}
props = tempMap;
}
- private String lookupKey(String key, Pattern pattern)
+ private String lookupKey(String key)
{
- String s = (String)props.get(key);
+ String s = props.get(key);
if (s == null)
+ {
return key;
- Matcher matcher = pattern.matcher(s);
+ }
+
+ //
+ Matcher matcher = PATTERN.matcher(s);
if (matcher.find())
{
- return recursivedResolving(s, pattern);
+ return recursivedResolving(s);
}
return s;
}
- private String recursivedResolving(String key, Pattern pattern)
+ private String recursivedResolving(String key)
{
String resolved = key;
- StringBuffer sB = new StringBuffer();
+ StringBuilder sB = new StringBuilder();
while (resolved.indexOf("#{") != -1)
{
sB.setLength(0);
@@ -159,7 +164,7 @@
int lastIndex = resolved.indexOf('}', firstIndex);
String realKey = resolved.substring(firstIndex + 2, lastIndex);
sB.append(resolved.substring(0, firstIndex));
- sB.append(lookupKey(realKey, pattern));
+ sB.append(lookupKey(realKey));
sB.append(resolved.substring(lastIndex + 1));
resolved = sB.toString();
}
Show replies by date