org.apache.commons.beanutils

Class ConvertUtilsBean

public class ConvertUtilsBean extends Object

Utility methods for converting String scalar values to objects of the specified Class, String arrays to arrays of the specified Class. The actual {@link Converter} instance to be used can be registered for each possible destination Class. Unless you override them, standard {@link 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 {@link 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
 

Since: 1.7

Version: $Revision: 1.12.2.1 $ $Date: 2004/07/27 21:44:26 $

Author: Craig R. McClanahan Ralph Schaer Chris Audley James Strachan

Field Summary
FastHashMapconverters
The set of {@link Converter}s that can be used to convert Strings into objects of a specified Class, keyed by the destination Class.
BooleandefaultBoolean
The default value for Boolean conversions.
BytedefaultByte
The default value for Byte conversions.
CharacterdefaultCharacter
The default value for Character conversions.
DoubledefaultDouble
The default value for Double conversions.
FloatdefaultFloat
The default value for Float conversions.
IntegerdefaultInteger
The default value for Integer conversions.
LongdefaultLong
The default value for Long conversions.
static ShortdefaultShort
The default value for Short conversions.
Loglog
The Log instance for this class.
Constructor Summary
ConvertUtilsBean()
Construct a bean with standard converters registered
Method Summary
Stringconvert(Object value)
Convert the specified value into a String.
Objectconvert(String value, Class clazz)
Convert the specified value to an object of the specified class (if possible).
Objectconvert(String[] values, Class clazz)
Convert an array of specified values to an array of objects of the specified class (if possible).
voidderegister()
Remove all registered {@link Converter}s, and re-establish the standard Converters.
voidderegister(Class clazz)
Remove any registered {@link Converter} for the specified destination Class.
booleangetDefaultBoolean()
Gets the default value for Boolean conversions.
bytegetDefaultByte()
Gets the default value for Byte conversions.
chargetDefaultCharacter()
Gets the default value for Character conversions.
doublegetDefaultDouble()
Gets the default value for Double conversions.
floatgetDefaultFloat()
Gets the default value for Float conversions.
intgetDefaultInteger()
Gets the default value for Integer conversions.
longgetDefaultLong()
Gets the default value for Long conversions.
shortgetDefaultShort()
Gets the default value for Short conversions.
protected static ConvertUtilsBeangetInstance()
Get singleton instance
Converterlookup(Class clazz)
Look up and return any registered {@link Converter} for the specified destination class; if there is no registered Converter, return null.
voidregister(Class clazz, Converter converter)
strictly for convenience since it has same parameter order as Map.put
voidregister(Converter converter, Class clazz)
Register a custom {@link Converter} for the specified destination Class, replacing any previously registered Converter.
voidsetDefaultBoolean(boolean newDefaultBoolean)
Sets the default value for Boolean conversions.
voidsetDefaultByte(byte newDefaultByte)
Sets the default value for Byte conversions.
voidsetDefaultCharacter(char newDefaultCharacter)
Sets the default value for Character conversions.
voidsetDefaultDouble(double newDefaultDouble)
Sets the default value for Double conversions.
voidsetDefaultFloat(float newDefaultFloat)
Sets the default value for Float conversions.
voidsetDefaultInteger(int newDefaultInteger)
Sets the default value for Integer conversions.
voidsetDefaultLong(long newDefaultLong)
Sets the default value for Long conversions.
voidsetDefaultShort(short newDefaultShort)
Sets the default value for Short conversions.

Field Detail

converters

private FastHashMap converters
The set of {@link Converter}s that can be used to convert Strings into objects of a specified Class, keyed by the destination Class.

defaultBoolean

private Boolean defaultBoolean

Deprecated: Register replacement converters for Boolean.TYPE and Boolean.class instead

The default value for Boolean conversions.

defaultByte

private Byte defaultByte

Deprecated: Register replacement converters for Byte.TYPE and Byte.class instead

The default value for Byte conversions.

defaultCharacter

private Character defaultCharacter

Deprecated: Register replacement converters for Character.TYPE and Character.class instead

The default value for Character conversions.

defaultDouble

private Double defaultDouble

Deprecated: Register replacement converters for Double.TYPE and Double.class instead

The default value for Double conversions.

defaultFloat

private Float defaultFloat

Deprecated: Register replacement converters for Float.TYPE and Float.class instead

The default value for Float conversions.

defaultInteger

private Integer defaultInteger

Deprecated: Register replacement converters for Integer.TYPE and Integer.class instead

The default value for Integer conversions.

defaultLong

private Long defaultLong

Deprecated: Register replacement converters for Long.TYPE and Long.class instead

The default value for Long conversions.

defaultShort

private static Short defaultShort

Deprecated: Register replacement converters for Short.TYPE and Short.class instead

The default value for Short conversions.

log

private Log log
The Log instance for this class.

Constructor Detail

ConvertUtilsBean

public ConvertUtilsBean()
Construct a bean with standard converters registered

Method Detail

convert

public String convert(Object value)
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 {@link 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)

convert

public Object convert(String value, 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 Value to be converted (may be null) clazz Java class to be converted to

Throws: ConversionException if thrown by an underlying Converter

convert

public Object convert(String[] values, Class clazz)
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 Values to be converted (may be null) clazz Java array or element class to be converted to

Throws: ConversionException if thrown by an underlying Converter

deregister

public void deregister()
Remove all registered {@link Converter}s, and re-establish the standard Converters.

deregister

public void deregister(Class clazz)
Remove any registered {@link Converter} for the specified destination Class.

Parameters: clazz Class for which to remove a registered Converter

getDefaultBoolean

public boolean getDefaultBoolean()

Deprecated: Register replacement converters for Boolean.TYPE and Boolean.class instead

Gets the default value for Boolean conversions.

getDefaultByte

public byte getDefaultByte()

Deprecated: Register replacement converters for Byte.TYPE and Byte.class instead

Gets the default value for Byte conversions.

getDefaultCharacter

public char getDefaultCharacter()

Deprecated: Register replacement converters for Character.TYPE and Character.class instead

Gets the default value for Character conversions.

getDefaultDouble

public double getDefaultDouble()

Deprecated: Register replacement converters for Double.TYPE and Double.class instead

Gets the default value for Double conversions.

getDefaultFloat

public float getDefaultFloat()

Deprecated: Register replacement converters for Float.TYPE and Float.class instead

Gets the default value for Float conversions.

getDefaultInteger

public int getDefaultInteger()

Deprecated: Register replacement converters for Integer.TYPE and Integer.class instead

Gets the default value for Integer conversions.

getDefaultLong

public long getDefaultLong()

Deprecated: Register replacement converters for Long.TYPE and Long.class instead

Gets the default value for Long conversions.

getDefaultShort

public short getDefaultShort()

Deprecated: Register replacement converters for Short.TYPE and Short.class instead

Gets the default value for Short conversions.

getInstance

protected static ConvertUtilsBean getInstance()
Get singleton instance

lookup

public Converter lookup(Class clazz)
Look up and return any registered {@link Converter} for the specified destination class; if there is no registered Converter, return null.

Parameters: clazz Class for which to return a registered Converter

register

private void register(Class clazz, Converter converter)
strictly for convenience since it has same parameter order as Map.put

register

public void register(Converter converter, Class clazz)
Register a custom {@link Converter} for the specified destination Class, replacing any previously registered Converter.

Parameters: converter Converter to be registered clazz Destination class for conversions performed by this Converter

setDefaultBoolean

public void setDefaultBoolean(boolean newDefaultBoolean)

Deprecated: Register replacement converters for Boolean.TYPE and Boolean.class instead

Sets the default value for Boolean conversions.

setDefaultByte

public void setDefaultByte(byte newDefaultByte)

Deprecated: Register replacement converters for Byte.TYPE and Byte.class instead

Sets the default value for Byte conversions.

setDefaultCharacter

public void setDefaultCharacter(char newDefaultCharacter)

Deprecated: Register replacement converters for Character.TYPE and Character.class instead

Sets the default value for Character conversions.

setDefaultDouble

public void setDefaultDouble(double newDefaultDouble)

Deprecated: Register replacement converters for Double.TYPE and Double.class instead

Sets the default value for Double conversions.

setDefaultFloat

public void setDefaultFloat(float newDefaultFloat)

Deprecated: Register replacement converters for Float.TYPE and Float.class instead

Sets the default value for Float conversions.

setDefaultInteger

public void setDefaultInteger(int newDefaultInteger)

Deprecated: Register replacement converters for Integer.TYPE and Integer.class instead

Sets the default value for Integer conversions.

setDefaultLong

public void setDefaultLong(long newDefaultLong)

Deprecated: Register replacement converters for Long.TYPE and Long.class instead

Sets the default value for Long conversions.

setDefaultShort

public void setDefaultShort(short newDefaultShort)

Deprecated: Register replacement converters for Short.TYPE and Short.class instead

Sets the default value for Short conversions.
Copyright (c) 2001-2004 - Apache Software Foundation