net.sourceforge.stripes.format
Class DefaultFormatterFactory

java.lang.Object
  extended by net.sourceforge.stripes.format.DefaultFormatterFactory
All Implemented Interfaces:
ConfigurableComponent, FormatterFactory

public class DefaultFormatterFactory
extends Object
implements FormatterFactory

Implementation of FormatterFactory that contains a set of built-in formatters. Additional formatters can be registered by calling add(Class, Class). If there is no registered formatter for a specific class, then it attempts to find the best available formatter by searching for a match against the target implemented interfaces, class's superclasses, and interface superclasses.

Author:
Tim Fennell

Constructor Summary
DefaultFormatterFactory()
           
 
Method Summary
 void add(Class<?> targetType, Class<? extends Formatter<?>> formatterClass)
          Adds a Formatter to the set of registered Formatters, overriding an existing formatter if one was registered for the type.
protected  Class<? extends Formatter<?>> cacheFormatterClass(Class<?> clazz, Class<? extends Formatter<?>> formatterClass)
          Add formatter class formatterClass for formatting objects of type clazz.
protected  void clearCache()
          Clear the class and instance caches.
protected  Class<? extends Formatter<?>> findFormatterClass(Class<?> targetClass)
          Search for a formatter class that best matches the requested class, first checking the specified class, then all the interfaces it implements, then all its superclasses and the interfaces they implement, and finally all the superclasses of the interfaces implemented by targetClass.
protected  Class<? extends Formatter<?>> findInInterfaces(Class<?> targetClass, Class<?>... ifaces)
          Called second by findFormatterClass(Class), after findInSuperclasses(Class).
protected  Class<? extends Formatter<?>> findInSuperclasses(Class<?> targetClass)
          Called first by findFormatterClass(Class).
protected  Configuration getConfiguration()
          Allows subclasses to access the stored configuration if needed.
 Formatter<?> getFormatter(Class<?> clazz, Locale locale, String formatType, String formatPattern)
          Check to see if the there is a Formatter for the specified clazz.
protected  Map<Class<?>,Class<? extends Formatter<?>>> getFormatters()
          Gets the (rather confusing) Map of Formatter objects.
 Formatter<?> getInstance(Class<? extends Formatter<?>> clazz, String formatType, String formatPattern, Locale locale)
          Gets an instance of the Formatter class specified.
 void init(Configuration configuration)
          Stores a reference to the configuration and configures the default formatters.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DefaultFormatterFactory

public DefaultFormatterFactory()
Method Detail

init

public void init(Configuration configuration)
          throws Exception
Stores a reference to the configuration and configures the default formatters.

Specified by:
init in interface ConfigurableComponent
Parameters:
configuration - the Configuration object being used by Stripes
Throws:
Exception - should be thrown if the component cannot be configured well enough to use.

getConfiguration

protected Configuration getConfiguration()
Allows subclasses to access the stored configuration if needed.


getFormatters

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

Returns:
the Map of Formatter classes

add

public void add(Class<?> targetType,
                Class<? extends Formatter<?>> formatterClass)
Adds a Formatter to the set of registered Formatters, overriding an existing formatter if one was registered for the type.

Specified by:
add in interface FormatterFactory
Parameters:
targetType - the type for which the formatter will handle formatting
formatterClass - the implementation class that will handle the formatting

clearCache

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


getFormatter

public Formatter<?> getFormatter(Class<?> clazz,
                                 Locale locale,
                                 String formatType,
                                 String formatPattern)
Check to see if the there is a Formatter for the specified clazz. If a Formatter is found an instance is created, configured and returned. Otherwise returns null.

Specified by:
getFormatter in interface FormatterFactory
Parameters:
clazz - the type of object being formatted
locale - the Locale into which the object should be formatted
formatType - the type of output to produce (e.g. date, time etc.)
formatPattern - a named format string, or a format pattern
Returns:
Formatter an instance of a Formatter, or null

findFormatterClass

protected Class<? extends Formatter<?>> findFormatterClass(Class<?> targetClass)
Search for a formatter class that best matches the requested class, first checking the specified class, then all the interfaces it implements, then all its superclasses and the interfaces they implement, and finally all the superclasses of the interfaces implemented by targetClass.

Parameters:
targetClass - the class of the object that needs to be formatted
Returns:
the best applicable formatter

findInSuperclasses

protected Class<? extends Formatter<?>> findInSuperclasses(Class<?> targetClass)
Called first by findFormatterClass(Class). Search for a formatter class that best matches the requested class, first checking the specified class, second all the interfaces it implements, third annotations. If no match is found, repeat the process for each superclass.

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

findInInterfaces

protected Class<? extends Formatter<?>> findInInterfaces(Class<?> targetClass,
                                                         Class<?>... ifaces)
Called second by findFormatterClass(Class), after findInSuperclasses(Class). Search for a formatter class that best matches the requested class by checking the superclasses of every interface implemented by targetClass.

Parameters:
targetClass - the class of the object that needs to be formatted
ifaces - an array of interfaces to search
Returns:
the first applicable formatter found or null if no match could be found

cacheFormatterClass

protected Class<? extends Formatter<?>> cacheFormatterClass(Class<?> clazz,
                                                            Class<? extends Formatter<?>> formatterClass)
Add formatter class formatterClass for formatting objects of type clazz.

Parameters:
clazz - the type of object being formatted
formatterClass - the class of the formatter
Returns:
the targetType parameter

getInstance

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

Parameters:
clazz - the Formatter type that is desired
Returns:
an instance of the Formatter specified
Throws:
Exception - if there is a problem instantiating the Formatter


? Copyright 2005-2006, Stripes Development Team.