org.springframework.ui.format.support
Class GenericFormatterRegistry

java.lang.Object
  extended by org.springframework.ui.format.support.GenericFormatterRegistry
All Implemented Interfaces:
Cloneable, ApplicationContextAware, FormatterRegistry

public class GenericFormatterRegistry
extends Object
implements FormatterRegistry, ApplicationContextAware, Cloneable

A generic implementation of FormatterRegistry suitable for use in most environments.

Since:
3.0
Author:
Keith Donald, Juergen Hoeller
See Also:
setConversionService(ConversionService), #add(org.springframework.ui.format.Formatter), #add(Class, org.springframework.ui.format.Formatter), #add(org.springframework.ui.format.AnnotationFormatterFactory)

Constructor Summary
GenericFormatterRegistry()
           
 
Method Summary
<A extends Annotation,T>
void
addFormatterByAnnotation(AnnotationFormatterFactory<A,T> factory)
          Adds a AnnotationFormatterFactory that returns the Formatter for properties annotated with a specific annotation.
 void addFormatterByAnnotation(Class<? extends Annotation> annotationType, Formatter<?> formatter)
          Adds a Formatter to this registry indexed by the given annotation type.
 void addFormatterByType(Class<?> type, Formatter<?> formatter)
          Adds a Formatter to this registry indexed by type.
<T> void
addFormatterByType(Formatter<T> formatter)
          Adds a Formatter to this registry indexed by <T>.
 GenericFormatterRegistry clone()
          Create an independent clone of this FormatterRegistry.
 ConversionService getConversionService()
          Return the type conversion service which this FormatterRegistry delegates to.
<T> Formatter<T>
getFormatter(Class<T> targetType)
          Get the Formatter for the specified type.
 Formatter<Object> getFormatter(TypeDescriptor type)
          Get the Formatter for the type descriptor.
 boolean isShared()
          Return whether this FormatterRegistry is shared, in which case newly registered Formatters will be visible to other callers as well.
 void setAnnotationFormatterFactories(Set<AnnotationFormatterFactory<?,?>> factories)
          Registers the annotation formatter factories in the set provided.
 void setAnnotationFormatterMap(Map<Class<? extends Annotation>,Formatter<?>> formatters)
          Registers the formatters in the map provided by annotation type.
 void setApplicationContext(ApplicationContext context)
          Take the context's default ConversionService if none specified locally.
 void setConversionService(ConversionService conversionService)
          Specify the type conversion service that will be used to coerce objects to the types required for formatting.
 void setFormatterMap(Map<Class<?>,Formatter<?>> formatters)
          Registers the formatters in the map provided by type.
 void setFormatters(Set<Formatter<?>> formatters)
          Registers the formatters in the set provided.
 void setShared(boolean shared)
          Specify whether this FormatterRegistry is shared, in which case newly registered Formatters will be visible to other callers as well.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GenericFormatterRegistry

public GenericFormatterRegistry()
Method Detail

setFormatters

public void setFormatters(Set<Formatter<?>> formatters)
Registers the formatters in the set provided. JavaBean-friendly alternative to calling addFormatterByType(Formatter).

See Also:
#add(Formatter)

setFormatterMap

public void setFormatterMap(Map<Class<?>,Formatter<?>> formatters)
Registers the formatters in the map provided by type. JavaBean-friendly alternative to calling addFormatterByType(Class, Formatter).

See Also:
#add(Class, Formatter)

setAnnotationFormatterMap

public void setAnnotationFormatterMap(Map<Class<? extends Annotation>,Formatter<?>> formatters)
Registers the formatters in the map provided by annotation type. JavaBean-friendly alternative to calling addFormatterByAnnotation(Class, Formatter).

See Also:
#add(Class, Formatter)

setAnnotationFormatterFactories

public void setAnnotationFormatterFactories(Set<AnnotationFormatterFactory<?,?>> factories)
Registers the annotation formatter factories in the set provided. JavaBean-friendly alternative to calling addFormatterByAnnotation(AnnotationFormatterFactory).

See Also:
#add(AnnotationFormatterFactory)

setConversionService

public void setConversionService(ConversionService conversionService)
Specify the type conversion service that will be used to coerce objects to the types required for formatting. Defaults to a DefaultConversionService.

See Also:
addFormatterByType(Class, Formatter)

getConversionService

public ConversionService getConversionService()
Return the type conversion service which this FormatterRegistry delegates to.


setApplicationContext

public void setApplicationContext(ApplicationContext context)
Take the context's default ConversionService if none specified locally.

Specified by:
setApplicationContext in interface ApplicationContextAware
Parameters:
context - the ApplicationContext object to be used by this object
See Also:
BeanInitializationException

setShared

public void setShared(boolean shared)
Specify whether this FormatterRegistry is shared, in which case newly registered Formatters will be visible to other callers as well.

A new GenericFormatterRegistry is considered as shared by default, whereas a cloned GenericFormatterRegistry will be non-shared by default.

See Also:
clone()

isShared

public boolean isShared()
Return whether this FormatterRegistry is shared, in which case newly registered Formatters will be visible to other callers as well.


clone

public GenericFormatterRegistry clone()
Create an independent clone of this FormatterRegistry.

Overrides:
clone in class Object
See Also:
setShared(boolean)

addFormatterByType

public void addFormatterByType(Class<?> type,
                               Formatter<?> formatter)
Description copied from interface: FormatterRegistry
Adds a Formatter to this registry indexed by type.

Use this add method when type differs from <T>. Calling getFormatter(type) returns a decorator that wraps the targetFormatter instance.

On format, the decorator first coerses the instance of type to <T>, then delegates to targetFormatter to format the value.

On parse, the decorator first delegates to the formatter to parse a <T>, then coerces the parsed value to type.

Specified by:
addFormatterByType in interface FormatterRegistry
Parameters:
type - the object type
formatter - the target formatter

addFormatterByType

public <T> void addFormatterByType(Formatter<T> formatter)
Description copied from interface: FormatterRegistry
Adds a Formatter to this registry indexed by <T>. Calling getFormatter(<T>.class) returns formatter.

Specified by:
addFormatterByType in interface FormatterRegistry
Type Parameters:
T - the type of object the formatter formats
Parameters:
formatter - the formatter

addFormatterByAnnotation

public void addFormatterByAnnotation(Class<? extends Annotation> annotationType,
                                     Formatter<?> formatter)
Description copied from interface: FormatterRegistry
Adds a Formatter to this registry indexed by the given annotation type. Calling getFormatter(...) on a field or accessor method with the given annotation returns formatter.

Specified by:
addFormatterByAnnotation in interface FormatterRegistry
formatter - the formatter

addFormatterByAnnotation

public <A extends Annotation,T> void addFormatterByAnnotation(AnnotationFormatterFactory<A,T> factory)
Description copied from interface: FormatterRegistry
Adds a AnnotationFormatterFactory that returns the Formatter for properties annotated with a specific annotation. Calling getFormatter(...) on a field or accessor method with the given annotation returns formatter.

Specified by:
addFormatterByAnnotation in interface FormatterRegistry
Type Parameters:
A - the type of Annotation this factory uses to create Formatter instances
T - the type of object that the factory's Formatters are dealing with
Parameters:
factory - the annotation formatter factory

getFormatter

public <T> Formatter<T> getFormatter(Class<T> targetType)
Description copied from interface: FormatterRegistry
Get the Formatter for the specified type.

Specified by:
getFormatter in interface FormatterRegistry
Returns:
the Formatter, or null if no suitable one is registered

getFormatter

public Formatter<Object> getFormatter(TypeDescriptor type)
Description copied from interface: FormatterRegistry
Get the Formatter for the type descriptor.

Specified by:
getFormatter in interface FormatterRegistry
Returns:
the Formatter, or null if no suitable one is registered