public class DefaultFormatterFactory extends java.lang.Object implements FormatterFactory
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.Constructor and Description |
---|
DefaultFormatterFactory() |
Modifier and Type | Method and Description |
---|---|
void |
add(java.lang.Class<?> targetType,
java.lang.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 java.lang.Class<? extends Formatter<?>> |
cacheFormatterClass(java.lang.Class<?> clazz,
java.lang.Class<? extends Formatter<?>> formatterClass)
Add formatter class
formatterClass for formatting objects of type clazz . |
protected void |
clearCache()
Clear the class and instance caches.
|
protected java.lang.Class<? extends Formatter<?>> |
findFormatterClass(java.lang.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 java.lang.Class<? extends Formatter<?>> |
findInInterfaces(java.lang.Class<?> targetClass,
java.lang.Class<?>... ifaces)
Called second by
findFormatterClass(Class) , after
findInSuperclasses(Class) . |
protected java.lang.Class<? extends Formatter<?>> |
findInSuperclasses(java.lang.Class<?> targetClass)
Called first by
findFormatterClass(Class) . |
protected Configuration |
getConfiguration()
Allows subclasses to access the stored configuration if needed.
|
Formatter<?> |
getFormatter(java.lang.Class<?> clazz,
java.util.Locale locale,
java.lang.String formatType,
java.lang.String formatPattern)
Check to see if the there is a Formatter for the specified clazz.
|
protected java.util.Map<java.lang.Class<?>,java.lang.Class<? extends Formatter<?>>> |
getFormatters()
Gets the (rather confusing) Map of Formatter objects.
|
Formatter<?> |
getInstance(java.lang.Class<? extends Formatter<?>> clazz,
java.lang.String formatType,
java.lang.String formatPattern,
java.util.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.
|
public void init(Configuration configuration) throws java.lang.Exception
init
in interface ConfigurableComponent
configuration
- the Configuration object being used by Stripesjava.lang.Exception
- should be thrown if the component cannot be configured well enough to use.protected Configuration getConfiguration()
protected java.util.Map<java.lang.Class<?>,java.lang.Class<? extends Formatter<?>>> getFormatters()
public void add(java.lang.Class<?> targetType, java.lang.Class<? extends Formatter<?>> formatterClass)
add
in interface FormatterFactory
targetType
- the type for which the formatter will handle formattingformatterClass
- the implementation class that will handle the formattingprotected void clearCache()
add(Class, Class)
.public Formatter<?> getFormatter(java.lang.Class<?> clazz, java.util.Locale locale, java.lang.String formatType, java.lang.String formatPattern)
getFormatter
in interface FormatterFactory
clazz
- the type of object being formattedlocale
- the Locale into which the object should be formattedformatType
- the type of output to produce (e.g. date, time etc.)formatPattern
- a named format string, or a format patternprotected java.lang.Class<? extends Formatter<?>> findFormatterClass(java.lang.Class<?> targetClass)
targetClass
.targetClass
- the class of the object that needs to be formattedprotected java.lang.Class<? extends Formatter<?>> findInSuperclasses(java.lang.Class<?> targetClass)
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.targetClass
- the class of the object that needs to be formattedprotected java.lang.Class<? extends Formatter<?>> findInInterfaces(java.lang.Class<?> targetClass, java.lang.Class<?>... ifaces)
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
.targetClass
- the class of the object that needs to be formattedifaces
- an array of interfaces to searchprotected java.lang.Class<? extends Formatter<?>> cacheFormatterClass(java.lang.Class<?> clazz, java.lang.Class<? extends Formatter<?>> formatterClass)
formatterClass
for formatting objects of type clazz
.clazz
- the type of object being formattedformatterClass
- the class of the formattertargetType
parameterpublic Formatter<?> getInstance(java.lang.Class<? extends Formatter<?>> clazz, java.lang.String formatType, java.lang.String formatPattern, java.util.Locale locale) throws java.lang.Exception
clazz
- the Formatter type that is desiredjava.lang.Exception
- if there is a problem instantiating the Formatter? Copyright 2005-2006, Stripes Development Team.