net.sourceforge.stripes.validation
Class DefaultTypeConverterFactory

java.lang.Object
  extended by net.sourceforge.stripes.validation.DefaultTypeConverterFactory
All Implemented Interfaces:
ConfigurableComponent, TypeConverterFactory

public class DefaultTypeConverterFactory
extends Object
implements TypeConverterFactory

Default TypeConverterFactory implementation that simply creates an instance level map of all the TypeConverters included in the Stripes distribution, and their applicable classes. Can handle all the primitive and wrapper types as well as the rich types for which type converters exist.

Author:
Tim Fennell

Constructor Summary
DefaultTypeConverterFactory()
           
 
Method Summary
 void add(Class<?> targetType, Class<? extends TypeConverter<?>> converterClass)
          Adds a TypeConverter to the set of registered TypeConverters, overriding an existing converter if one was registered for the type.
protected  Class<? extends TypeConverter<?>> cacheTypeConverterClass(Class<?> clazz, Class<? extends TypeConverter<?>> converterClass)
          Add converter class converterClass for converting objects of type clazz.
protected  void clearCache()
          Clear the instance cache.
protected  Class<? extends TypeConverter<?>> findTypeConverterClass(Class<?> targetClass)
          Search for a type converter class that best matches the requested class.
protected  Configuration getConfiguration()
          Provides subclasses with access to the configuration provided at initialization.
 TypeConverter getInstance(Class<? extends TypeConverter> clazz, Locale locale)
          Gets an instance of the TypeConverter class specified.
 TypeConverter getTypeConverter(Class forType, Locale locale)
          Gets the applicable type converter for the class passed in.
protected  Map<Class<?>,Class<? extends TypeConverter<?>>> getTypeConverters()
          Gets the (rather confusing) Map of TypeConverter objects.
 void init(Configuration configuration)
          Places all the known convertible types and type converters into an instance level Map.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DefaultTypeConverterFactory

public DefaultTypeConverterFactory()
Method Detail

init

public void init(Configuration configuration)
Places all the known convertible types and type converters into an instance level Map.

Specified by:
init in interface ConfigurableComponent
Parameters:
configuration - the Configuration object being used by Stripes

getConfiguration

protected Configuration getConfiguration()
Provides subclasses with access to the configuration provided at initialization.


getTypeConverters

protected Map<Class<?>,Class<? extends TypeConverter<?>>> getTypeConverters()
Gets the (rather confusing) Map of TypeConverter objects. The Map uses the target class as the key in the Map, and the Class object representing the TypeConverter as the value.

Returns:
the Map of TypeConverter classes

add

public void add(Class<?> targetType,
                Class<? extends TypeConverter<?>> converterClass)
Adds a TypeConverter to the set of registered TypeConverters, overriding an existing converter if one was registered for the type.

Specified by:
add in interface TypeConverterFactory
Parameters:
targetType - the type for which the converter will handle conversions
converterClass - the implementation class that will handle the conversions

getTypeConverter

public TypeConverter getTypeConverter(Class forType,
                                      Locale locale)
                               throws Exception
Gets the applicable type converter for the class passed in. This is based on the default set of type converters which are stored in a Map on this class. Enums are a special case, whereby if there is no converter in the map, the EnumeratedTypeConverter will be returned.

Specified by:
getTypeConverter in interface TypeConverterFactory
Parameters:
forType - the type/Class that is the target type of the conversion. It is assumed that the input type is String, so to convert a String to a Date object you would supply java.util.Date.class.
locale - the locale of the Strings to be converted with the returned converter
Returns:
an instance of a TypeConverter which will convert Strings to the desired type
Throws:
Exception - if the TypeConverter cannot be instantiated

findTypeConverterClass

protected Class<? extends TypeConverter<?>> findTypeConverterClass(Class<?> targetClass)
Search for a type converter class that best matches the requested class.

Parameters:
targetClass - the class of the object that needs to be converted
Returns:
the first applicable type converter found or null if no match could be found

cacheTypeConverterClass

protected Class<? extends TypeConverter<?>> cacheTypeConverterClass(Class<?> clazz,
                                                                    Class<? extends TypeConverter<?>> converterClass)
Add converter class converterClass for converting objects of type clazz.

Parameters:
clazz - the type of object being converted
converterClass - the class of the converter
Returns:
the targetType parameter

clearCache

protected void clearCache()
Clear the instance cache. This is called by add(Class, Class).


getInstance

public TypeConverter getInstance(Class<? extends TypeConverter> clazz,
                                 Locale locale)
                          throws Exception
Gets an instance of the TypeConverter class specified.

Specified by:
getInstance in interface TypeConverterFactory
Parameters:
clazz - the TypeConverter type that is desired
locale - the locale of the Strings to be converted with the returned converter
Returns:
an instance of the TypeConverter specified
Throws:
Exception - if there is a problem instantiating the TypeConverter


? Copyright 2005-2006, Stripes Development Team.