org.apache.commons.convert.conversion
Class AbstractConversionFactory

java.lang.Object
  extended by org.apache.commons.convert.conversion.AbstractConversionFactory
All Implemented Interfaces:
ConversionFactory
Direct Known Subclasses:
NumberToIntegerConversionFactory, ObjectToStringConversionFactory, TimeZoneToStringConversionFactory

public abstract class AbstractConversionFactory
extends Object
implements ConversionFactory

Abstract base class for creating conversion factories.

This class is especially suited for creating factories as it minimises the effort required in the subclass.

To implement a conversion from a class and all of it subclasses, simply extend this class and implement the convertValue(Object, Converter) method.

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

Field Summary
protected static Class STRING_CLASS
          Useful constant for subclass constructors
 
Constructor Summary
protected AbstractConversionFactory(Class baseFromType, Class toType)
          Restricted constructor.
 
Method Summary
protected abstract  Object convertValue(Object value, Converter converter)
          Convert the non-null value to another type.
 Class getBaseFromType()
          The base type to convert from.
 Conversion getInstance(Object value, Class fromType, Class toType)
          Create a new conversion object for the specified from and to types.
protected  int getMatchPercent()
          Gets the percentage to return if a match occurs.
 int getMatchPercent(Object value, Class fromType, Class toType)
          Checks if this factory matches the request conversion.
 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

AbstractConversionFactory

protected AbstractConversionFactory(Class baseFromType,
                                    Class toType)
Restricted constructor.

Parameters:
baseFromType - the base of the class hierarchy to convert from
toType - the type to convert to
Method Detail

getMatchPercent

public int getMatchPercent(Object value,
                           Class fromType,
                           Class toType)
Checks if this factory matches the request conversion.

This implementation returns getMatchPercent() if the specified fromType is the same as, or a subclass of, the stored baseFromType and the specified toType matches the stored toType.

Specified by:
getMatchPercent in interface ConversionFactory
Parameters:
value - the value to be converted, read only, may be null
fromType - the type to convert from, may be null
toType - the type to convert to, may be null
Returns:
60 if baseFromType or subclass matches fromType and toType matches

getMatchPercent

protected int getMatchPercent()
Gets the percentage to return if a match occurs.

This implementation returns 60. Subclasses may wish to override and return a different value.

Returns:
60

getInstance

public Conversion getInstance(Object value,
                              Class fromType,
                              Class toType)
Create a new conversion object for the specified from and to types.

This implementation returns a Conversion instance that refers back to this class for its implementation of the actual conversion via convertValue(Object, Converter).

Specified by:
getInstance in interface ConversionFactory
Parameters:
value - the value to be converted, read only, may be null
fromType - the type to convert from, may be null
toType - the type to convert to, may be null
Returns:
a Conversion object for repeatedly performing conversions

convertValue

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

This method is implemented by subclases to perform the actual conversion.

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

getBaseFromType

public Class getBaseFromType()
The base type to convert from. This type and its subclasses will be matched by this factory.

Returns:
the Class object representing the class to convert to

getToType

public Class getToType()
The type to convert to.

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.