|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.omnaest.utils.beans.replicator.BeanReplicator<FROM,TO>
FROM - TO - public class BeanReplicator<FROM,TO>
BeanReplicator allows to copy(Object, Object) or clone(Object) instances from one type to another
type. BeanReplicator maps similar named and typed properties to each other. The declare(Declaration)
method allows to specify in more detail how the replication of a target instance takes place. Map<String,Object>Map<String,Object> to Java beanMap to Map / SortedMapList to List / Set / Collection / ArraySet to List / Set / Collection / ArrayCollection to List / Set / Collection / ArrayIterable to List / Set / Collection / ArrayList / Set / Collection / ArrayString to Long conversions are done automatically. BeanReplicator will try to generate an proxy implementation on the fly
which will act like a normal bean. Of course such conversions are much slower, than normal bean to bean copy actions. BeanReplicator throws internally
NoMatchingPropertiesExceptions which are given to any ExceptionHandler set with
setExceptionHandler(ExceptionHandler).CopyException and given to the set ExceptionHandler.
The BeanReplicator methods itself will never throw any Exception.BeanReplicator instance is thread safe in its copy(Object, Object) and clone(Object) methods. The
declare(Declaration) method is NOT thread safe and the modification of any BeanReplicator.Declaration should be strongly
avoided during any copy(Object, Object) or clone(Object) invocation. BeanReplicator is still about 2-5 times faster than the
Apache Commons BeanUtils.copyProperties(Object, Object)
{
BeanReplicator<BeanFrom, BeanTo> beanReplicator = new BeanReplicator<BeanFrom, BeanTo>( BeanFrom.class, BeanTo.class );
final BeanFrom simpleBean = new BeanFrom();
final TestSimpleBeanTo clone = new TestSimpleBeanTo();
beanReplicator.copy( simpleBean, clone );
}
Clone example:
{
BeanReplicator<BeanFrom, BeanTo> beanReplicator = new BeanReplicator<BeanFrom, BeanTo>( BeanFrom.class, BeanTo.class );
final BeanFrom simpleBean = new BeanFrom();
final BeanTo clone = beanReplicator.clone( simpleBean );
}
Remapping example:
Source bean:
|--Bean0From bean0From
|--Bean1From bean1From
|--Bean2From bean2From
|--String fieldForLong
|--String fieldString1
|--String fieldString2
Target bean:
|-- Bean0To bean0To
|--Bean1To bean1To
|--Bean2To bean2To
|--long fieldLong
|--String fieldString1
|--String fieldString2
beanReplicator.declare( new Declaration()
{
@Override
public void declare( DeclarationSupport support )
{
support.addTypeMapping( Bean1From.class, Bean1To.class );
support.addPropertyNameMapping( "bean1From", "bean1To" );
support.addTypeMappingForPath( "bean1From", Bean2From.class, Bean2To.class );
support.addPropertyNameMapping( "bean1From", "bean2From", "bean2To" );
support.addTypeAndPropertyNameMapping( "bean1From.bean2From", String.class, "fieldForLong", Long.class, "fieldLong" );
}
} );
BeanReplicator.DeclarationSupport,
BeanReplicator.Declaration,
declare(Declaration),
setExceptionHandler(ExceptionHandler),
Serialized Form| Nested Class Summary | |
|---|---|
static interface |
BeanReplicator.ConverterPipeDeclarer
|
static interface |
BeanReplicator.Declaration
A BeanReplicator.Declaration allows to declare several kinds of mappings a BeanReplicator should rely on. |
static interface |
BeanReplicator.DeclarationSupport
The BeanReplicator.DeclarationSupport allows to add e.g. type to type or property name mappings, ElementConverter pipes,
preservations, ... |
static interface |
BeanReplicator.PipeBuilder<FROM,TO>
The BeanReplicator.PipeBuilder allows to construct a pipe from a given type to another over one or multiple
ElementConverter instances. |
| Constructor Summary | |
|---|---|
BeanReplicator(Class<? super FROM> sourceType,
Class<? extends TO> targetType)
|
|
| Method Summary | |
|---|---|
TO |
clone(FROM source)
Clones a given source instance into its target instance representation |
void |
copy(FROM source,
TO target)
Copies from a given source instance to a given target instance |
BeanReplicator<FROM,TO> |
declare(BeanReplicator.Declaration declaration)
Allows the specify a mapping BeanReplicator.Declaration BeanReplicator.Declaration have to be made before any call to copy(Object, Object) or clone(Object) |
BeanReplicator<FROM,TO> |
setExceptionHandler(ExceptionHandler exceptionHandler)
The given ExceptionHandler should handle any CopyException and PreparedBeanCopier.NonMatchingPropertyException |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public BeanReplicator(Class<? super FROM> sourceType,
Class<? extends TO> targetType)
sourceType - targetType - BeanReplicator| Method Detail |
|---|
public TO clone(FROM source)
source -
copy(Object, Object)
public void copy(FROM source,
TO target)
source - target - clone(Object)public BeanReplicator<FROM,TO> declare(BeanReplicator.Declaration declaration)
BeanReplicator.Declaration BeanReplicator.Declaration have to be made before any call to copy(Object, Object) or clone(Object)
declaration - BeanReplicator.Declaration
public BeanReplicator<FROM,TO> setExceptionHandler(ExceptionHandler exceptionHandler)
ExceptionHandler should handle any CopyException and PreparedBeanCopier.NonMatchingPropertyException
exceptionHandler -
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||