| java.lang.Object org.apache.commons.beanutils.ConvertUtilsBean
All known Subclasses: org.apache.commons.beanutils.ConvertUtilsBean2,
ConvertUtilsBean | public class ConvertUtilsBean (Code) | | Utility methods for converting String scalar values to objects of the
specified Class, String arrays to arrays of the specified Class. The
actual
Converter instance to be used can be registered for each
possible destination Class. Unless you override them, standard
Converter instances are provided for all of the following
destination Classes:
- java.lang.BigDecimal (no default value)
- java.lang.BigInteger (no default value)
- boolean and java.lang.Boolean (default to false)
- byte and java.lang.Byte (default to zero)
- char and java.lang.Character (default to a space)
- java.lang.Class (no default value)
- double and java.lang.Double (default to zero)
- float and java.lang.Float (default to zero)
- int and java.lang.Integer (default to zero)
- long and java.lang.Long (default to zero)
- short and java.lang.Short (default to zero)
- java.lang.String (default to null)
- java.io.File (no default value)
- java.net.URL (no default value)
- java.sql.Date (no default value)
- java.sql.Time (no default value)
- java.sql.Timestamp (no default value)
For backwards compatibility, the standard Converters for primitive
types (and the corresponding wrapper classes) return a defined
default value when a conversion error occurs. If you prefer to have a
ConversionException thrown instead, replace the standard Converter
instances with instances created with the zero-arguments constructor. For
example, to cause the Converters for integers to throw an exception on
conversion errors, you could do this:
// No-args constructor gets the version that throws exceptions
Converter myConverter =
new org.apache.commons.beanutils.converter.IntegerConverter();
ConvertUtils.register(myConverter, Integer.TYPE); // Native type
ConvertUtils.register(myConverter, Integer.class); // Wrapper class
Converters generally treat null input as if it were invalid
input, ie they return their default value if one was specified when the
converter was constructed, and throw an exception otherwise. If you prefer
nulls to be preserved for converters that are converting to objects (not
primitives) then register a converter as above, passing a default value of
null to the converter constructor (and of course registering that converter
only for the .class target).
When a converter is listed above as having no default value, then that
converter will throw an exception when passed null or an invalid value
as its input. In particular, by default the BigInteger and BigDecimal
converters have no default (and are therefore somewhat inconsistent
with the other numerical converters which all have zero as their default).
Converters that generate arrays of each of the primitive types are
also automatically configured (including String[]). When passed null
or invalid input, these return an empty array (not null). See class
AbstractArrayConverter for the supported input formats for these converters.
author: Craig R. McClanahan author: Ralph Schaer author: Chris Audley author: James Strachan version: $Revision: 557868 $ $Date: 2007-07-20 06:22:19 +0100 (Fri, 20 Jul 2007) $ since: 1.7 |
Method Summary | |
public String | convert(Object value) Convert the specified value into a String. | public Object | convert(String value, Class clazz) Convert the specified value to an object of the specified class (if
possible). | public Object | convert(String[] values, Class clazz) Convert an array of specified values to an array of objects of the
specified class (if possible). | public Object | convert(Object value, Class targetType) | public void | deregister() Remove all registered
Converter s, and re-establish the
standard Converters. | public void | deregister(Class clazz) Remove any registered
Converter for the specified destination
Class . | public boolean | getDefaultBoolean() Gets the default value for Boolean conversions. | public byte | getDefaultByte() Gets the default value for Byte conversions. | public char | getDefaultCharacter() Gets the default value for Character conversions. | public double | getDefaultDouble() Gets the default value for Double conversions. | public float | getDefaultFloat() Gets the default value for Float conversions. | public int | getDefaultInteger() Gets the default value for Integer conversions. | public long | getDefaultLong() Gets the default value for Long conversions. | public short | getDefaultShort() Gets the default value for Short conversions. | protected static ConvertUtilsBean | getInstance() | public Converter | lookup(Class clazz) Look up and return any registered
Converter for the specified
destination class; if there is no registered Converter, return
null . | public Converter | lookup(Class sourceType, Class targetType) Look up and return any registered
Converter for the specified
source and destination class; if there is no registered Converter,
return null . | public void | register(boolean throwException, boolean defaultNull, int defaultArraySize) Register the provided converters with the specified defaults.
Parameters: throwException - true if the converters shouldthrow an exception when a conversion error occurs, otherwise false if a default value should be used. Parameters: defaultNull - true if the standard converters(see ConvertUtilsBean.registerStandard(booleanboolean))should use a default value of null , otherwise false .N.B.
| public void | register(Converter converter, Class clazz) Register a custom
Converter for the specified destination
Class , replacing any previously registered Converter. | public void | setDefaultBoolean(boolean newDefaultBoolean) Sets the default value for Boolean conversions. | public void | setDefaultByte(byte newDefaultByte) Sets the default value for Byte conversions. | public void | setDefaultCharacter(char newDefaultCharacter) Sets the default value for Character conversions. | public void | setDefaultDouble(double newDefaultDouble) Sets the default value for Double conversions. | public void | setDefaultFloat(float newDefaultFloat) Sets the default value for Float conversions. | public void | setDefaultInteger(int newDefaultInteger) Sets the default value for Integer conversions. | public void | setDefaultLong(long newDefaultLong) Sets the default value for Long conversions. | public void | setDefaultShort(short newDefaultShort) Sets the default value for Short conversions. |
ConvertUtilsBean | public ConvertUtilsBean()(Code) | | Construct a bean with standard converters registered
|
convert | public String convert(Object value)(Code) | | Convert the specified value into a String. If the specified value
is an array, the first element (converted to a String) will be
returned. The registered
Converter for the
java.lang.String class will be used, which allows
applications to customize Object->String conversions (the default
implementation simply uses toString()).
Parameters: value - Value to be converted (may be null) The converted String value |
convert | public Object convert(String value, Class clazz)(Code) | | Convert the specified value to an object of the specified class (if
possible). Otherwise, return a String representation of the value.
Parameters: value - Value to be converted (may be null) Parameters: clazz - Java class to be converted to The converted value exception: ConversionException - if thrown by an underlying Converter |
convert | public Object convert(String[] values, Class clazz)(Code) | | Convert an array of specified values to an array of objects of the
specified class (if possible). If the specified Java class is itself
an array class, this class will be the type of the returned value.
Otherwise, an array will be constructed whose component type is the
specified class.
Parameters: values - Array of values to be converted Parameters: clazz - Java array or element class to be converted to The converted value exception: ConversionException - if thrown by an underlying Converter |
convert | public Object convert(Object value, Class targetType)(Code) | | Convert the value to an object of the specified class (if
possible).
Parameters: value - Value to be converted (may be null) Parameters: targetType - Class of the value to be converted to The converted value exception: ConversionException - if thrown by an underlying Converter |
deregister | public void deregister()(Code) | | Remove all registered
Converter s, and re-establish the
standard Converters.
|
deregister | public void deregister(Class clazz)(Code) | | Remove any registered
Converter for the specified destination
Class .
Parameters: clazz - Class for which to remove a registered Converter |
getDefaultBoolean | public boolean getDefaultBoolean()(Code) | | Gets the default value for Boolean conversions.
The default Boolean value |
getDefaultByte | public byte getDefaultByte()(Code) | | Gets the default value for Byte conversions.
The default Byte value |
getDefaultCharacter | public char getDefaultCharacter()(Code) | | Gets the default value for Character conversions.
The default Character value |
getDefaultDouble | public double getDefaultDouble()(Code) | | Gets the default value for Double conversions.
The default Double value |
getDefaultFloat | public float getDefaultFloat()(Code) | | Gets the default value for Float conversions.
The default Float value |
getDefaultInteger | public int getDefaultInteger()(Code) | | Gets the default value for Integer conversions.
The default Integer value |
getDefaultLong | public long getDefaultLong()(Code) | | Gets the default value for Long conversions.
The default Long value |
getDefaultShort | public short getDefaultShort()(Code) | | Gets the default value for Short conversions.
The default Short value |
getInstance | protected static ConvertUtilsBean getInstance()(Code) | | Get singleton instance
The singleton instance |
lookup | public Converter lookup(Class clazz)(Code) | | Look up and return any registered
Converter for the specified
destination class; if there is no registered Converter, return
null .
Parameters: clazz - Class for which to return a registered Converter The registered Converter or null if not found |
lookup | public Converter lookup(Class sourceType, Class targetType)(Code) | | Look up and return any registered
Converter for the specified
source and destination class; if there is no registered Converter,
return null .
Parameters: sourceType - Class of the value being converted Parameters: targetType - Class of the value to be converted to The registered Converter or null if not found |
register | public void register(boolean throwException, boolean defaultNull, int defaultArraySize)(Code) | | Register the provided converters with the specified defaults.
Parameters: throwException - true if the converters shouldthrow an exception when a conversion error occurs, otherwise false if a default value should be used. Parameters: defaultNull - true if the standard converters(see ConvertUtilsBean.registerStandard(booleanboolean))should use a default value of null , otherwise false .N.B. This values is ignored if throwException is true Parameters: defaultArraySize - The size of the default array value for array converters(N.B. This values is ignored if throwException is true ).Specifying a value less than zero causes a null value to be used forthe default.
|
register | public void register(Converter converter, Class clazz)(Code) | | Register a custom
Converter for the specified destination
Class , replacing any previously registered Converter.
Parameters: converter - Converter to be registered Parameters: clazz - Destination class for conversions performed by thisConverter |
setDefaultBoolean | public void setDefaultBoolean(boolean newDefaultBoolean)(Code) | | Sets the default value for Boolean conversions.
Parameters: newDefaultBoolean - The default Boolean value |
setDefaultByte | public void setDefaultByte(byte newDefaultByte)(Code) | | Sets the default value for Byte conversions.
Parameters: newDefaultByte - The default Byte value |
setDefaultCharacter | public void setDefaultCharacter(char newDefaultCharacter)(Code) | | Sets the default value for Character conversions.
Parameters: newDefaultCharacter - The default Character value |
setDefaultDouble | public void setDefaultDouble(double newDefaultDouble)(Code) | | Sets the default value for Double conversions.
Parameters: newDefaultDouble - The default Double value |
setDefaultFloat | public void setDefaultFloat(float newDefaultFloat)(Code) | | Sets the default value for Float conversions.
Parameters: newDefaultFloat - The default Float value |
setDefaultInteger | public void setDefaultInteger(int newDefaultInteger)(Code) | | Sets the default value for Integer conversions.
Parameters: newDefaultInteger - The default Integer value |
setDefaultLong | public void setDefaultLong(long newDefaultLong)(Code) | | Sets the default value for Long conversions.
Parameters: newDefaultLong - The default Long value |
setDefaultShort | public void setDefaultShort(short newDefaultShort)(Code) | | Sets the default value for Short conversions.
Parameters: newDefaultShort - The default Short value |
|
|