net.sf.jsefa.common.annotation
Class TypeMappingFactory<N,R extends TypeMappingRegistry<N>>

java.lang.Object
  extended by net.sf.jsefa.common.annotation.TypeMappingFactory<N,R>
Type Parameters:
N - the type of the data type name
R - the type of the type mapping registry
Direct Known Subclasses:
RbfTypeMappingFactory, XmlTypeMappingFactory

public abstract class TypeMappingFactory<N,R extends TypeMappingRegistry<N>>
extends Object

Abstract super class for factories which can create TypeMappings from annotated classes.

All subclasses should be thread-safe.

Author:
Norman Lahme-Huetig

Nested Class Summary
protected  class TypeMappingFactory.TypeMappingPlaceholder
          A placeholder for a type mapping used during the construction of a type mapping, i. e. between calling prepareToCreate(Class, Object) and the registration of the type mapping.
 
Constructor Summary
TypeMappingFactory(R typeMappingRegistry, SimpleTypeConverterProvider simpleTypeConverterProvider, ValidatorProvider validatorProvider, ObjectAccessorProvider objectAccessorProvider)
          Constructs a new TypeMappingFactory.
 
Method Summary
protected  void assertHasSimpleType(Class<?> objectType)
          Asserts that a given object type is a simple type.
protected  void assertNoCollectionType(Class<?> objectType)
          Asserts that a given object type is not a collection class.
protected  void assertTypeMappingExists(N dataTypeName)
          Asserts that a type mapping exists for the given data type name.
protected  void assertTypeMappingIsSimple(N dataTypeName)
          Asserts that a type mapping exists for the given data type name and is simple.
abstract  N createIfAbsent(Class<?> objectType)
          Creates a type mapping for the given object type, registers it with the type mapping registry and returns its data type name.
protected  SimpleTypeConverter createSimpleTypeConverter(Class<?> objectType, Field field, Annotation annotation)
          Creates a simple type converter.
protected abstract  N getAnnotatedDataTypeName(Annotation annotation, Class<?> annotationContextClass)
          Returns the data type name as declared through the given annotation.
protected  Class<?> getCollectionItemType(Annotation annotation, Field field, boolean fromFieldDeclarationAllowed)
          Returns the type of the items of a collection.
protected  Class<?> getMapKeyType(Annotation annotation, Field field)
          Returns the type of the keys of a map.
protected  Class<?> getMapValueType(Annotation annotation, Field field, boolean fromFieldDeclarationAllowed)
          Returns the type of the values of a map.
protected  ObjectAccessorProvider getObjectAccessorProvider()
          Returns the object accessor provider.
protected  SimpleTypeConverterProvider getSimpleTypeConverterProvider()
          Returns the simple type converter provider.
 R getTypeMappingRegistry()
          Returns the type mapping registry.
protected  ValidatorFactory getValidatorFactory()
          Returns the validator factory.
protected  ValidatorProvider getValidatorProvider()
          Returns the validator provider.
protected  boolean hasCollectionType(Class<?> objectType)
          Returns true if and only if the given object type is a collection type.
protected  boolean hasMapType(Class<?> objectType)
          Returns true if and only if the given object type is a map type.
protected  boolean hasSimpleType(Class<?> objectType)
          Returns true if and only if the given object type is a simple type.
protected  boolean prepareToCreate(Class<?> objectType, N dataTypeName)
          Called before creating a new type mapping with the given data type name for the given object type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TypeMappingFactory

public TypeMappingFactory(R typeMappingRegistry,
                          SimpleTypeConverterProvider simpleTypeConverterProvider,
                          ValidatorProvider validatorProvider,
                          ObjectAccessorProvider objectAccessorProvider)
Constructs a new TypeMappingFactory.

Parameters:
typeMappingRegistry - the type mapping registry. New types will be registered using that registry.
simpleTypeConverterProvider - the simple type converter provider to use
validatorProvider - the validator provider to use
objectAccessorProvider - the object accessor provider to use
Method Detail

createIfAbsent

public abstract N createIfAbsent(Class<?> objectType)
Creates a type mapping for the given object type, registers it with the type mapping registry and returns its data type name. The first two steps are omitted if a type mapping is already registered for the given object type.

Parameters:
objectType - the object type to create a type mapping for.
Returns:
the name of the created or found data type.

getTypeMappingRegistry

public final R getTypeMappingRegistry()
Returns the type mapping registry.

Returns:
the type mapping registry.

prepareToCreate

protected final boolean prepareToCreate(Class<?> objectType,
                                        N dataTypeName)
Called before creating a new type mapping with the given data type name for the given object type. Returns true, if the type mapping registry has no entry for the data type name and if it is the first time this method is called with the given argument; otherwise false.

The purpose of this method is to prevent from creating duplicates and from falling in an endless loop in case of a cycle in the type mapping graph.

In case the method returns true, a TypeMappingFactory.TypeMappingPlaceholder is registered so that object type information can be retrieved during the construction of a type mapping. That type mapping is replaced by the real type mapping after finishing its construction.

Parameters:
objectType - the object type
dataTypeName - the data type name
Returns:
true, if no type mapping with the given name already exists or is already under construction.

getObjectAccessorProvider

protected final ObjectAccessorProvider getObjectAccessorProvider()
Returns the object accessor provider.

Returns:
the object accessor provider.

getSimpleTypeConverterProvider

protected final SimpleTypeConverterProvider getSimpleTypeConverterProvider()
Returns the simple type converter provider.

Returns:
the simple type converter provider.

getValidatorProvider

protected final ValidatorProvider getValidatorProvider()
Returns the validator provider.

Returns:
the validator provider

createSimpleTypeConverter

protected final SimpleTypeConverter createSimpleTypeConverter(Class<?> objectType,
                                                              Field field,
                                                              Annotation annotation)
Creates a simple type converter.

Parameters:
objectType - the object type to create a converter for
field - the field to create a converter for. May be null.
annotation - the annotation providing parameters for constructing the converter. May be null.
Returns:
a simple type converter

getCollectionItemType

protected final Class<?> getCollectionItemType(Annotation annotation,
                                               Field field,
                                               boolean fromFieldDeclarationAllowed)
Returns the type of the items of a collection.

Parameters:
annotation - the annotation of the field
field - the field
fromFieldDeclarationAllowed - true, if the field is a collection which contains only values of one type so that the type may be deduced from the actual parameter type of the parameterized type of the field; false otherwise.
Returns:
an object type

getMapValueType

protected final Class<?> getMapValueType(Annotation annotation,
                                         Field field,
                                         boolean fromFieldDeclarationAllowed)
Returns the type of the values of a map.

Parameters:
annotation - the annotation of the field
field - the field
fromFieldDeclarationAllowed - true, if the field is a map which contains only values of one type so that the type may be deduced from the actual parameter type of the parameterized type of the field; false otherwise.
Returns:
an object type

getMapKeyType

protected final Class<?> getMapKeyType(Annotation annotation,
                                       Field field)
Returns the type of the keys of a map.

Parameters:
annotation - the annotation of the field
field - the field
Returns:
an object type

getAnnotatedDataTypeName

protected abstract N getAnnotatedDataTypeName(Annotation annotation,
                                              Class<?> annotationContextClass)
Returns the data type name as declared through the given annotation.

Parameters:
annotation - the annotation
annotationContextClass - the context class of the annotation. This class may be needed to interpret the annotation data.
Returns:
a data type name

getValidatorFactory

protected final ValidatorFactory getValidatorFactory()
Returns the validator factory.

Returns:
the validator factory

hasSimpleType

protected final boolean hasSimpleType(Class<?> objectType)
Returns true if and only if the given object type is a simple type.

Parameters:
objectType - the object type
Returns:
true, if the object type is a simple type; false otherwise.

hasCollectionType

protected final boolean hasCollectionType(Class<?> objectType)
Returns true if and only if the given object type is a collection type.

Parameters:
objectType - the object type
Returns:
true, if the given object type is a collection type; false otherwise.

hasMapType

protected final boolean hasMapType(Class<?> objectType)
Returns true if and only if the given object type is a map type.

Parameters:
objectType - the object type
Returns:
true, if the given object type is a map type; false otherwise.

assertTypeMappingExists

protected final void assertTypeMappingExists(N dataTypeName)
Asserts that a type mapping exists for the given data type name.

Parameters:
dataTypeName - the data type name.
Throws:
AnnotationException - if the assertion fails.

assertTypeMappingIsSimple

protected final void assertTypeMappingIsSimple(N dataTypeName)
Asserts that a type mapping exists for the given data type name and is simple.

Parameters:
dataTypeName - the data type name.
Throws:
AnnotationException - if the assertion fails.

assertNoCollectionType

protected final void assertNoCollectionType(Class<?> objectType)
Asserts that a given object type is not a collection class.

Parameters:
objectType - the object type to check
Throws:
AnnotationException - if the assertion fails.

assertHasSimpleType

protected final void assertHasSimpleType(Class<?> objectType)
Asserts that a given object type is a simple type.

Parameters:
objectType - the object type to check
Throws:
AnnotationException - if the assertion fails.


Copyright © 2014. All rights reserved.