info.informatica.util.locale
Class ResourceCache

java.lang.Object
  extended by info.informatica.util.locale.ResourceCache

public class ResourceCache
extends Object

Map associating PropertyResourceBundles and Locales.

Allows to access a ResourceBundle for a given locale multiple times, without having to reload the Bundle each time. Also tries to keep single copies of each Bundle (multiple locales may map to the same ResourceBundle object).

It can also be used to load the best bundle for a list of given locales: instead of just probing one Locale and returning the default one when not found, when getBundle() is invoked with the array argument it tries to find the best locale according the array of desired locales.

Author:
amengual at informatica dot info
See Also:
ResourceBundle

Method Summary
static ResourceCache createResourceCache(String baseclass)
          Create a ResourceCache for a base resource, with the default ClassLoader.
static ResourceCache createResourceCache(String baseclass, ClassLoader loader)
          Create a ResourceCache for a base resource, supplying a ClassLoader.
static ResourceCache createResourceCache(String baseclass, ClassLoader loader, Locale deflocale, ResourceBundle.Control control)
          Create a ResourceCache for a base resource, supplying a ClassLoader, a default locale and a Resource Control.
 ResourceBundle getBundle()
          Retrieves the Bundle associated to default locale.
 ResourceBundle getBundle(Enumeration<Locale> locales)
          Retrieves the most appropriate Bundle according to an Enumeration of desired locales.
 ResourceBundle getBundle(Locale loc)
          Retrieves the best Bundle associated to a given Locale.
 ResourceBundle getBundle(Locale[] loc)
          Retrieves the most appropriate Bundle according to an array of desired locales.
 Object getObject(String name)
           
 Object getObject(String name, Locale loc)
           
 String getString(String name)
           
 String getString(String name, Locale loc)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

createResourceCache

public static ResourceCache createResourceCache(String baseclass,
                                                ClassLoader loader,
                                                Locale deflocale,
                                                ResourceBundle.Control control)
Create a ResourceCache for a base resource, supplying a ClassLoader, a default locale and a Resource Control.

Parameters:
baseclass - the base resource class name.
loader - the class loader.
Returns:
the ResourceCache.
Throws:
MissingResourceException - if no resources can be found at the baseclass.

createResourceCache

public static ResourceCache createResourceCache(String baseclass,
                                                ClassLoader loader)
Create a ResourceCache for a base resource, supplying a ClassLoader.

Parameters:
baseclass - the base resource class name.
loader - the class loader.
Returns:
the ResourceCache.
Throws:
MissingResourceException - if no resources can be found at the baseclass.

createResourceCache

public static ResourceCache createResourceCache(String baseclass)
Create a ResourceCache for a base resource, with the default ClassLoader.

Parameters:
baseclass - the base resource class name.
Returns:
the ResourceCache.
Throws:
MissingResourceException - if no resources can be found at the baseclass.

getBundle

public ResourceBundle getBundle(Enumeration<Locale> locales)
Retrieves the most appropriate Bundle according to an Enumeration of desired locales.

If the list of locales is null, returns the default locale.

Parameters:
locales - the Enumeration of desired locales, in descending order of priority
Returns:
the most appropriate Bundle for the given locale list

getBundle

public ResourceBundle getBundle(Locale[] loc)
Retrieves the most appropriate Bundle according to an array of desired locales.

If the list of locales is null, returns the default locale.

Parameters:
loc - the array of desired locales, in descending order of priority
Returns:
the most appropriate Bundle for the given locale list

getBundle

public ResourceBundle getBundle(Locale loc)
Retrieves the best Bundle associated to a given Locale.

Parameters:
loc - the requested locale
Returns:
the most appropriate Bundle for the given locale

getBundle

public ResourceBundle getBundle()
Retrieves the Bundle associated to default locale.

Returns:
the most appropriate Bundle for the default locale

getObject

public Object getObject(String name)
                 throws LocaleNotFoundException
Parameters:
name - the key name
Returns:
resource Object for name, for the default locale
Throws:
LocaleNotFoundException

getString

public String getString(String name)
                 throws LocaleNotFoundException
Parameters:
name - the key name
Returns:
resource String for name, for the default locale
Throws:
LocaleNotFoundException

getObject

public Object getObject(String name,
                        Locale loc)
                 throws MissingResourceException
Parameters:
name - the key name
loc - the requested locale
Returns:
resource Object for name, for the given locale
Throws:
MissingResourceException

getString

public String getString(String name,
                        Locale loc)
                 throws MissingResourceException
Parameters:
name - the key name
loc - the requested locale
Returns:
resource String for name, for the requested locale
Throws:
MissingResourceException