kiwi.util
Class LocaleManager

java.lang.Object
  extended bykiwi.util.LocaleManager

public class LocaleManager
extends java.lang.Object

The Kiwi locale manager. This class retrieves resource bundles from one or more ResourceManagers, and provides convenience methods for formatting various types of data according to the rules of the current locale.

In the case of decimal-based values such as percentages and currency amounts, if the corresponding parse fails, this class will resort to parsing the string as a generic decimal value, throwing an exception if that also fails. This allows for the parsing of values that are formatted specifically or generically. For example, the currency value $49.55 will parse to the decimal value 49.55 whether the source string is "$49.55" or "49.55".

Author:
Mark Lindner, PING Software Group

Field Summary
private static java.text.Collator collator
           
private  java.text.NumberFormat currencyFormat
           
private  java.text.DateFormat[] dateFormat
           
private  java.text.DateFormatSymbols dateFormatSymbols
           
private  java.text.DateFormat[] dateTimeFormat
           
private  java.text.NumberFormat decimalFormat
           
static int DEFAULT_CURRENCY_DECIMALS
          The default number of decimal digits to retain when formatting currency values.
static int DEFAULT_NUMBER_DECIMALS
          The default number of decimal digits to retain when formatting numeric values.
static int DEFAULT_PERCENTAGE_DECIMALS
          The default number of decimal digits to retain when formatting percentage values.
private static LocaleManager defaultLocaleManager
           
private  java.text.NumberFormat integerFormat
           
private  int[] lengthTypes
           
private  java.text.NumberFormat percentFormat
           
private static java.text.ParsePosition pos
           
private  java.util.Vector resourceManagers
           
private  java.text.DateFormat[] timeFormat
           
 
Constructor Summary
LocaleManager()
          Construct a new LocaleManager.
LocaleManager(ResourceManager resourceManager)
          Construct a new LocaleManager.
 
Method Summary
 void addResourceManager(ResourceManager manager)
          Register a ResourceManager with the locale manager.
 java.lang.String formatCurrency(double value)
          Format a currency value according to the rules of the current locale, with DEFAULT_CURRENCY_DECIMALS decimal places retained, and grouping turned off.
 java.lang.String formatCurrency(double value, int decimals)
          Format a currency value according to the rules of the current locale, with the specified number of decimal places retained, and grouping turned off.
 java.lang.String formatCurrency(double value, int decimals, boolean grouping)
          Format a currency value according to the rules of the current locale, with the specified number of decimal places retained, and the specified grouping policy.
 java.lang.String formatCurrency(float value)
          Format a currency value according to the rules of the current locale, with DEFAULT_CURRENCY_DECIMALS decimal places retained, and grouping turned off.
 java.lang.String formatCurrency(float value, int decimals)
          Format a currency value according to the rules of the current locale, with the specified number of decimal places retained, and grouping turned off.
 java.lang.String formatCurrency(float value, int decimals, boolean grouping)
          Format a currency value according to the rules of the current locale, with the specified number of decimal places retained, and the specified grouping policy.
 java.lang.String formatDate(java.util.Calendar date)
          Format a date according to the rules of the current locale, with the default (medium) format length.
 java.lang.String formatDate(java.util.Date date)
          Format a date according to the rules of the current locale, with the default (medium) format length.
 java.lang.String formatDate(java.util.Date date, int type)
          Format a date according to the rules of the current locale, with the specified format length.
 java.lang.String formatDateTime(java.util.Calendar date)
          Format a date and time according to the rules of the current locale, with the default (medium) format length.
 java.lang.String formatDateTime(java.util.Date date)
          Format a date and time according to the rules of the current locale, with the default (medium) format length.
 java.lang.String formatDateTime(java.util.Date date, int type)
          Format a date and time according to the rules of the current locale, with the specified format length.
 java.lang.String formatDecimal(double value)
          Format a floating point value according to the rules of the current locale, with DEFAULT_NUMBER_DECIMALS decimal places retained, and grouping turned off.
 java.lang.String formatDecimal(double value, int decimals)
          Format a floating point value according to the rules of the current locale, with the specified number of decimal places retained, and grouping turned off.
 java.lang.String formatDecimal(double value, int decimals, boolean grouping)
          Format a floating point value according to the rules of the current locale, with the specified number of decimal places retained, and the specified grouping policy.
 java.lang.String formatDecimal(float value)
          Format a floating point value according to the rules of the current locale, with DEFAULT_NUMBER_DECIMALS decimal places retained, and grouping turned off.
 java.lang.String formatDecimal(float value, int decimals)
          Format a floating point value according to the rules of the current locale, with the specified number of decimal places retained, and grouping turned off.
 java.lang.String formatDecimal(float value, int decimals, boolean grouping)
          Format a floating point value according to the rules of the current locale, with the specified number of decimal places retained, and the specified grouping policy.
 java.lang.String formatInteger(int value)
          Format an integer value according to the rules of the current locale, with grouping turned off.
 java.lang.String formatInteger(int value, boolean grouping)
          Format an integer value according to the rules of the current locale, and the specified grouping policy.
 java.lang.String formatInteger(long value)
          Format an integer value according to the rules of the current locale, with grouping turned off.
 java.lang.String formatInteger(long value, boolean grouping)
          Format an integer value according to the rules of the current locale, and the specified grouping policy.
 java.lang.String formatPercentage(double value)
          Format a percentage value according to the rules of the current locale, with DEFAULT_PERCENTAGE_DECIMALS decimal places retained, and grouping turned off.
 java.lang.String formatPercentage(double value, int decimals)
          Format a percentage value according to the rules of the current locale, with the specified number of decimal places retained, and grouping turned off.
 java.lang.String formatPercentage(double value, int decimals, boolean grouping)
          Format a percentage value according to the rules of the current locale, with the specified number of decimal places retained, and the specified grouping policy.
 java.lang.String formatPercentage(float value)
          Format a percentage value according to the rules of the current locale, with DEFAULT_PERCENTAGE_DECIMALS decimal places retained, and grouping turned off.
 java.lang.String formatPercentage(float value, int decimals)
          Format a percentage value according to the rules of the current locale, with the specified number of decimal places retained, and grouping turned off.
 java.lang.String formatPercentage(float value, int decimals, boolean grouping)
          Format a percentage value according to the rules of the current locale, with the specified number of decimal places retained, and the specified grouping policy.
 java.lang.String formatTime(java.util.Calendar date)
          Format a time according to the rules of the current locale, with the default (medium) format length.
 java.lang.String formatTime(java.util.Date date)
          Format a time according to the rules of the current locale, with the default (medium) format length.
 java.lang.String formatTime(java.util.Date date, int type)
          Format a time according to the rules of the current locale, with the specified format length.
 java.text.Collator getCollator()
          Get an instance of the Collator object for the current locale.
 java.text.DateFormatSymbols getDateFormatSymbols()
          Get an instance of the DateFormatSymbols object for the current locale.
static LocaleManager getDefaultLocaleManager()
          Get a reference to the default LocaleManager.
 java.util.Locale getLocale()
          Get the default locale.
 LocaleData getLocaleData(java.lang.String name)
          Retrieve the named resource bundle.
static LocaleManager getLocaleManager()
          Deprecated. Use getDefaultLocaleManager() instead.
 double parseCurrency(java.lang.String s)
          Parse a currency value from a string using the rules of the current locale.
 java.util.Date parseDate(java.lang.String s)
          Parse a date value from a string using the rules of the current locale, with the default (medium) format length.
 java.util.Date parseDate(java.lang.String s, int type)
          Parse a date value from a string using the rules of the current locale, with the specified format length.
 java.util.Date parseDateTime(java.lang.String s)
          Parse a date and time value from a string using the rules of the current locale, with the default (medium) format length.
 java.util.Date parseDateTime(java.lang.String s, int type)
          Parse a date and time value from a string using the rules of the current locale, with the specified format length.
 double parseDecimal(java.lang.String s)
          Parse a numeric value from a string using the rules of the current locale.
 long parseInteger(java.lang.String s)
          Parse an integer value from a string using the rules of the current locale.
 double parsePercentage(java.lang.String s)
          Parse a currency value from a string using the rules of the current locale.
 java.util.Date parseTime(java.lang.String s)
          Parse a time value from a string using the rules of the current locale, with the default (medium) format length.
 java.util.Date parseTime(java.lang.String s, int type)
          Parse a time value from a string using the rules of the current locale, with the specified format length.
 void removeResourceManager(ResourceManager manager)
          Unregister a ResourceManager from the locale manager.
 void setLocale(java.util.Locale locale)
          Set the default locale.
private  void trapGarbage(java.lang.String s)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

defaultLocaleManager

private static LocaleManager defaultLocaleManager

collator

private static java.text.Collator collator

currencyFormat

private java.text.NumberFormat currencyFormat

percentFormat

private java.text.NumberFormat percentFormat

decimalFormat

private java.text.NumberFormat decimalFormat

integerFormat

private java.text.NumberFormat integerFormat

resourceManagers

private java.util.Vector resourceManagers

pos

private static java.text.ParsePosition pos

dateFormat

private java.text.DateFormat[] dateFormat

timeFormat

private java.text.DateFormat[] timeFormat

dateTimeFormat

private java.text.DateFormat[] dateTimeFormat

lengthTypes

private int[] lengthTypes

dateFormatSymbols

private java.text.DateFormatSymbols dateFormatSymbols

DEFAULT_CURRENCY_DECIMALS

public static final int DEFAULT_CURRENCY_DECIMALS
The default number of decimal digits to retain when formatting currency values.

See Also:
Constant Field Values

DEFAULT_PERCENTAGE_DECIMALS

public static final int DEFAULT_PERCENTAGE_DECIMALS
The default number of decimal digits to retain when formatting percentage values.

See Also:
Constant Field Values

DEFAULT_NUMBER_DECIMALS

public static final int DEFAULT_NUMBER_DECIMALS
The default number of decimal digits to retain when formatting numeric values.

See Also:
Constant Field Values
Constructor Detail

LocaleManager

public LocaleManager(ResourceManager resourceManager)
Construct a new LocaleManager.

Parameters:
resourceManager - The ResourceManager that will be used to load resource bundles.
Since:
Kiwi 1.3

LocaleManager

public LocaleManager()
Construct a new LocaleManager.

Since:
Kiwi 1.3
Method Detail

getLocale

public java.util.Locale getLocale()
Get the default locale.

Returns:
The current locale for this LocaleManager.
Since:
Kiwi 1.3

setLocale

public void setLocale(java.util.Locale locale)
Set the default locale. This method should be called in place of Locale.setDefault() (though it does call this method itself).

Parameters:
locale - The new locale.

getLocaleManager

public static LocaleManager getLocaleManager()
Deprecated. Use getDefaultLocaleManager() instead.

Get a reference to the default LocaleManager.


getDefaultLocaleManager

public static LocaleManager getDefaultLocaleManager()
Get a reference to the default LocaleManager.

Returns:
The default LocaleManager.
Since:
Kiwi 1.3

addResourceManager

public void addResourceManager(ResourceManager manager)
Register a ResourceManager with the locale manager. The locale manager searches through all registered resource managers when searching for a resource bundle. The Kiwi internal resource manager is always registered.

Parameters:
manager - The ResourceManager to register.

removeResourceManager

public void removeResourceManager(ResourceManager manager)
Unregister a ResourceManager from the locale manager. The Kiwi internal resource manager is always registered and cannot be removed.

Parameters:
manager - The ResourceManager to unregister.

getLocaleData

public LocaleData getLocaleData(java.lang.String name)
                         throws ResourceNotFoundException
Retrieve the named resource bundle. The locale manager queries all resource managers sequentially for the desired resource bundle, starting with the Kiwi internal resource manager, returning as soon as the bundle is found.

Returns:
The LocaleData object representing the specified resource bundle.
Throws:
java.util.ResourceNotFoundException - If the specified bundle could not be located by any of the registered resource managers.
ResourceNotFoundException

formatDate

public java.lang.String formatDate(java.util.Calendar date)
Format a date according to the rules of the current locale, with the default (medium) format length.

Parameters:
date - The date to format.
Returns:
A string representation of the value.

formatDate

public java.lang.String formatDate(java.util.Date date)
Format a date according to the rules of the current locale, with the default (medium) format length.

Parameters:
date - The date to format.
Returns:
A string representation of the value.

formatDate

public java.lang.String formatDate(java.util.Date date,
                                   int type)
Format a date according to the rules of the current locale, with the specified format length.

Parameters:
date - The date to format.
type - The format length; one of the symbolic constants SHORT, MEDIUM, or LONG.
Returns:
A string representation of the value.

formatTime

public java.lang.String formatTime(java.util.Calendar date)
Format a time according to the rules of the current locale, with the default (medium) format length.

Parameters:
date - The date to format.
Returns:
A string representation of the value.

formatTime

public java.lang.String formatTime(java.util.Date date)
Format a time according to the rules of the current locale, with the default (medium) format length.

Parameters:
date - The date to format.
Returns:
A string representation of the value.

formatTime

public java.lang.String formatTime(java.util.Date date,
                                   int type)
Format a time according to the rules of the current locale, with the specified format length.

Parameters:
date - The date to format.
type - The format length; one of the symbolic constants SHORT, MEDIUM, or LONG.
Returns:
A string representation of the value.

formatDateTime

public java.lang.String formatDateTime(java.util.Calendar date)
Format a date and time according to the rules of the current locale, with the default (medium) format length.

Parameters:
date - The date to format.
Returns:
A string representation of the value.

formatDateTime

public java.lang.String formatDateTime(java.util.Date date)
Format a date and time according to the rules of the current locale, with the default (medium) format length.

Parameters:
date - The date to format.
Returns:
A string representation of the value.

formatDateTime

public java.lang.String formatDateTime(java.util.Date date,
                                       int type)
Format a date and time according to the rules of the current locale, with the specified format length.

Parameters:
date - The date to format.
type - The format length; one of the symbolic constants SHORT, MEDIUM, or LONG.
Returns:
A string representation of the value.

parseDate

public java.util.Date parseDate(java.lang.String s)
                         throws java.text.ParseException
Parse a date value from a string using the rules of the current locale, with the default (medium) format length.

Parameters:
s - The string to parse.
Returns:
The resulting Date object.
Throws:
java.text.ParseException - If the value could not be parsed.

parseDate

public java.util.Date parseDate(java.lang.String s,
                                int type)
                         throws java.text.ParseException
Parse a date value from a string using the rules of the current locale, with the specified format length.

Parameters:
s - The string to parse.
type - The format length; one of the symbolic constants SHORT, MEDIUM, or LONG.
Returns:
The resulting Date object.
Throws:
java.text.ParseException - If the value could not be parsed.

parseTime

public java.util.Date parseTime(java.lang.String s)
                         throws java.text.ParseException
Parse a time value from a string using the rules of the current locale, with the default (medium) format length.

Parameters:
s - The string to parse.
Returns:
The resulting Date object.
Throws:
java.text.ParseException - If the value could not be parsed.

parseTime

public java.util.Date parseTime(java.lang.String s,
                                int type)
                         throws java.text.ParseException
Parse a time value from a string using the rules of the current locale, with the specified format length.

Parameters:
s - The string to parse.
type - The format length; one of the symbolic constants SHORT, MEDIUM, or LONG.
Returns:
The resulting Date object.
Throws:
java.text.ParseException - If the value could not be parsed.

parseDateTime

public java.util.Date parseDateTime(java.lang.String s)
                             throws java.text.ParseException
Parse a date and time value from a string using the rules of the current locale, with the default (medium) format length.

Parameters:
s - The string to parse.
Returns:
The resulting Date object.
Throws:
java.text.ParseException - If the value could not be parsed.

parseDateTime

public java.util.Date parseDateTime(java.lang.String s,
                                    int type)
                             throws java.text.ParseException
Parse a date and time value from a string using the rules of the current locale, with the specified format length.

Parameters:
s - The string to parse.
type - The format length; one of the symbolic constants SHORT, MEDIUM, or LONG.
Returns:
The resulting Date object.
Throws:
java.text.ParseException - If the value could not be parsed.

formatCurrency

public java.lang.String formatCurrency(double value)
Format a currency value according to the rules of the current locale, with DEFAULT_CURRENCY_DECIMALS decimal places retained, and grouping turned off.

Parameters:
value - The currency value to format.
Returns:
A string representation of the value.

formatCurrency

public java.lang.String formatCurrency(double value,
                                       int decimals)
Format a currency value according to the rules of the current locale, with the specified number of decimal places retained, and grouping turned off.

Parameters:
value - The currency value to format.
decimals - The number of decimal places to retain.
Returns:
A string representation of the value.

formatCurrency

public java.lang.String formatCurrency(double value,
                                       int decimals,
                                       boolean grouping)
Format a currency value according to the rules of the current locale, with the specified number of decimal places retained, and the specified grouping policy.

Parameters:
value - The currency value to format.
decimals - The number of decimal places to retain.
grouping - A flag specifying whether grouping should be used.
Returns:
A string representation of the value.

formatCurrency

public java.lang.String formatCurrency(float value)
Format a currency value according to the rules of the current locale, with DEFAULT_CURRENCY_DECIMALS decimal places retained, and grouping turned off.

Parameters:
value - The currency value to format.
Returns:
A string representation of the value.

formatCurrency

public java.lang.String formatCurrency(float value,
                                       int decimals)
Format a currency value according to the rules of the current locale, with the specified number of decimal places retained, and grouping turned off.

Parameters:
value - The currency value to format.
decimals - The number of decimal places to retain.
Returns:
A string representation of the value.

formatCurrency

public java.lang.String formatCurrency(float value,
                                       int decimals,
                                       boolean grouping)
Format a currency value according to the rules of the current locale, with the specified number of decimal places retained, and the specified grouping policy.

Parameters:
value - The currency value to format.
decimals - The number of decimal places to retain.
grouping - A flag specifying whether grouping should be used.
Returns:
A string representation of the value.

parseCurrency

public double parseCurrency(java.lang.String s)
                     throws java.text.ParseException
Parse a currency value from a string using the rules of the current locale. The input string may include grouping characters.

Parameters:
s - The string to parse.
Returns:
The resulting value.
Throws:
java.text.ParseException - If the value could not be parsed.

formatPercentage

public java.lang.String formatPercentage(double value)
Format a percentage value according to the rules of the current locale, with DEFAULT_PERCENTAGE_DECIMALS decimal places retained, and grouping turned off.

Parameters:
value - The percentage value to format.
Returns:
A string representation of the value.

formatPercentage

public java.lang.String formatPercentage(double value,
                                         int decimals)
Format a percentage value according to the rules of the current locale, with the specified number of decimal places retained, and grouping turned off.

Parameters:
value - The percentage value to format.
decimals - The number of decimal places to retain.
Returns:
A string representation of the value.

formatPercentage

public java.lang.String formatPercentage(double value,
                                         int decimals,
                                         boolean grouping)
Format a percentage value according to the rules of the current locale, with the specified number of decimal places retained, and the specified grouping policy.

Parameters:
value - The percentage value to format.
decimals - The number of decimal places to retain.
grouping - A flag specifying whether grouping should be used.
Returns:
A string representation of the value.

formatPercentage

public java.lang.String formatPercentage(float value)
Format a percentage value according to the rules of the current locale, with DEFAULT_PERCENTAGE_DECIMALS decimal places retained, and grouping turned off.

Parameters:
value - The percentage value to format.
Returns:
A string representation of the value.

formatPercentage

public java.lang.String formatPercentage(float value,
                                         int decimals)
Format a percentage value according to the rules of the current locale, with the specified number of decimal places retained, and grouping turned off.

Parameters:
value - The percentage value to format.
decimals - The number of decimal places to retain.
Returns:
A string representation of the value.

formatPercentage

public java.lang.String formatPercentage(float value,
                                         int decimals,
                                         boolean grouping)
Format a percentage value according to the rules of the current locale, with the specified number of decimal places retained, and the specified grouping policy.

Parameters:
value - The percentage value to format.
decimals - The number of decimal places to retain.
grouping - A flag specifying whether grouping should be used.
Returns:
A string representation of the value.

parsePercentage

public double parsePercentage(java.lang.String s)
                       throws java.text.ParseException
Parse a currency value from a string using the rules of the current locale. The input string may include grouping characters.

Parameters:
s - The string to parse.
Returns:
The resulting value.
Throws:
java.text.ParseException - If the value could not be parsed.

formatInteger

public java.lang.String formatInteger(int value)
Format an integer value according to the rules of the current locale, with grouping turned off.

Parameters:
value - The integer value to format.
Returns:
A string representation of the value.

formatInteger

public java.lang.String formatInteger(long value)
Format an integer value according to the rules of the current locale, with grouping turned off.

Parameters:
value - The integer value to format.
Returns:
A string representation of the value.

formatInteger

public java.lang.String formatInteger(int value,
                                      boolean grouping)
Format an integer value according to the rules of the current locale, and the specified grouping policy.

Parameters:
value - The integer value to format.
Returns:
A string representation of the value.

formatInteger

public java.lang.String formatInteger(long value,
                                      boolean grouping)
Format an integer value according to the rules of the current locale, and the specified grouping policy.

Parameters:
value - The integer value to format.
Returns:
A string representation of the value.

parseInteger

public long parseInteger(java.lang.String s)
                  throws java.text.ParseException
Parse an integer value from a string using the rules of the current locale. The input string may include grouping characters.

Parameters:
s - The string to parse.
Returns:
The resulting value.
Throws:
java.text.ParseException - If the value could not be parsed.

formatDecimal

public java.lang.String formatDecimal(double value)
Format a floating point value according to the rules of the current locale, with DEFAULT_NUMBER_DECIMALS decimal places retained, and grouping turned off.

Parameters:
value - The floating point value to format.
Returns:
A string representation of the value.

formatDecimal

public java.lang.String formatDecimal(double value,
                                      int decimals)
Format a floating point value according to the rules of the current locale, with the specified number of decimal places retained, and grouping turned off.

Parameters:
value - The floating point value to format.
decimals - The number of decimal places to retain.
Returns:
A string representation of the value.

formatDecimal

public java.lang.String formatDecimal(double value,
                                      int decimals,
                                      boolean grouping)
Format a floating point value according to the rules of the current locale, with the specified number of decimal places retained, and the specified grouping policy.

Parameters:
value - The floating point value to format.
decimals - The number of decimal places to retain.
grouping - A flag specifying whether grouping should be used.
Returns:
A string representation of the value.

formatDecimal

public java.lang.String formatDecimal(float value)
Format a floating point value according to the rules of the current locale, with DEFAULT_NUMBER_DECIMALS decimal places retained, and grouping turned off.

Parameters:
value - The floating point value to format.
Returns:
A string representation of the value.

formatDecimal

public java.lang.String formatDecimal(float value,
                                      int decimals)
Format a floating point value according to the rules of the current locale, with the specified number of decimal places retained, and grouping turned off.

Parameters:
value - The floating point value to format.
decimals - The number of decimal places to retain.
Returns:
A string representation of the value.

formatDecimal

public java.lang.String formatDecimal(float value,
                                      int decimals,
                                      boolean grouping)
Format a floating point value according to the rules of the current locale, with the specified number of decimal places retained, and the specified grouping policy.

Parameters:
value - The floating point value to format.
decimals - The number of decimal places to retain.
grouping - A flag specifying whether grouping should be used.
Returns:
A string representation of the value.

parseDecimal

public double parseDecimal(java.lang.String s)
                    throws java.text.ParseException
Parse a numeric value from a string using the rules of the current locale. The input string may include grouping characters.

Parameters:
s - The string to parse.
Returns:
The resulting value.
Throws:
java.text.ParseException - If the value could not be parsed.

getDateFormatSymbols

public java.text.DateFormatSymbols getDateFormatSymbols()
Get an instance of the DateFormatSymbols object for the current locale.

Returns:
The DateFormatSymbols object.

getCollator

public java.text.Collator getCollator()
Get an instance of the Collator object for the current locale.

Returns:
The Collator object.

trapGarbage

private void trapGarbage(java.lang.String s)
                  throws java.text.ParseException
Throws:
java.text.ParseException