Package com.hotels.beans.transformer
Class TransformerImpl
- java.lang.Object
-
- com.hotels.beans.transformer.TransformerImpl
-
- All Implemented Interfaces:
Transformer
public class TransformerImpl extends Object
Utility methods for populating Mutable, Immutable and Hybrid JavaBeans properties via reflection. The implementations are provided by BeanUtils.
-
-
Constructor Summary
Constructors Constructor Description TransformerImpl()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
removeFieldMapping(String destFieldName)
Removes the field mapping for the given field.void
removeFieldTransformer(String destFieldName)
Removes the field transformer for the given field.void
resetFieldsMapping()
Removes all the configured fields mapping.void
resetFieldsTransformationSkip()
Removes all the configured fields to skip.void
resetFieldsTransformer()
Removes all the configured fields transformer.Transformer
setDefaultValueForMissingField(boolean useDefaultValue)
It allows to configure the transformer in order to set a default value in case some field is missing in the source object.Transformer
setFlatFieldNameTransformation(boolean useFlatTransformation)
It allows to configure the transformer in order to apply a transformation function on all fields matching the given name without keeping in consideration their full path.Transformer
setValidationEnabled(boolean validationEnabled)
It allows to enable the object validation.Transformer
skipTransformationForField(String... fieldName)
Allows to specify all the fields for which the transformation have to be skipped.<T,K>
Ktransform(T sourceObj, Class<? extends K> targetClass)
Copies all properties from an object to a new one.protected <T,K>
Ktransform(T sourceObj, Class<? extends K> targetClass, String breadcrumb)
Copies all properties from an object to a new one.<T,K>
voidtransform(T sourceObj, K targetObject)
Copies all properties from an object to a new one.protected <T,K>
voidtransform(T sourceObj, K targetObject, String breadcrumb)
Copies all properties from an object to a new one.Transformer
withFieldMapping(FieldMapping... fieldMapping)
Initializes the mapping between fields in the source object and the destination one.Transformer
withFieldTransformer(FieldTransformer... fieldTransformer)
Initializes the field transformer functions.
-
-
-
Method Detail
-
transform
protected final <T,K> K transform(T sourceObj, Class<? extends K> targetClass, String breadcrumb)
Copies all properties from an object to a new one.- Type Parameters:
T
- the Source object typeK
- the target object type- Parameters:
sourceObj
- the source objecttargetClass
- the destination object classbreadcrumb
- the full path of the current field starting from his ancestor- Returns:
- a copy of the source object into the destination object
-
transform
protected final <T,K> void transform(T sourceObj, K targetObject, String breadcrumb)
Copies all properties from an object to a new one.- Type Parameters:
T
- the Source object typeK
- the target object type- Parameters:
sourceObj
- the source objecttargetObject
- the destination objectbreadcrumb
- the full path of the current field starting from his ancestor
-
withFieldMapping
public final Transformer withFieldMapping(FieldMapping... fieldMapping)
Initializes the mapping between fields in the source object and the destination one.- Specified by:
withFieldMapping
in interfaceTransformer
- Parameters:
fieldMapping
- the field mapping- Returns:
- the
Transformer
instance
-
removeFieldMapping
public final void removeFieldMapping(String destFieldName)
Removes the field mapping for the given field.- Specified by:
removeFieldMapping
in interfaceTransformer
- Parameters:
destFieldName
- the field name in the destination object
-
resetFieldsMapping
public final void resetFieldsMapping()
Removes all the configured fields mapping.- Specified by:
resetFieldsMapping
in interfaceTransformer
-
withFieldTransformer
public final Transformer withFieldTransformer(FieldTransformer... fieldTransformer)
Initializes the field transformer functions. The transformer function returns directly the field value.- Specified by:
withFieldTransformer
in interfaceTransformer
- Parameters:
fieldTransformer
- the fields transformer function- Returns:
- the
Transformer
instance
-
removeFieldTransformer
public final void removeFieldTransformer(String destFieldName)
Removes the field transformer for the given field.- Specified by:
removeFieldTransformer
in interfaceTransformer
- Parameters:
destFieldName
- the field name in the destination object
-
resetFieldsTransformer
public final void resetFieldsTransformer()
Removes all the configured fields transformer.- Specified by:
resetFieldsTransformer
in interfaceTransformer
-
setDefaultValueForMissingField
public final Transformer setDefaultValueForMissingField(boolean useDefaultValue)
It allows to configure the transformer in order to set a default value in case some field is missing in the source object. If set to true the default value is set, if false if it raises a:MissingFieldException
in case of missing fields.- Specified by:
setDefaultValueForMissingField
in interfaceTransformer
- Parameters:
useDefaultValue
- true in case the default value should be set, false if it should raise a:MissingFieldException
in case of missing field.- Returns:
- the
Transformer
instance
-
setFlatFieldNameTransformation
public final Transformer setFlatFieldNameTransformation(boolean useFlatTransformation)
It allows to configure the transformer in order to apply a transformation function on all fields matching the given name without keeping in consideration their full path. If set to true the default value is set, if false if it raises a:MissingFieldException
in case of missing fields.- Specified by:
setFlatFieldNameTransformation
in interfaceTransformer
- Parameters:
useFlatTransformation
- indicates if the transformer function has to be performed on all fields matching the given name without keeping in consideration their full path.- Returns:
- the
Transformer
instance
-
setValidationEnabled
public Transformer setValidationEnabled(boolean validationEnabled)
It allows to enable the object validation.- Specified by:
setValidationEnabled
in interfaceTransformer
- Parameters:
validationEnabled
- if true the validation is performed.- Returns:
- the
Transformer
instance
-
transform
public final <T,K> K transform(T sourceObj, Class<? extends K> targetClass)
Copies all properties from an object to a new one.- Specified by:
transform
in interfaceTransformer
- Type Parameters:
T
- the Source object typeK
- the target object type- Parameters:
sourceObj
- the source objecttargetClass
- the destination object class- Returns:
- a copy of the source object into the destination object
-
transform
public final <T,K> void transform(T sourceObj, K targetObject)
Copies all properties from an object to a new one.- Specified by:
transform
in interfaceTransformer
- Type Parameters:
T
- the Source object typeK
- the target object type- Parameters:
sourceObj
- the source objecttargetObject
- the destination object
-
skipTransformationForField
public Transformer skipTransformationForField(String... fieldName)
Allows to specify all the fields for which the transformation have to be skipped.- Specified by:
skipTransformationForField
in interfaceTransformer
- Parameters:
fieldName
- the destination object's field(s) name that have to be skipped- Returns:
- the
Transformer
instance
-
resetFieldsTransformationSkip
public void resetFieldsTransformationSkip()
Description copied from interface:Transformer
Removes all the configured fields to skip.- Specified by:
resetFieldsTransformationSkip
in interfaceTransformer
-
-