public class DefaultFormatterFactory extends 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(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.
|
public void init(Configuration configuration) throws Exception
init in interface ConfigurableComponentconfiguration - the Configuration object being used by StripesException - should be thrown if the component cannot be configured well enough to use.protected Configuration getConfiguration()
protected Map<Class<?>,Class<? extends Formatter<?>>> getFormatters()
public void add(Class<?> targetType, Class<? extends Formatter<?>> formatterClass)
add in interface FormatterFactorytargetType - 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(Class<?> clazz, Locale locale, String formatType, String formatPattern)
getFormatter in interface FormatterFactoryclazz - 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 Class<? extends Formatter<?>> findFormatterClass(Class<?> targetClass)
targetClass.targetClass - the class of the object that needs to be formattedprotected Class<? extends Formatter<?>> findInSuperclasses(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 Class<? extends Formatter<?>> findInInterfaces(Class<?> targetClass, 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 Class<? extends Formatter<?>> cacheFormatterClass(Class<?> clazz, 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(Class<? extends Formatter<?>> clazz, String formatType, String formatPattern, Locale locale) throws Exception
clazz - the Formatter type that is desiredException - if there is a problem instantiating the Formatter© Copyright 2005-2006, Stripes Development Team.