org.omnaest.utils.beans.copier
Class PreparedBeanCopier<FROM,TO>

java.lang.Object
  extended by org.omnaest.utils.beans.copier.PreparedBeanCopier<FROM,TO>
All Implemented Interfaces:
Serializable

public class PreparedBeanCopier<FROM,TO>
extends Object
implements Serializable

A PreparedBeanCopier will prepare reflection based property copy actions for two given Class types in advance.

This allows to execute the copy process about 5 - 10 times faster than the Apache Commons BeanUtils.copyProperties(Object, Object)

E.g. for bean copy actions including instantiations of Maps or Lists this implementations is only about 5 - 10 times slower than regular getter and setter transfer constructs.

The overall speed is about 5-10 seconds for about 10000000 copy operations invoked by the same PreparedBeanCopier instance.

The PreparedBeanCopier can be based on interfaces when being constructed, which allows to copy a reduced set of properties by giving an interface with a subset of the instance properties at construction time and the implementing instance during copy method invocation.

The PreparedBeanCopier.Configuration allows to set various options, like:



The deepCloneProperties(Object) and deepCopyProperties(Object, Object) instances are thread safe per default, as long as any at setExceptionHandler(ExceptionHandler) or at Configuration#add(CopierFactory) or Configuration#add(InstanceFactoryCreator) provided instances are thread safe.

Author:
Omnaest
See Also:
Serialized Form

Nested Class Summary
static class PreparedBeanCopier.Configuration
           
static interface PreparedBeanCopier.CopierFactory
          Factory for PreparedBeanCopier.CopierFactory.Copier instances
static interface PreparedBeanCopier.InstanceFactoryCreator
           
static interface PreparedBeanCopier.MetaDataHandler
           
static class PreparedBeanCopier.NonMatchingPropertyException
           
static interface PreparedBeanCopier.Transformer
          A PreparedBeanCopier.Transformer will return new transformed instances for another given instance
 
Constructor Summary
PreparedBeanCopier(Class<? extends FROM> typeFrom, Class<? extends TO> typeTo)
           
PreparedBeanCopier(Class<? extends FROM> typeFrom, Class<? extends TO> typeTo, PreparedBeanCopier.Configuration configuration)
           
 
Method Summary
 TO deepCloneProperties(FROM instanceFrom)
           
 PreparedBeanCopier<FROM,TO> deepCopyProperties(FROM instanceFrom, TO instanceTo)
           
 List<String> getNonMatchingPropertyNameList()
          Returns a List of all non matching property names
 boolean hasNonMatchingProperties()
          Returns true if there are properties which could not be matched
 PreparedBeanCopier<FROM,TO> setExceptionHandler(ExceptionHandler exceptionHandler)
           
 PreparedBeanCopier<FROM,TO> setPropertyAccessTypeFrom(BeanPropertyAccessor.PropertyAccessType propertyAccessTypeFrom)
           
 PreparedBeanCopier<FROM,TO> setPropertyAccessTypeTo(BeanPropertyAccessor.PropertyAccessType propertyAccessTypeTo)
           
 PreparedBeanCopier<FROM,TO> throwExceptionWhenAnyPropertiesAreNotMatching()
          Throws an PreparedBeanCopier.NonMatchingPropertyException if hasNonMatchingProperties() is true
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PreparedBeanCopier

public PreparedBeanCopier(Class<? extends FROM> typeFrom,
                          Class<? extends TO> typeTo)
Parameters:
typeFrom -
typeTo -
See Also:
PreparedBeanCopier

PreparedBeanCopier

public PreparedBeanCopier(Class<? extends FROM> typeFrom,
                          Class<? extends TO> typeTo,
                          PreparedBeanCopier.Configuration configuration)
Parameters:
typeFrom - Class
typeTo - Class
configuration - PreparedBeanCopier.Configuration
See Also:
PreparedBeanCopier
Method Detail

deepCloneProperties

public TO deepCloneProperties(FROM instanceFrom)
Parameters:
instanceFrom -
Returns:
new clone instance

deepCopyProperties

public PreparedBeanCopier<FROM,TO> deepCopyProperties(FROM instanceFrom,
                                                      TO instanceTo)
Parameters:
instanceFrom -
instanceTo -
Returns:
this

setExceptionHandler

public PreparedBeanCopier<FROM,TO> setExceptionHandler(ExceptionHandler exceptionHandler)
Parameters:
exceptionHandler - ExceptionHandler
Returns:
this

setPropertyAccessTypeFrom

public PreparedBeanCopier<FROM,TO> setPropertyAccessTypeFrom(BeanPropertyAccessor.PropertyAccessType propertyAccessTypeFrom)
Parameters:
propertyAccessTypeFrom - BeanPropertyAccessor.PropertyAccessType
Returns:
this

setPropertyAccessTypeTo

public PreparedBeanCopier<FROM,TO> setPropertyAccessTypeTo(BeanPropertyAccessor.PropertyAccessType propertyAccessTypeTo)
Parameters:
propertyAccessTypeTo - BeanPropertyAccessor.PropertyAccessType
Returns:
this

getNonMatchingPropertyNameList

public List<String> getNonMatchingPropertyNameList()
Returns a List of all non matching property names

Returns:
See Also:
hasNonMatchingProperties()

hasNonMatchingProperties

public boolean hasNonMatchingProperties()
Returns true if there are properties which could not be matched

Returns:
See Also:
getNonMatchingPropertyNameList()

throwExceptionWhenAnyPropertiesAreNotMatching

public PreparedBeanCopier<FROM,TO> throwExceptionWhenAnyPropertiesAreNotMatching()
                                                                          throws PreparedBeanCopier.NonMatchingPropertyException
Throws an PreparedBeanCopier.NonMatchingPropertyException if hasNonMatchingProperties() is true

Returns:
this
Throws:
PreparedBeanCopier.NonMatchingPropertyException
See Also:
hasNonMatchingProperties()


Copyright © 2013. All Rights Reserved.