Class LocaleConvertUtilsBean
- java.lang.Object
-
- org.apache.commons.beanutils.locale.LocaleConvertUtilsBean
-
public class LocaleConvertUtilsBean extends java.lang.ObjectUtility methods for converting locale-sensitive String scalar values to objects of the specified Class, String arrays to arrays of the specified Class and object to locale-sensitive String scalar value.
This class provides the implementations used by the static utility methods in
LocaleConvertUtils.The actual
LocaleConverterinstance to be used can be registered for each possible destination Class. Unless you override them, standardLocaleConverterinstances are provided for all of the following destination Classes:- java.lang.BigDecimal
- java.lang.BigInteger
- byte and java.lang.Byte
- double and java.lang.Double
- float and java.lang.Float
- int and java.lang.Integer
- long and java.lang.Long
- short and java.lang.Short
- java.lang.String
- java.sql.Date
- java.sql.Time
- java.sql.Timestamp
For backwards compatibility, the standard locale converters for primitive types (and the corresponding wrapper classes). If you prefer to have another
LocaleConverterthrown instead, replace the standardLocaleConverterinstances with ones created with the one of the appropriate constructors. It's important thatLocaleConvertershould be registered for the specified locale and Class (or primitive type).- Since:
- 1.7
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classLocaleConvertUtilsBean.DelegateFastHashMapFastHashMap implementation that uses WeakReferences to overcome memory leak problems.
-
Field Summary
Fields Modifier and Type Field Description private booleanapplyLocalizedIndicate whether the pattern is localized or notprivate java.util.LocaledefaultLocaleThe locale - default for convertion.private org.apache.commons.logging.LoglogTheLoginstance for this class.private org.apache.commons.collections.FastHashMapmapConvertersEvery entry of the mapConverters is: key = locale value = FastHashMap of converters for the certain locale.
-
Constructor Summary
Constructors Constructor Description LocaleConvertUtilsBean()Makes the state by default (deregisters all converters for all locales) and then registers default locale converters.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description java.lang.Stringconvert(java.lang.Object value)Convert the specified locale-sensitive value into a String.java.lang.Stringconvert(java.lang.Object value, java.lang.String pattern)Convert the specified locale-sensitive value into a String using the conversion pattern.java.lang.Stringconvert(java.lang.Object value, java.util.Locale locale, java.lang.String pattern)Convert the specified locale-sensitive value into a String using the paticular convertion pattern.java.lang.Objectconvert(java.lang.String[] values, java.lang.Class<?> clazz)Convert an array of specified values to an array of objects of the specified class (if possible).java.lang.Objectconvert(java.lang.String[] values, java.lang.Class<?> clazz, java.lang.String pattern)Convert an array of specified values to an array of objects of the specified class (if possible) using the convertion pattern.java.lang.Objectconvert(java.lang.String[] values, java.lang.Class<?> clazz, java.util.Locale locale, java.lang.String pattern)Convert an array of specified values to an array of objects of the specified class (if possible) using the convertion pattern.java.lang.Objectconvert(java.lang.String value, java.lang.Class<?> clazz)Convert the specified value to an object of the specified class (if possible).java.lang.Objectconvert(java.lang.String value, java.lang.Class<?> clazz, java.lang.String pattern)Convert the specified value to an object of the specified class (if possible) using the convertion pattern.java.lang.Objectconvert(java.lang.String value, java.lang.Class<?> clazz, java.util.Locale locale, java.lang.String pattern)Convert the specified value to an object of the specified class (if possible) using the convertion pattern.protected org.apache.commons.collections.FastHashMapcreate(java.util.Locale locale)Deprecated.This method will be modified to return a Map in the next release.voidderegister()Remove any registeredLocaleConverter.voidderegister(java.lang.Class<?> clazz, java.util.Locale locale)Remove any registeredLocaleConverterfor the specified locale and Class.voidderegister(java.util.Locale locale)Remove any registeredLocaleConverterfor the specified localebooleangetApplyLocalized()getter for applyLocalizedjava.util.LocalegetDefaultLocale()getter for defaultLocale.static LocaleConvertUtilsBeangetInstance()Gets singleton instance.LocaleConverterlookup(java.lang.Class<?> clazz, java.util.Locale locale)Look up and return any registeredLocaleConverterfor the specified destination class and locale; if there is no registered Converter, returnnull.protected org.apache.commons.collections.FastHashMaplookup(java.util.Locale locale)Deprecated.This method will be modified to return a Map in the next release.voidregister(LocaleConverter converter, java.lang.Class<?> clazz, java.util.Locale locale)Register a customLocaleConverterfor the specified destinationClass, replacing any previously registered converter.voidsetApplyLocalized(boolean newApplyLocalized)setter for applyLocalizedvoidsetDefaultLocale(java.util.Locale locale)setter for defaultLocale.
-
-
-
Field Detail
-
defaultLocale
private java.util.Locale defaultLocale
The locale - default for convertion.
-
applyLocalized
private boolean applyLocalized
Indicate whether the pattern is localized or not
-
log
private final org.apache.commons.logging.Log log
TheLoginstance for this class.
-
mapConverters
private final org.apache.commons.collections.FastHashMap mapConverters
Every entry of the mapConverters is: key = locale value = FastHashMap of converters for the certain locale.
-
-
Method Detail
-
getInstance
public static LocaleConvertUtilsBean getInstance()
Gets singleton instance. This is the same as the instance used by the defaultLocaleBeanUtilsBeansingleton.- Returns:
- the singleton instance
-
convert
public java.lang.String convert(java.lang.Object value)
Convert the specified locale-sensitive value into a String.- Parameters:
value- The Value to be converted- Returns:
- the converted value
- Throws:
ConversionException- if thrown by an underlying Converter
-
convert
public java.lang.String convert(java.lang.Object value, java.util.Locale locale, java.lang.String pattern)Convert the specified locale-sensitive value into a String using the paticular convertion pattern.- Parameters:
value- The Value to be convertedlocale- The localepattern- The convertion pattern- Returns:
- the converted value
- Throws:
ConversionException- if thrown by an underlying Converter
-
convert
public java.lang.String convert(java.lang.Object value, java.lang.String pattern)Convert the specified locale-sensitive value into a String using the conversion pattern.- Parameters:
value- The Value to be convertedpattern- The convertion pattern- Returns:
- the converted value
- Throws:
ConversionException- if thrown by an underlying Converter
-
convert
public java.lang.Object convert(java.lang.String value, java.lang.Class<?> clazz)Convert the specified value to an object of the specified class (if possible). Otherwise, return a String representation of the value.- Parameters:
value- The String scalar value to be convertedclazz- The Data type to which this value should be converted.- Returns:
- the converted value
- Throws:
ConversionException- if thrown by an underlying Converter
-
convert
public java.lang.Object convert(java.lang.String value, java.lang.Class<?> clazz, java.util.Locale locale, java.lang.String pattern)Convert the specified value to an object of the specified class (if possible) using the convertion pattern. Otherwise, return a String representation of the value.- Parameters:
value- The String scalar value to be convertedclazz- The Data type to which this value should be converted.locale- The localepattern- The convertion pattern- Returns:
- the converted value
- Throws:
ConversionException- if thrown by an underlying Converter
-
convert
public java.lang.Object convert(java.lang.String value, java.lang.Class<?> clazz, java.lang.String pattern)Convert the specified value to an object of the specified class (if possible) using the convertion pattern. Otherwise, return a String representation of the value.- Parameters:
value- The String scalar value to be convertedclazz- The Data type to which this value should be converted.pattern- The convertion pattern- Returns:
- the converted value
- Throws:
ConversionException- if thrown by an underlying Converter
-
convert
public java.lang.Object convert(java.lang.String[] values, java.lang.Class<?> clazz)Convert an array of specified values to an array of objects of the specified class (if possible).- Parameters:
values- Value to be converted (may be null)clazz- Java array or element class to be converted to- Returns:
- the converted value
- Throws:
ConversionException- if thrown by an underlying Converter
-
convert
public java.lang.Object convert(java.lang.String[] values, java.lang.Class<?> clazz, java.util.Locale locale, java.lang.String pattern)Convert an array of specified values to an array of objects of the specified class (if possible) using the convertion pattern.- Parameters:
values- Value to be converted (may be null)clazz- Java array or element class to be converted tolocale- The localepattern- The convertion pattern- Returns:
- the converted value
- Throws:
ConversionException- if thrown by an underlying Converter
-
convert
public java.lang.Object convert(java.lang.String[] values, java.lang.Class<?> clazz, java.lang.String pattern)Convert an array of specified values to an array of objects of the specified class (if possible) using the convertion pattern.- Parameters:
values- Value to be converted (may be null)clazz- Java array or element class to be converted topattern- The convertion pattern- Returns:
- the converted value
- Throws:
ConversionException- if thrown by an underlying Converter
-
create
@Deprecated protected org.apache.commons.collections.FastHashMap create(java.util.Locale locale)
Deprecated.This method will be modified to return a Map in the next release.Create allLocaleConvertertypes for specified locale.- Parameters:
locale- The Locale- Returns:
- The FastHashMap instance contains the all
LocaleConvertertypes for the specified locale.
-
deregister
public void deregister()
Remove any registeredLocaleConverter.
-
deregister
public void deregister(java.lang.Class<?> clazz, java.util.Locale locale)Remove any registeredLocaleConverterfor the specified locale and Class.- Parameters:
clazz- Class for which to remove a registered Converterlocale- The locale
-
deregister
public void deregister(java.util.Locale locale)
Remove any registeredLocaleConverterfor the specified locale- Parameters:
locale- The locale
-
getApplyLocalized
public boolean getApplyLocalized()
getter for applyLocalized- Returns:
trueif pattern is localized, otherwisefalse
-
getDefaultLocale
public java.util.Locale getDefaultLocale()
getter for defaultLocale.- Returns:
- the default locale
-
lookup
public LocaleConverter lookup(java.lang.Class<?> clazz, java.util.Locale locale)
Look up and return any registeredLocaleConverterfor the specified destination class and locale; if there is no registered Converter, returnnull.- Parameters:
clazz- Class for which to return a registered Converterlocale- The Locale- Returns:
- The registered locale Converter, if any
-
lookup
@Deprecated protected org.apache.commons.collections.FastHashMap lookup(java.util.Locale locale)
Deprecated.This method will be modified to return a Map in the next release.Look up and return any registered FastHashMap instance for the specified locale; if there is no registered one, returnnull.- Parameters:
locale- The Locale- Returns:
- The FastHashMap instance contains the all
LocaleConvertertypes for the specified locale.
-
register
public void register(LocaleConverter converter, java.lang.Class<?> clazz, java.util.Locale locale)
Register a customLocaleConverterfor the specified destinationClass, replacing any previously registered converter.- Parameters:
converter- The LocaleConverter to be registeredclazz- The Destination class for conversions performed by this Converterlocale- The locale
-
setApplyLocalized
public void setApplyLocalized(boolean newApplyLocalized)
setter for applyLocalized- Parameters:
newApplyLocalized-trueif pattern is localized, otherwisefalse
-
setDefaultLocale
public void setDefaultLocale(java.util.Locale locale)
setter for defaultLocale.- Parameters:
locale- the default locale
-
-