Class TypeHandlerFactory
- java.lang.Object
-
- org.beanio.internal.util.TypeHandlerFactory
-
public class TypeHandlerFactory extends java.lang.Object
A factory class used to get aTypeHandler
for parsing field text into field objects, and for formatting field objects into field text.A
TypeHandler
is registered and retrieved by class, type alias, or name. If a stream format is specified when registering a type handler by class or type alias, the type handler will only be returned when the same format is queried for. In most cases, registering a type handler by type alias has the same effect as registering the type handler using the target class associated with the alias. There are two exceptions: type handlers can be specifically registered for 'date
' and 'time
' type aliases without overriding the default Date type handler, which is registered for the classjava.util.Date
and type alias 'datetime
'.If a registered type handler implements the
ConfigurableTypeHandler
interface, handler properties can be overridden using aProperties
object. When the type handler is retrieved, the factory callsConfigurableTypeHandler.newInstance(Properties)
to allow the type handler to return a customized version of itself.By default, a
TypeHandlerFactory
holds a reference to a parent factory. If a factory cannot find a type handler, its parent will be checked recursively until there is no parent left to check.- Since:
- 1.0
- See Also:
TypeHandler
,ConfigurableTypeHandler
-
-
Constructor Summary
Constructors Constructor Description TypeHandlerFactory()
Constructs a newTypeHandlerFactory
using the default type handler factory for its parent and the sameClassLoader
that loaded this class.TypeHandlerFactory(java.lang.ClassLoader classLoader)
Constructs a newTypeHandlerFactory
using the default type handler factory for its parent.TypeHandlerFactory(java.lang.ClassLoader classLoader, TypeHandlerFactory parent)
Constructs a newTypeHandlerFactory
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static TypeHandlerFactory
getDefault()
Returns the defaultTypeHandlerFactory
.TypeHandler
getTypeHandler(java.lang.String name)
Returns a named type handler, ornull
if there is no type handler configured for the given name in this factory or any of its ancestors.TypeHandler
getTypeHandler(java.lang.String name, java.util.Properties properties)
Returns a named type handler, ornull
if there is no type handler configured for the given name in this factory or any of its ancestors.TypeHandler
getTypeHandlerFor(java.lang.Class<?> clazz)
Returns a type handler for a class, ornull
if there is no type handler configured for the class in this factory or any of its ancestorsTypeHandler
getTypeHandlerFor(java.lang.Class<?> clazz, java.lang.String format, java.util.Properties properties)
Returns a type handler for a class, ornull
if there is no type handler configured for the class in this factory or any of its ancestorsTypeHandler
getTypeHandlerFor(java.lang.String type)
Returns the type handler for the given type, ornull
if there is no type handler configured for the type in this factory or any of its ancestors.TypeHandler
getTypeHandlerFor(java.lang.String type, java.lang.String format)
Returns the type handler for the given type and format, ornull
if there is no type handler configured for the type in this factory or any of its ancestors.TypeHandler
getTypeHandlerFor(java.lang.String type, java.lang.String format, java.util.Properties properties)
Returns the type handler for the given type, ornull
if there is no type handler configured for the type in this factory or any of its ancestors.void
registerHandler(java.lang.String name, TypeHandler handler)
Registers a type handler in this factory.void
registerHandlerFor(java.lang.Class<?> clazz, TypeHandler handler)
Registers a type handler in this factory for any stream format.void
registerHandlerFor(java.lang.Class<?> clazz, TypeHandler handler, java.lang.String format)
Registers a type handler in this factory for a specific stream format.void
registerHandlerFor(java.lang.String type, TypeHandler handler)
Registers a type handler in this factory by class type for all stream formatsvoid
registerHandlerFor(java.lang.String type, TypeHandler handler, java.lang.String format)
Registers a type handler in this factory by class type for a specific stream format.
-
-
-
Constructor Detail
-
TypeHandlerFactory
public TypeHandlerFactory()
Constructs a newTypeHandlerFactory
using the default type handler factory for its parent and the sameClassLoader
that loaded this class.
-
TypeHandlerFactory
public TypeHandlerFactory(java.lang.ClassLoader classLoader)
Constructs a newTypeHandlerFactory
using the default type handler factory for its parent.- Parameters:
classLoader
- theClassLoader
for resolving unrecognized types- Since:
- 2.0
-
TypeHandlerFactory
public TypeHandlerFactory(java.lang.ClassLoader classLoader, TypeHandlerFactory parent)
Constructs a newTypeHandlerFactory
.- Parameters:
classLoader
- theClassLoader
for resolving unrecognized typesparent
- the parentTypeHandlerFactory
- Since:
- 2.0
-
-
Method Detail
-
getTypeHandler
public TypeHandler getTypeHandler(java.lang.String name)
Returns a named type handler, ornull
if there is no type handler configured for the given name in this factory or any of its ancestors.- Parameters:
name
- the name of type handler was registered under- Returns:
- the type handler, or
null
if there is no configured type handler registered for the name
-
getTypeHandler
public TypeHandler getTypeHandler(java.lang.String name, java.util.Properties properties) throws java.lang.IllegalArgumentException
Returns a named type handler, ornull
if there is no type handler configured for the given name in this factory or any of its ancestors.- Parameters:
name
- the name the type handler was registered underproperties
- the custom properties for configuring the type handler- Returns:
- the type handler, or
null
if there is no configured type handler registered for the name - Throws:
java.lang.IllegalArgumentException
- if a custom property value was invalid
-
getTypeHandlerFor
public TypeHandler getTypeHandlerFor(java.lang.String type)
Returns the type handler for the given type, ornull
if there is no type handler configured for the type in this factory or any of its ancestors.- Parameters:
type
- the class name or type alias- Returns:
- the type handler, or
null
if there is no configured type handler registered for the type
-
getTypeHandlerFor
public TypeHandler getTypeHandlerFor(java.lang.String type, java.lang.String format)
Returns the type handler for the given type and format, ornull
if there is no type handler configured for the type in this factory or any of its ancestors.- Parameters:
type
- the class name or type aliasformat
- the stream format, or if null, format specific handlers will not be returned- Returns:
- the type handler, or
null
if there is no configured type handler registered for the type - Since:
- 2.0
-
getTypeHandlerFor
public TypeHandler getTypeHandlerFor(java.lang.String type, java.lang.String format, java.util.Properties properties) throws java.lang.IllegalArgumentException
Returns the type handler for the given type, ornull
if there is no type handler configured for the type in this factory or any of its ancestors.- Parameters:
type
- the property typeformat
- the stream format, or if null, format specific handlers will not be returnedproperties
- the custom properties for configuring the type handler- Returns:
- the type handler, or
null
if there is no configured type handler registered for the type - Throws:
java.lang.IllegalArgumentException
- if a custom property value was invalid- Since:
- 2.0
-
getTypeHandlerFor
public TypeHandler getTypeHandlerFor(java.lang.Class<?> clazz)
Returns a type handler for a class, ornull
if there is no type handler configured for the class in this factory or any of its ancestors- Parameters:
clazz
- the target class to find a type handler for- Returns:
- the type handler, or null if the class is not supported
-
getTypeHandlerFor
public TypeHandler getTypeHandlerFor(java.lang.Class<?> clazz, java.lang.String format, java.util.Properties properties) throws java.lang.IllegalArgumentException
Returns a type handler for a class, ornull
if there is no type handler configured for the class in this factory or any of its ancestors- Parameters:
clazz
- the target class to find a type handler forformat
- the stream format, or if null, format specific handlers will not be returnedproperties
- the custom properties for configuring the type handler- Returns:
- the type handler, or null if the class is not supported
- Throws:
java.lang.IllegalArgumentException
- if a custom property value was invalid- Since:
- 2.0
-
registerHandler
public void registerHandler(java.lang.String name, TypeHandler handler)
Registers a type handler in this factory.- Parameters:
name
- the name to register the type handler underhandler
- the type handler to register
-
registerHandlerFor
public void registerHandlerFor(java.lang.String type, TypeHandler handler) throws java.lang.IllegalArgumentException
Registers a type handler in this factory by class type for all stream formats- Parameters:
type
- the fully qualified class name or type alias to register the type handler forhandler
- the type handler to register- Throws:
java.lang.IllegalArgumentException
- if the type name is invalid or if the handler type is not assignable from the type
-
registerHandlerFor
public void registerHandlerFor(java.lang.String type, TypeHandler handler, java.lang.String format) throws java.lang.IllegalArgumentException
Registers a type handler in this factory by class type for a specific stream format.- Parameters:
type
- the fully qualified class name or type alias to register the type handler forhandler
- the type handler to registerformat
- the stream format to register the type handler for, or if null the type handler may be returned for any format- Throws:
java.lang.IllegalArgumentException
- if the type name is invalid or if the handler type is not assignable from the type- Since:
- 2.0
-
registerHandlerFor
public void registerHandlerFor(java.lang.Class<?> clazz, TypeHandler handler) throws java.lang.IllegalArgumentException
Registers a type handler in this factory for any stream format.- Parameters:
clazz
- the target class to register the type handler forhandler
- the type handler to register- Throws:
java.lang.IllegalArgumentException
- if the handler type is not assignable from the registered class type
-
registerHandlerFor
public void registerHandlerFor(java.lang.Class<?> clazz, TypeHandler handler, java.lang.String format) throws java.lang.IllegalArgumentException
Registers a type handler in this factory for a specific stream format.- Parameters:
clazz
- the target class to register the type handler forhandler
- the type handler to registerformat
- the stream format to register the type handler for, or if null the type handler may be returned for any format- Throws:
java.lang.IllegalArgumentException
- if the handler type is not assignable from the registered class type
-
getDefault
public static TypeHandlerFactory getDefault()
Returns the defaultTypeHandlerFactory
.- Returns:
- the default
TypeHandlerFactory
-
-