Class ResourceCache

java.lang.Object
io.sf.jclf.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.

When this class is used with non-modular JDKs, the property resources are assumed to use UTF-8 encoding.

See Also:
  • Method Details

    • createResourceCache

      @Deprecated public static ResourceCache createResourceCache(String baseclass, ClassLoader loader, Locale deflocale, ResourceBundle.Control control)
      Deprecated.
      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.
      deflocale - the default locale for the cache.
      control - the resource control.
      Returns:
      the ResourceCache.
      Throws:
      MissingResourceException - if no resources can be found at the baseclass.
    • createResourceCache

      public static ResourceCache createResourceCache(String baseclass, ClassLoader loader, Locale deflocale)
      Create a ResourceCache for a base resource, supplying a ClassLoader and a default locale.
      Parameters:
      baseclass - the base resource class name.
      loader - the class loader.
      deflocale - the default locale for the cache.
      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 MissingResourceException
      Parameters:
      name - the key name
      Returns:
      resource Object for name, for the default locale
      Throws:
      MissingResourceException - if no object for the given name can be found.
    • getString

      public String getString(String name) throws MissingResourceException
      Parameters:
      name - the key name
      Returns:
      resource String for name, for the default locale
      Throws:
      MissingResourceException - if no string for the given name can be found.
    • 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 - if no object for the given name can be found.
    • 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 - if no string for the given name can be found.