cache ResourceBundles, approx 8% perf improvement

This commit is contained in:
Lars Kühne 2002-11-10 21:58:34 +00:00
parent 10bae67ca1
commit 7e083cf6cb
1 changed files with 27 additions and 3 deletions

View File

@ -25,6 +25,8 @@ package com.puppycrawl.tools.checkstyle.api;
import java.util.ResourceBundle;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.Map;
import java.util.HashMap;
import java.text.MessageFormat;
/**
@ -33,14 +35,22 @@ import java.text.MessageFormat;
* java.text.MessageFormat.
*
* @author <a href="mailto:checkstyle@puppycrawl.com">Oliver Burn</a>
* @author lkuehne
* @version 1.0
*/
public class LocalizedMessage
public final class LocalizedMessage
implements Comparable
{
/** the locale to localise messages to **/
private static Locale sLocale = Locale.getDefault();
/**
* A cache that maps bundle names to RessourceBundles.
* Avoids repetitive calls to ResourceBundle.getBundle().
* TODO: Clear before method termination.
*/
private static Map sBundleCache = new HashMap();
/** the line number **/
private final int mLineNo;
/** the column number **/
@ -102,8 +112,7 @@ public class LocalizedMessage
// Configuration object. This is because the class loader in the
// Configuration is specified by the user for resolving custom
// classes.
final ResourceBundle bundle =
ResourceBundle.getBundle(mBundle, sLocale);
final ResourceBundle bundle = getBundle(mBundle);
final String pattern = bundle.getString(mKey);
return MessageFormat.format(pattern, mArgs);
}
@ -115,6 +124,21 @@ public class LocalizedMessage
}
}
/**
* Find a ResourceBundle for a given bundle name.
* @param aBundleName the bundle name
* @return a ResourceBundle
*/
private static ResourceBundle getBundle(String aBundleName)
{
ResourceBundle bundle = (ResourceBundle) sBundleCache.get(aBundleName);
if (bundle == null) {
bundle = ResourceBundle.getBundle(aBundleName, sLocale);
sBundleCache.put(aBundleName, bundle);
}
return bundle;
}
/** @return the line number **/
public int getLineNo()
{