org.apache.commons.convert
Class Converter

java.lang.Object
  extended by org.apache.commons.convert.Converter

public class Converter
extends Object

Converter is the central class that holds a set of registered converters together and allows conversion to occur.

This class allows applications to add their own converters. This is achieved by creating a new instance and using that within the application. Generally, the instance is stored in a static variable in the application.

In this way, the application remains independent of other converters used by other applications or libraries.

Since:
1.0
Version:
$Id: Converter.java 155441 2005-02-26 13:19:22Z dirkv $
Author:
Stephen Colebourne

Constructor Summary
Converter()
          Constructs a new instance of the Converter useful to create a set of conversions separate from the default set.
Converter(boolean addDefaults)
          Constructs a new instance of the Converter useful to create a set of conversions separate from the default set.
 
Method Summary
 Object convert(Object value, Class toClass)
          Convert the specified input object into an output object of the another type.
 Object convert(Object value, Class fromClass, Class toClass)
          Convert the specified input object into an output object of the another type.
 String convertToString(Object value)
          Convert the specified input object into a String.
 String convertToString(Object value, Class fromClass)
          Convert the specified input object into a String.
protected  ConversionRegistry createRegistry()
          Factory method for subclasses to change the registry implementation.
protected  Object doConversion(Conversion conversion, Object value)
          Convert the specified input object into an output object using the conversion.
protected  Object doConversion(Object value, Class fromClass, Class toClass)
          Convert the specified input object into an output object of another type.
 ConversionRegistry getRegistry()
          Gets the converter registry used for adding and removing converters.
 String toString()
          Returns a string describing this object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Converter

public Converter()
Constructs a new instance of the Converter useful to create a set of conversions separate from the default set.

The created converter has no conversions registered.


Converter

public Converter(boolean addDefaults)
Constructs a new instance of the Converter useful to create a set of conversions separate from the default set.

Parameters:
addDefaults - whether to add the default conversions
Method Detail

createRegistry

protected ConversionRegistry createRegistry()
Factory method for subclasses to change the registry implementation.

Returns:
a new, non-null, registry

convert

public Object convert(Object value,
                      Class fromClass,
                      Class toClass)
Convert the specified input object into an output object of the another type.

Parameters:
value - the input value to be converted
fromClass - the class to convert from, useful if null passed in
toClass - the class to convert to
Returns:
the converted value
Throws:
ConversionException - (runtime) if conversion fails

convert

public Object convert(Object value,
                      Class toClass)
Convert the specified input object into an output object of the another type.

Parameters:
value - the input value to be converted
toClass - the class to convert to
Returns:
the converted value
Throws:
ConversionException - (runtime) if conversion fails

convertToString

public String convertToString(Object value,
                              Class fromClass)
Convert the specified input object into a String.

Parameters:
value - the input value to be converted
fromClass - the class to convert from, useful if null passed in
Returns:
the converted value
Throws:
ConversionException - (runtime) if conversion fails

convertToString

public String convertToString(Object value)
Convert the specified input object into a String.

Parameters:
value - the input value to be converted
Returns:
the converted value
Throws:
ConversionException - (runtime) if conversion fails

doConversion

protected Object doConversion(Object value,
                              Class fromClass,
                              Class toClass)
Convert the specified input object into an output object of another type.

This implementation uses {doConversion(Conversion, Object).

Parameters:
value - the input value to be converted
fromClass - the class to convert from, useful if null passed in
toClass - the class to convert to
Returns:
the converted value
Throws:
ConversionException - (runtime) if conversion fails

doConversion

protected Object doConversion(Conversion conversion,
                              Object value)
Convert the specified input object into an output object using the conversion.

This implementation catches exceptions and wraps them in a ConversionException.

Parameters:
conversion - the conversion to use
value - the value to convert
Returns:
the converted value
Throws:
ConversionException - (runtime) if conversion fails

getRegistry

public ConversionRegistry getRegistry()
Gets the converter registry used for adding and removing converters.

Returns:
the converter registry, never null

toString

public String toString()
Returns a string describing this object.

Overrides:
toString in class Object
Returns:
a string describing this object


Copyright © 2003-2010 The Apache Software Foundation. All Rights Reserved.