ma.glasnost.orika.impl
Class DefaultMapperFactory

java.lang.Object
  extended by ma.glasnost.orika.impl.DefaultMapperFactory
All Implemented Interfaces:
MapperFactory

public class DefaultMapperFactory
extends Object
implements MapperFactory

The mapper factory is the heart of Orika, a small container where metadata are stored, it's used by other components, to look up for generated mappers, converters, object factories ... etc.

Author:
S.M. El Aatifi

Nested Class Summary
static class DefaultMapperFactory.Builder
          Use this builder to generate instances of DefaultMapperFactory with the desired customizations.

For example, an instance with no customizations could be generated with the following code:
static class DefaultMapperFactory.MapperFactoryBuilder<F extends DefaultMapperFactory,B extends DefaultMapperFactory.MapperFactoryBuilder<F,B>>
          MapperFactoryBuilder provides an extensible Builder definition usable for providing your own Builder class for subclasses of DefaultMapperFactory.

See the defined DefaultMapperFactory.MapperFactoryBuilder below for example of how to subclass.
 
Constructor Summary
protected DefaultMapperFactory(DefaultMapperFactory.MapperFactoryBuilder<?,?> builder)
          Constructs a new instance of DefaultMapperFactory
 
Method Summary
protected  void addClassMapBuilderFactory(ClassMapBuilderFactory factory)
          Add factory to the factories chain
 void build()
          Builds this MapperFactory.
protected  MapperFacade buildMapperFacade(MappingContextFactory contextFactory, UnenhanceStrategy unenhanceStrategy)
          Builds the MapperFacade for this factory.
protected  UnenhanceStrategy buildUnenhanceStrategy(UnenhanceStrategy unenhanceStrategy, SuperTypeResolverStrategy superTypeStrategy)
          Generates the UnenhanceStrategy to be used for this MapperFactory, applying the passed delegateStrategy if not null.
This allows the MapperFactory a chance to fill in the unenhance strategy with references to other parts of the factory (registered mappers, converters, object factories) which may be important in the "unenhancing" process.
<A,B> ClassMapBuilder<A,B>
classMap(Class<A> aType, Class<B> bType)
          Constructs a new ClassMapBuilder instance initialized with the provided types which can be used to configure/customize the mapping between the two types.

The returned ClassMapBuilder instance, after being fully configured, should finally be registered with the factory using the registerClassMap method.
<A,B> ClassMapBuilder<A,B>
classMap(Class<A> aType, Type<B> bType)
          Constructs a new ClassMapBuilder instance initialized with the provided types which can be used to configure/customize the mapping between the two types.

The returned ClassMapBuilder instance, after being fully configured, should finally be registered with the factory using the registerClassMap method.
<A,B> ClassMapBuilder<A,B>
classMap(Type<A> aType, Class<B> bType)
          Constructs a new ClassMapBuilder instance initialized with the provided types which can be used to configure/customize the mapping between the two types.

The returned ClassMapBuilder instance, after being fully configured, should finally be registered with the factory using the registerClassMap method.
<A,B> ClassMapBuilder<A,B>
classMap(Type<A> aType, Type<B> bType)
          Constructs a new ClassMapBuilder instance initialized with the provided types which can be used to configure/customize the mapping between the two types.

The returned ClassMapBuilder instance, after being fully configured, should finally be registered with the factory using the registerClassMap method.
 boolean existsRegisteredMapper(Type<?> sourceType, Type<?> destinationType, boolean includeAutoGeneratedMappers)
          Tests for the existence of a registered mapper which corresponds to the supplied mapperKey
<A,B> ClassMap<A,B>
getClassMap(MapperKey mapperKey)
          Gets the ClassMap instance (if any) which has been associated with the types represented by the given MapperKey instance.
protected  ClassMapBuilderFactory getClassMapBuilderFactory()
           
 ConverterFactory getConverterFactory()
          Get an instance of the ConverterFactory associated with this MapperFactory; it may be used to register Converter instances to be used during mapping.
 MapperFacade getMapperFacade()
           
<A,B> BoundMapperFacade<A,B>
getMapperFacade(Class<A> aType, Class<B> bType)
          Returns a BoundMapperFacade for the specified source and destination types; this mapper is bidirectional by default, and provides signatures for mapping in place, or with instantiation, as needed.
<A,B> BoundMapperFacade<A,B>
getMapperFacade(Class<A> aType, Class<B> bType, boolean containsCycles)
          Returns a BoundMapperFacade for the specified source and destination types; this mapper is bidirectional by default, and provides signatures for mapping in place, or with instantiation, as needed.
In the case that false is provided for the 'containsCycles' parameter, an optimized mapper instance will be provided.
<S,D> BoundMapperFacade<S,D>
getMapperFacade(Type<S> sourceType, Type<D> destinationType)
          Returns a BoundMapperFacade for the specified source and destination types; this mapper is bidirectional by default, and provides signatures for mapping in place, or with instantiation, as needed.
<S,D> BoundMapperFacade<S,D>
getMapperFacade(Type<S> sourceType, Type<D> destinationType, boolean containsCycles)
          Returns a BoundMapperFacade for the specified source and destination types; this mapper is bidirectional by default, and provides signatures for mapping in place, or with instantiation, as needed.
In the case that false is provided for the 'containsCycles' parameter, an optimized mapper instance will be provided.
protected
<A,B> Mapper<A,B>
getRegisteredMapper(MapperKey mapperKey)
           
protected
<A,B> Mapper<A,B>
getRegisteredMapper(Type<A> typeA, Type<B> typeB, boolean includeAutoGeneratedMappers)
           
 UnenhanceStrategy getUserUnenhanceStrategy()
          Returns the UnenhanceStrategy associated with this MapperFactory, provided when this MapperFactory was constructed.
<S,D> Type<? extends D>
lookupConcreteDestinationType(Type<S> sourceType, Type<D> destinationType, MappingContext context)
           
 Set<Type<? extends Object>> lookupMappedClasses(Type<?> type)
          Get the set of classes which have been mapped for the specified type.
<A,B> Mapper<A,B>
lookupMapper(MapperKey mapperKey)
          Get the Mapper (if any) which has been associated with the given MapperKey.
 Mapper<Object,Object> lookupMapper(MapperKey mapperKey, MappingContext context)
          Searches for a Mapper which is capable of mapping the classes identified by the provided MapperKey instance
<T> ObjectFactory<T>
lookupObjectFactory(Type<T> targetType)
          Return the object factory (if any) which has been registered for the given type.
<T> ObjectFactory<T>
lookupObjectFactory(Type<T> type, MappingContext context)
           
 Set<ClassMap<Object,Object>> lookupUsedClassMap(MapperKey mapperKey)
          Lookup the class map hierarchy used for the mapping defined by the specified MapperKey, if any exists
protected
<S,D> void
register(Type<S> sourceType, Type<D> destinationType, boolean isAutoGenerated)
          Registers that a mapping exists from the specified source type to the specified destination type
<A,B> void
registerClassMap(ClassMap<A,B> classMap)
          Registers the given ClassMap instance with the factory; it will be used to configure an appropriate mapping.
<A,B> void
registerClassMap(ClassMapBuilder<A,B> builder)
          Registers the ClassMap configured by the specified ClassMapBuilder; it will be used to configure an appropriate mapping.
 void registerConcreteType(Class<?> abstractType, Class<?> concreteType)
          Register a concrete type to use when a need arises to generate a new instance of a given abstract type (abstract class or interface)
 void registerConcreteType(Type<?> abstractType, Type<?> concreteType)
          Register a concrete type to use when a need arises to generate a new instance of a given abstract type (abstract class or interface)
 void registerDefaultFieldMapper(DefaultFieldMapper... mappers)
          Register one or more DefaultFieldMapper instances to be used by this MapperFactory; these instances will be used whenever automatically generating a Mapper for a given pair of types.
<A,B> void
registerMapper(Mapper<A,B> mapper)
          Registers the specified custom mapper with the factory; it will be used for mapping between it's configured types.
 void registerMappingHint(MappingHint... hints)
          Deprecated. 
<D> void
registerObjectFactory(ObjectFactory<D> objectFactory, Type<D> destinationType)
          Register the given ObjectFactory with the MapperFactory; it will be used when constructing new instances of the specified targetType.
<T> void
registerObjectFactory(ObjectFactory<T> objectFactory, Class<T> targetClass)
          Register the given ObjectFactory with the MapperFactory; it will be used when constructing new instances of the specified targetType.
protected  Type<?> resolveConcreteType(Type<?> type, Type<?> originalType)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DefaultMapperFactory

protected DefaultMapperFactory(DefaultMapperFactory.MapperFactoryBuilder<?,?> builder)
Constructs a new instance of DefaultMapperFactory

Parameters:
builder -
Method Detail

addClassMapBuilderFactory

protected void addClassMapBuilderFactory(ClassMapBuilderFactory factory)
Add factory to the factories chain

Parameters:
factory -

buildUnenhanceStrategy

protected UnenhanceStrategy buildUnenhanceStrategy(UnenhanceStrategy unenhanceStrategy,
                                                   SuperTypeResolverStrategy superTypeStrategy)
Generates the UnenhanceStrategy to be used for this MapperFactory, applying the passed delegateStrategy if not null.
This allows the MapperFactory a chance to fill in the unenhance strategy with references to other parts of the factory (registered mappers, converters, object factories) which may be important in the "unenhancing" process.

Parameters:
unenhanceStrategy -
superTypeStrategy - true if the passed UnenhanceStrategy should take full responsibility for un-enhancement; false if the default behavior should be applied as a fail-safe after consulting the passed strategy.
Returns:
the resulting UnenhanceStrategy

buildMapperFacade

protected MapperFacade buildMapperFacade(MappingContextFactory contextFactory,
                                         UnenhanceStrategy unenhanceStrategy)
Builds the MapperFacade for this factory. Subclasses can override this method to build a custom MapperFacade. Please note that this method is called from the constructor and as such properties of the factory may not yet be initialized.

Parameters:
contextFactory -
unenhanceStrategy -
Returns:
the MapperFacade to use

lookupMapper

public <A,B> Mapper<A,B> lookupMapper(MapperKey mapperKey)
Description copied from interface: MapperFactory
Get the Mapper (if any) which has been associated with the given MapperKey. If one does not exist, and the Mapper supports automatic generation, an appropriate mapper will be generated.

Specified by:
lookupMapper in interface MapperFactory
Parameters:
mapperKey - the MapperKey for which to look up an associated Mapper
Returns:
the Mapper associated with mapperKey;

lookupMapper

public Mapper<Object,Object> lookupMapper(MapperKey mapperKey,
                                          MappingContext context)
Searches for a Mapper which is capable of mapping the classes identified by the provided MapperKey instance

Parameters:
mapperKey -
context -
Returns:
the Mapper instance which is able to support the set of classes identified by the passed MapperKey

existsRegisteredMapper

public boolean existsRegisteredMapper(Type<?> sourceType,
                                      Type<?> destinationType,
                                      boolean includeAutoGeneratedMappers)
Description copied from interface: MapperFactory
Tests for the existence of a registered mapper which corresponds to the supplied mapperKey

Specified by:
existsRegisteredMapper in interface MapperFactory
Parameters:
sourceType - the source type
destinationType - the destination type
includeAutoGeneratedMappers - specifies whether an auto-generated mapper for the given source and destination type should be considered
Returns:
true if such a mapper is registered with this factory

getRegisteredMapper

protected <A,B> Mapper<A,B> getRegisteredMapper(MapperKey mapperKey)
Parameters:
mapperKey -
Returns:
a registered Mapper which is able to map the specified types

getRegisteredMapper

protected <A,B> Mapper<A,B> getRegisteredMapper(Type<A> typeA,
                                                Type<B> typeB,
                                                boolean includeAutoGeneratedMappers)
Parameters:
typeA -
typeB -
includeAutoGeneratedMappers - whether auto-generated mappers should be included in the lookup
Returns:
a registered Mapper which is able to map the specified types

getMapperFacade

public MapperFacade getMapperFacade()
Specified by:
getMapperFacade in interface MapperFactory
Returns:
a thread-safe MapperFacade instance as configured by this MapperFactory

registerObjectFactory

public <D> void registerObjectFactory(ObjectFactory<D> objectFactory,
                                      Type<D> destinationType)
Description copied from interface: MapperFactory
Register the given ObjectFactory with the MapperFactory; it will be used when constructing new instances of the specified targetType.

Specified by:
registerObjectFactory in interface MapperFactory
Parameters:
objectFactory - the object factory to register
destinationType - the type which is generated by the given object factory

registerMappingHint

@Deprecated
public void registerMappingHint(MappingHint... hints)
Deprecated. 

Specified by:
registerMappingHint in interface MapperFactory

registerDefaultFieldMapper

public void registerDefaultFieldMapper(DefaultFieldMapper... mappers)
Description copied from interface: MapperFactory
Register one or more DefaultFieldMapper instances to be used by this MapperFactory; these instances will be used whenever automatically generating a Mapper for a given pair of types.

Specified by:
registerDefaultFieldMapper in interface MapperFactory
Parameters:
mappers - one or more DefaultFieldMapper instances to register

registerConcreteType

public void registerConcreteType(Type<?> abstractType,
                                 Type<?> concreteType)
Description copied from interface: MapperFactory
Register a concrete type to use when a need arises to generate a new instance of a given abstract type (abstract class or interface)

Specified by:
registerConcreteType in interface MapperFactory
Parameters:
abstractType - the type of the abstract class or interface
concreteType - the concrete type to instantiate

registerConcreteType

public void registerConcreteType(Class<?> abstractType,
                                 Class<?> concreteType)
Description copied from interface: MapperFactory
Register a concrete type to use when a need arises to generate a new instance of a given abstract type (abstract class or interface)

Specified by:
registerConcreteType in interface MapperFactory
Parameters:
abstractType - the abstract class or interface
concreteType - the concrete class to instantiate

lookupObjectFactory

public <T> ObjectFactory<T> lookupObjectFactory(Type<T> targetType)
Description copied from interface: MapperFactory
Return the object factory (if any) which has been registered for the given type.

Specified by:
lookupObjectFactory in interface MapperFactory
Parameters:
targetType - the type for which to lookup a registered ObjectFactory.
Returns:
the ObjectFactory registered for the given targetType; null if no ObjectFactory has been registered for the given type.

lookupObjectFactory

public <T> ObjectFactory<T> lookupObjectFactory(Type<T> type,
                                                MappingContext context)
Parameters:
type -
context -
Returns:
an ObjectFactory instance which is able to instantiate the specified type

lookupConcreteDestinationType

public <S,D> Type<? extends D> lookupConcreteDestinationType(Type<S> sourceType,
                                                             Type<D> destinationType,
                                                             MappingContext context)
Specified by:
lookupConcreteDestinationType in interface MapperFactory
Returns:

resolveConcreteType

protected Type<?> resolveConcreteType(Type<?> type,
                                      Type<?> originalType)
Parameters:
type -
originalType -
Returns:
a concrete type (if any) which has been registered for the specified abstract type

registerClassMap

public <A,B> void registerClassMap(ClassMap<A,B> classMap)
Description copied from interface: MapperFactory
Registers the given ClassMap instance with the factory; it will be used to configure an appropriate mapping.

Specified by:
registerClassMap in interface MapperFactory
Parameters:
classMap - the ClassMap instance to register

registerClassMap

public <A,B> void registerClassMap(ClassMapBuilder<A,B> builder)
Description copied from interface: MapperFactory
Registers the ClassMap configured by the specified ClassMapBuilder; it will be used to configure an appropriate mapping.

Specified by:
registerClassMap in interface MapperFactory
Parameters:
builder - the ClassMapBuilder to register

build

public void build()
Description copied from interface: MapperFactory
Builds this MapperFactory.

Specified by:
build in interface MapperFactory

lookupUsedClassMap

public Set<ClassMap<Object,Object>> lookupUsedClassMap(MapperKey mapperKey)
Description copied from interface: MapperFactory
Lookup the class map hierarchy used for the mapping defined by the specified MapperKey, if any exists

Specified by:
lookupUsedClassMap in interface MapperFactory
Parameters:
mapperKey - the mapper key defining the mapping to resolve
Returns:
the set of class mappings in the hierarchy of mappings used for the specified mapper key

register

protected <S,D> void register(Type<S> sourceType,
                              Type<D> destinationType,
                              boolean isAutoGenerated)
Registers that a mapping exists from the specified source type to the specified destination type

Parameters:
sourceType -
destinationType -

getClassMap

public <A,B> ClassMap<A,B> getClassMap(MapperKey mapperKey)
Description copied from interface: MapperFactory
Gets the ClassMap instance (if any) which has been associated with the types represented by the given MapperKey instance.

Specified by:
getClassMap in interface MapperFactory
Parameters:
mapperKey - the MapperKey which should be used to look up an associated ClassMap
Returns:
the ClassMap which has been associated with the given MapperKey; null if no instance has been associated with this MapperKey instance.

lookupMappedClasses

public Set<Type<? extends Object>> lookupMappedClasses(Type<?> type)
Description copied from interface: MapperFactory
Get the set of classes which have been mapped for the specified type.

Specified by:
lookupMappedClasses in interface MapperFactory
Parameters:
type - the type for which to look up mapped types
Returns:
the set of types which have been mapped for the specified type.

getConverterFactory

public ConverterFactory getConverterFactory()
Description copied from interface: MapperFactory
Get an instance of the ConverterFactory associated with this MapperFactory; it may be used to register Converter instances to be used during mapping.

Specified by:
getConverterFactory in interface MapperFactory
Returns:
the ConverterFactory instance associated with this MapperFactory;

registerObjectFactory

public <T> void registerObjectFactory(ObjectFactory<T> objectFactory,
                                      Class<T> targetClass)
Description copied from interface: MapperFactory
Register the given ObjectFactory with the MapperFactory; it will be used when constructing new instances of the specified targetType.

Specified by:
registerObjectFactory in interface MapperFactory
Parameters:
objectFactory - the object factory to register

getClassMapBuilderFactory

protected ClassMapBuilderFactory getClassMapBuilderFactory()
Returns:
the (initialized) ClassMapBuilderFactory configured for this mapper factory

classMap

public <A,B> ClassMapBuilder<A,B> classMap(Type<A> aType,
                                           Type<B> bType)
Description copied from interface: MapperFactory
Constructs a new ClassMapBuilder instance initialized with the provided types which can be used to configure/customize the mapping between the two types.

The returned ClassMapBuilder instance, after being fully configured, should finally be registered with the factory using the registerClassMap method.

Specified by:
classMap in interface MapperFactory
Parameters:
aType - the Type instance representing the "A" side of the mapping
bType - the Type instance representing the "B" side of the mapping
Returns:

classMap

public <A,B> ClassMapBuilder<A,B> classMap(Class<A> aType,
                                           Type<B> bType)
Description copied from interface: MapperFactory
Constructs a new ClassMapBuilder instance initialized with the provided types which can be used to configure/customize the mapping between the two types.

The returned ClassMapBuilder instance, after being fully configured, should finally be registered with the factory using the registerClassMap method.

Specified by:
classMap in interface MapperFactory
Parameters:
aType - the Class instance representing the "A" side of the mapping
bType - the Type instance representing the "B" side of the mapping
Returns:

classMap

public <A,B> ClassMapBuilder<A,B> classMap(Type<A> aType,
                                           Class<B> bType)
Description copied from interface: MapperFactory
Constructs a new ClassMapBuilder instance initialized with the provided types which can be used to configure/customize the mapping between the two types.

The returned ClassMapBuilder instance, after being fully configured, should finally be registered with the factory using the registerClassMap method.

Specified by:
classMap in interface MapperFactory
Parameters:
aType - the Type instance representing the "A" side of the mapping
bType - the Class instance representing the "B" side of the mapping
Returns:

classMap

public <A,B> ClassMapBuilder<A,B> classMap(Class<A> aType,
                                           Class<B> bType)
Description copied from interface: MapperFactory
Constructs a new ClassMapBuilder instance initialized with the provided types which can be used to configure/customize the mapping between the two types.

The returned ClassMapBuilder instance, after being fully configured, should finally be registered with the factory using the registerClassMap method.

Specified by:
classMap in interface MapperFactory
Parameters:
aType - the Class instance representing the "A" side of the mapping
bType - the Class instance representing the "B" side of the mapping
Returns:

registerMapper

public <A,B> void registerMapper(Mapper<A,B> mapper)
Description copied from interface: MapperFactory
Registers the specified custom mapper with the factory; it will be used for mapping between it's configured types.

Specified by:
registerMapper in interface MapperFactory
Parameters:
mapper - the mapper to register

getMapperFacade

public <S,D> BoundMapperFacade<S,D> getMapperFacade(Type<S> sourceType,
                                                    Type<D> destinationType)
Description copied from interface: MapperFactory
Returns a BoundMapperFacade for the specified source and destination types; this mapper is bidirectional by default, and provides signatures for mapping in place, or with instantiation, as needed.

Specified by:
getMapperFacade in interface MapperFactory
Parameters:
sourceType - the 'A' type for the returned DedicatedMapperFacade
destinationType - the 'B' type for the returned DedicatedMapperFacade

getMapperFacade

public <S,D> BoundMapperFacade<S,D> getMapperFacade(Type<S> sourceType,
                                                    Type<D> destinationType,
                                                    boolean containsCycles)
Description copied from interface: MapperFactory
Returns a BoundMapperFacade for the specified source and destination types; this mapper is bidirectional by default, and provides signatures for mapping in place, or with instantiation, as needed.
In the case that false is provided for the 'containsCycles' parameter, an optimized mapper instance will be provided.

Specified by:
getMapperFacade in interface MapperFactory
Parameters:
sourceType - the 'A' type for the returned DedicatedMapperFacade
destinationType - the 'B' type for the returned DedicatedMapperFacade
containsCycles - true if the object graph for the source and destination types may contain cycles in it's graph; if 'false', an optimized DedicatedMapperFacade instance can be provided.
Returns:

getMapperFacade

public <A,B> BoundMapperFacade<A,B> getMapperFacade(Class<A> aType,
                                                    Class<B> bType)
Description copied from interface: MapperFactory
Returns a BoundMapperFacade for the specified source and destination types; this mapper is bidirectional by default, and provides signatures for mapping in place, or with instantiation, as needed.

Specified by:
getMapperFacade in interface MapperFactory
Parameters:
aType - the 'A' type for the returned DedicatedMapperFacade
bType - the 'B' type for the returned DedicatedMapperFacade

getMapperFacade

public <A,B> BoundMapperFacade<A,B> getMapperFacade(Class<A> aType,
                                                    Class<B> bType,
                                                    boolean containsCycles)
Description copied from interface: MapperFactory
Returns a BoundMapperFacade for the specified source and destination types; this mapper is bidirectional by default, and provides signatures for mapping in place, or with instantiation, as needed.
In the case that false is provided for the 'containsCycles' parameter, an optimized mapper instance will be provided.

Specified by:
getMapperFacade in interface MapperFactory
Parameters:
aType - the 'A' type for the returned DedicatedMapperFacade
bType - the 'B' type for the returned DedicatedMapperFacade
containsCycles - true if the object graph for the source and destination types may contain cycles in it's graph; if 'false', an optimized DedicatedMapperFacade instance can be provided.
Returns:

getUserUnenhanceStrategy

public UnenhanceStrategy getUserUnenhanceStrategy()
Description copied from interface: MapperFactory
Returns the UnenhanceStrategy associated with this MapperFactory, provided when this MapperFactory was constructed.

Specified by:
getUserUnenhanceStrategy in interface MapperFactory
Returns:
the UnenhanceStrategy associated with this MapperFactory.


Copyright © 2013 Glasnost. All Rights Reserved.