|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.commons.beanutils.ConvertUtilsBean
public class ConvertUtilsBean
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:
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.
Constructor Summary | |
---|---|
ConvertUtilsBean()
Construct a bean with standard converters registered |
Method Summary | |
---|---|
String |
convert(Object value)
Convert the specified value into a String. |
Object |
convert(Object value,
Class targetType)
Convert the value to an object of the specified class (if possible). |
Object |
convert(String[] values,
Class clazz)
Convert an array of specified values to an array of objects of the specified class (if possible). |
Object |
convert(String value,
Class clazz)
Convert the specified value to an object of the specified class (if possible). |
void |
deregister()
Remove all registered Converter s, and re-establish the
standard Converters. |
void |
deregister(Class clazz)
Remove any registered Converter for the specified destination
Class . |
boolean |
getDefaultBoolean()
Deprecated. Register replacement converters for Boolean.TYPE and Boolean.class instead |
byte |
getDefaultByte()
Deprecated. Register replacement converters for Byte.TYPE and Byte.class instead |
char |
getDefaultCharacter()
Deprecated. Register replacement converters for Character.TYPE and Character.class instead |
double |
getDefaultDouble()
Deprecated. Register replacement converters for Double.TYPE and Double.class instead |
float |
getDefaultFloat()
Deprecated. Register replacement converters for Float.TYPE and Float.class instead |
int |
getDefaultInteger()
Deprecated. Register replacement converters for Integer.TYPE and Integer.class instead |
long |
getDefaultLong()
Deprecated. Register replacement converters for Long.TYPE and Long.class instead |
short |
getDefaultShort()
Deprecated. Register replacement converters for Short.TYPE and Short.class instead |
protected static ConvertUtilsBean |
getInstance()
Get singleton instance |
Converter |
lookup(Class clazz)
Look up and return any registered Converter for the specified
destination class; if there is no registered Converter, return
null . |
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 . |
void |
register(boolean throwException,
boolean defaultNull,
int defaultArraySize)
Register the provided converters with the specified defaults. |
void |
register(Converter converter,
Class clazz)
Register a custom Converter for the specified destination
Class , replacing any previously registered Converter. |
void |
setDefaultBoolean(boolean newDefaultBoolean)
Deprecated. Register replacement converters for Boolean.TYPE and Boolean.class instead |
void |
setDefaultByte(byte newDefaultByte)
Deprecated. Register replacement converters for Byte.TYPE and Byte.class instead |
void |
setDefaultCharacter(char newDefaultCharacter)
Deprecated. Register replacement converters for Character.TYPE and Character.class instead |
void |
setDefaultDouble(double newDefaultDouble)
Deprecated. Register replacement converters for Double.TYPE and Double.class instead |
void |
setDefaultFloat(float newDefaultFloat)
Deprecated. Register replacement converters for Float.TYPE and Float.class instead |
void |
setDefaultInteger(int newDefaultInteger)
Deprecated. Register replacement converters for Integer.TYPE and Integer.class instead |
void |
setDefaultLong(long newDefaultLong)
Deprecated. Register replacement converters for Long.TYPE and Long.class instead |
void |
setDefaultShort(short newDefaultShort)
Deprecated. Register replacement converters for Short.TYPE and Short.class instead |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public ConvertUtilsBean()
Method Detail |
---|
protected static ConvertUtilsBean getInstance()
public boolean getDefaultBoolean()
public void setDefaultBoolean(boolean newDefaultBoolean)
newDefaultBoolean
- The default Boolean valuepublic byte getDefaultByte()
public void setDefaultByte(byte newDefaultByte)
newDefaultByte
- The default Byte valuepublic char getDefaultCharacter()
public void setDefaultCharacter(char newDefaultCharacter)
newDefaultCharacter
- The default Character valuepublic double getDefaultDouble()
public void setDefaultDouble(double newDefaultDouble)
newDefaultDouble
- The default Double valuepublic float getDefaultFloat()
public void setDefaultFloat(float newDefaultFloat)
newDefaultFloat
- The default Float valuepublic int getDefaultInteger()
public void setDefaultInteger(int newDefaultInteger)
newDefaultInteger
- The default Integer valuepublic long getDefaultLong()
public void setDefaultLong(long newDefaultLong)
newDefaultLong
- The default Long valuepublic short getDefaultShort()
public void setDefaultShort(short newDefaultShort)
newDefaultShort
- The default Short valuepublic String convert(Object value)
Converter
for the
java.lang.String
class will be used, which allows
applications to customize Object->String conversions (the default
implementation simply uses toString()).
value
- Value to be converted (may be null)
public Object convert(String value, Class clazz)
value
- Value to be converted (may be null)clazz
- Java class to be converted to
ConversionException
- if thrown by an underlying Converterpublic Object convert(String[] values, Class clazz)
values
- Array of values to be convertedclazz
- Java array or element class to be converted to
ConversionException
- if thrown by an underlying Converterpublic Object convert(Object value, Class targetType)
Convert the value to an object of the specified class (if possible).
value
- Value to be converted (may be null)targetType
- Class of the value to be converted to
ConversionException
- if thrown by an underlying Converterpublic void deregister()
Converter
s, and re-establish the
standard Converters.
public void register(boolean throwException, boolean defaultNull, int defaultArraySize)
throwException
- true
if the converters should
throw an exception when a conversion error occurs, otherwise
false
if a default value should be used.defaultNull
- true
if the standard converters
(see registerStandard(boolean, boolean)
)
should use a default value of null
, otherwise false
.
N.B. This values is ignored if throwException
is true
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 for
the default.
public void deregister(Class clazz)
Converter
for the specified destination
Class
.
clazz
- Class for which to remove a registered Converterpublic Converter lookup(Class clazz)
Converter
for the specified
destination class; if there is no registered Converter, return
null
.
clazz
- Class for which to return a registered Converter
Converter
or null
if not foundpublic Converter lookup(Class sourceType, Class targetType)
Converter
for the specified
source and destination class; if there is no registered Converter,
return null
.
sourceType
- Class of the value being convertedtargetType
- Class of the value to be converted to
Converter
or null
if not foundpublic void register(Converter converter, Class clazz)
Converter
for the specified destination
Class
, replacing any previously registered Converter.
converter
- Converter to be registeredclazz
- Destination class for conversions performed by this
Converter
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |