org.apache.commons.convert.conversion
Class AbstractConversion

java.lang.Object
  extended by org.apache.commons.convert.conversion.AbstractConversion
All Implemented Interfaces:
Conversion
Direct Known Subclasses:
ClassToStringConversion, StringToIntegerConversion

public abstract class AbstractConversion
extends Object
implements Conversion

Abstract base class for conversion objects that provides basic features.

Most Conversion implementations will extend this class. It provides convenient implementations of the methods of the interface, and simplifies null-handling in the subclass.

To implement a conversion from a single class, simply extend this class and implement the convertValue(Object, Converter) method. To implement a conversion from a class and its subclasses, use AbstractConversionFactory.

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

Field Summary
protected static Class STRING_CLASS
          Useful constant for subclass constructors
 
Constructor Summary
protected AbstractConversion(Class fromType, Class toType)
          Constructor that stores the from and to types.
 
Method Summary
 Object convert(Object value, Converter converter)
          Converts an object from one type to another.
protected  Object convertValue(Object value, Converter converter)
          Convert the specified non-null value to another type.
 Class getFromType()
          The type to convert from.
 Class getToType()
          The type to convert to.
 String toString()
          Gets a suitable debugging string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

STRING_CLASS

protected static final Class STRING_CLASS
Useful constant for subclass constructors

Constructor Detail

AbstractConversion

protected AbstractConversion(Class fromType,
                             Class toType)
Constructor that stores the from and to types.

Parameters:
fromType - the type to convert from
toType - the type to convert to
Method Detail

convert

public Object convert(Object value,
                      Converter converter)
               throws Exception
Converts an object from one type to another.

This implementation delegates to convertValue after handling null. If the null-safe behaviour is undesired, override this method.

Specified by:
convert in interface Conversion
Parameters:
value - the input value to be converted, may be null
converter - the converter being used, not null
Returns:
the converted value
Throws:
Exception - if conversion fails, use ConversionException if creating a new exception, otherwise just allow exceptions to be thrown

convertValue

protected Object convertValue(Object value,
                              Converter converter)
                       throws Exception
Convert the specified non-null value to another type.

Parameters:
value - the input value to be converted, pre-checked to not be null
converter - the converter being used, not null
Returns:
the converted value
Throws:
Exception - if conversion fails, use ConversionException if creating a new exception, otherwise just allow exceptions to be thrown

getFromType

public Class getFromType()
The type to convert from.

Specified by:
getFromType in interface Conversion
Returns:
the Class object representing the class to convert to

getToType

public Class getToType()
The type to convert to.

Specified by:
getToType in interface Conversion
Returns:
the Class object representing the class to convert from

toString

public String toString()
Gets a suitable debugging string.

Overrides:
toString in class Object
Returns:
a debugging string


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