Package org.nuiton.util.beans
Class Binder<I,O>
java.lang.Object
org.nuiton.util.beans.Binder<I,O>
- Type Parameters:
I- the source bean typeO- the destination bean type
- All Implemented Interfaces:
java.io.Serializable
public class Binder<I,O>
extends java.lang.Object
implements java.io.Serializable
A
binder permits to copy some properties from an object to another
one.
It is based on a Binder.BinderModel which contains the mapping of properties
to transfert from the source object to the destination object.
Use the method copy(Object, Object, String...) to transfert properties.
Use the method obtainProperties(Object, String...) to obtain some
properties from a given object.
For more informations about how to obtain a binder, see the
BinderFactory or the package info javadoc or unit tests...- Since:
- 1.1.5
- Author:
- Tony Chemit - chemit@codelutin.com
- See Also:
BinderFactory,BinderModelBuilder, Serialized Form
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classBinder.BinderModel<S,T>Model of aBinder.static classBinder.CollectionStrategyTypes of loading of collections. -
Field Summary
Fields Modifier and Type Field Description protected Binder.BinderModel<I,O>modelthe model of the binder -
Constructor Summary
Constructors Constructor Description Binder() -
Method Summary
Modifier and Type Method Description protected java.lang.Objectbind(Binder binder, java.lang.Object read)protected java.lang.ObjectbindCollection(java.lang.String sourceProperty, java.lang.Object read)protected java.lang.ObjectbindProperty(java.lang.String sourceProperty, java.lang.Object read)protected voidcopy(I source, O target, boolean excludeProperties, java.lang.String... propertyNames)Copy properties from a source bean to a destination one according to the model of the binder.voidcopy(I source, O target, java.lang.String... propertyNames)Copy properties from a source bean to a destination one according to the model of the binder.voidcopyExcluding(I source, O target, java.lang.String... propertyNames)Copy properties from a source bean to a destination one according to the model of the binder excludingpropertyNames.java.util.List<PropertyDiff>diff(I source, O target)Compare two beans property by property according to the model.protected java.util.List<PropertyDiff>diff(I source, O target, boolean excludeProperties, java.lang.String... propertyNames)java.util.List<PropertyDiff>diffExcluding(I source, O target, java.lang.String... propertyNames)Compare two beans property by property according to the model.protected java.lang.String[]getAllPropertiesExclude(java.lang.String... propertyNameExcludes)Obtains all properties from binder's model except thosepropertyNameExcludes.protected java.lang.ObjectgetCollectionValue(java.lang.String sourceProperty, java.lang.Object readValue)protected Binder.BinderModel<I,O>getModel()Get the model of the binder.protected java.lang.String[]getProperties(java.lang.String... propertyNames)Obtain the properties, if none is given inpropertyNamesparameter, will use all property names defined in binder's model, otherwise, check that all given property names are safe (registred in binder's model).java.lang.reflect.TypegetSourcePropertyGenericType(java.lang.String propertyName)Get the generic type of a source property.java.lang.Class<?>getSourcePropertyType(java.lang.String propertyName)Get the type of a source property.java.lang.Class<I>getSourceType()Obtains the type of the source bean.java.lang.reflect.TypegetTargetPropertyGenericType(java.lang.String propertyName)Get the generic type of a target property.java.lang.Class<?>getTargetPropertyType(java.lang.String propertyName)Get the type of a target property.java.lang.Class<O>getTargetType()Obtains the type of the target bean.voidinjectProperties(java.util.Map<java.lang.String,java.lang.Object> properties, O target)Inject all not null properties to the target bean.voidinjectProperties(java.util.Map<java.lang.String,java.lang.Object> properties, O target, boolean includeNullValues)Inject all properties to the target bean.java.util.Map<java.lang.String,java.lang.Object>obtainProperties(I source, boolean keepPrimitiveDefaultValues, boolean includeNullValues, java.lang.String... propertyNames)Obtain from the given object all properties registered in the binder model.java.util.Map<java.lang.String,java.lang.Object>obtainProperties(I source, boolean includeNullValues, java.lang.String... propertyNames)Obtain from the given object all properties registered in the binder model.java.util.Map<java.lang.String,java.lang.Object>obtainProperties(I source, java.lang.String... propertyNames)Obtain from the given object all properties registered in the binder model.<OO> OOobtainSourceProperty(I source, java.lang.String propertyName)Obtain a property from a source object (A source object type reflect the source type of the binder).<OO> OOobtainTargetProperty(O target, java.lang.String propertyName)Obtain a property from a target object (A target object type reflect the target type of the binder).protected java.lang.ObjectreadProperty(java.lang.String sourceProperty, java.lang.Object source, java.lang.reflect.Method readMethod)protected voidsetModel(Binder.BinderModel<I,O> model)Set the model of the binder.protected java.lang.Objecttransform(java.lang.String propertyName, java.lang.Object propertyValue)
-
Field Details
-
model
the model of the binder
-
-
Constructor Details
-
Binder
public Binder()
-
-
Method Details
-
getSourceType
Obtains the type of the source bean.- Returns:
- the type of the source bean
-
getTargetType
Obtains the type of the target bean.- Returns:
- the type of the target bean
-
obtainProperties
public java.util.Map<java.lang.String,java.lang.Object> obtainProperties(I source, boolean keepPrimitiveDefaultValues, boolean includeNullValues, java.lang.String... propertyNames)Obtain from the given object all properties registered in the binder model.- Parameters:
source- the bean to readpropertyNames- subset of properties to loadkeepPrimitiveDefaultValues- to keep primitive default value and not replace them by anullvalue.includeNullValues- get all the properties and values for the given bean. If false, you'll get only the values- Returns:
- the map of properties obtained indexed by their property name,
or an empty map is the given
fromisnull. - Since:
- 3.0
-
obtainProperties
public java.util.Map<java.lang.String,java.lang.Object> obtainProperties(I source, boolean includeNullValues, java.lang.String... propertyNames)Obtain from the given object all properties registered in the binder model.- Parameters:
source- the bean to readpropertyNames- subset of properties to loadincludeNullValues- get all the properties and values for the given bean. If false, you'll get only the values- Returns:
- the map of properties obtained indexed by their property name,
or an empty map is the given
fromisnull. - Since:
- 2.3
-
obtainProperties
public java.util.Map<java.lang.String,java.lang.Object> obtainProperties(I source, java.lang.String... propertyNames)Obtain from the given object all properties registered in the binder model. Note: If a property's value is null, it will not be injected in the result.- Parameters:
source- the bean to readpropertyNames- subset of properties to load- Returns:
- the map of properties obtained indexed by their property name,
or an empty map is the given
fromisnull.
-
obtainSourceProperty
Obtain a property from a source object (A source object type reflect the source type of the binder). Note: The property value has no special treatment, the result is the exact value from the source object (no binder collection transformation, ...).- Type Parameters:
OO- type of property to get- Parameters:
source- the source object to inspectpropertyName- name of the property to get- Returns:
- the property value in the source object.
- Since:
- 3.0
-
obtainTargetProperty
Obtain a property from a target object (A target object type reflect the target type of the binder). Note: The property value has no special treatment, the result is the exact value from the target object (no binder collection transformation, ...).- Type Parameters:
OO- type of property to get- Parameters:
target- the target object to inspectpropertyName- name of the property to get- Returns:
- the property value in the target object.
- Since:
- 3.0
-
injectProperties
public void injectProperties(java.util.Map<java.lang.String,java.lang.Object> properties, O target)Inject all not null properties to the target bean.- Parameters:
properties- properties to set into beantarget- the bean to set- Since:
- 3.0
-
injectProperties
public void injectProperties(java.util.Map<java.lang.String,java.lang.Object> properties, O target, boolean includeNullValues)Inject all properties to the target bean.- Parameters:
properties- properties to set into beantarget- the bean to setincludeNullValues-trueto set also null properties values- Since:
- 3.0
-
transform
protected java.lang.Object transform(java.lang.String propertyName, java.lang.Object propertyValue) -
copy
Copy properties from a source bean to a destination one according to the model of the binder. IfpropertyNamesis defined, only those properties will be copied. Note: Iffromobject is null, thennullvalues will be set to mapped properties intodst- Parameters:
source- the bean to readtarget- the bean to writepropertyNames- optional subset of properties to copy (if none is specifed, will use all the properties defined in binder)- Throws:
java.lang.NullPointerException- if target parameter isnull
-
copyExcluding
Copy properties from a source bean to a destination one according to the model of the binder excludingpropertyNames. Note: Iffromobject is null, thennullvalues will be set to mapped properties intodst.- Parameters:
source- the bean to readtarget- the bean to writepropertyNames- optional subset of properties to copy (if none is specifed, will use all the properties defined in binder)- Throws:
java.lang.NullPointerException- if target parameter isnull
-
getSourcePropertyType
public java.lang.Class<?> getSourcePropertyType(java.lang.String propertyName)Get the type of a source property.- Parameters:
propertyName- name of the source property- Returns:
- the type of the source property
- Throws:
java.lang.IllegalArgumentException- if binder does not define this source property
-
getSourcePropertyGenericType
public java.lang.reflect.Type getSourcePropertyGenericType(java.lang.String propertyName)Get the generic type of a source property.- Parameters:
propertyName- name of the source property- Returns:
- the generic type of the source property
- Throws:
java.lang.IllegalArgumentException- if binder does not define this source property
-
getTargetPropertyType
public java.lang.Class<?> getTargetPropertyType(java.lang.String propertyName)Get the type of a target property.- Parameters:
propertyName- name of the target property- Returns:
- the type of the target property
- Throws:
java.lang.IllegalArgumentException- if binder does not define this target property
-
getTargetPropertyGenericType
public java.lang.reflect.Type getTargetPropertyGenericType(java.lang.String propertyName)Get the generic type of a target property.- Parameters:
propertyName- name of the target property- Returns:
- the generic type of the target property
- Throws:
java.lang.IllegalArgumentException- if binder does not define this target property
-
copy
protected void copy(I source, O target, boolean excludeProperties, java.lang.String... propertyNames) throws java.lang.RuntimeExceptionCopy properties from a source bean to a destination one according to the model of the binder. Note: Iffromobject is null, thennullvalues will be set to mapped properties intodst.- Parameters:
source- the bean to readtarget- the bean to writeexcludeProperties- true to exclude followingpropertyNamespropertyNames- optional subset of properties to copy (if none is specifed, will use all the properties defined in binder)- Throws:
java.lang.NullPointerException- if target parameter isnulljava.lang.RuntimeException- if a property can not be copied to the target object
-
readProperty
protected java.lang.Object readProperty(java.lang.String sourceProperty, java.lang.Object source, java.lang.reflect.Method readMethod) -
diff
protected java.util.List<PropertyDiff> diff(I source, O target, boolean excludeProperties, java.lang.String... propertyNames) -
diff
Compare two beans property by property according to the model. List contains one element per property with different values (according to the result of an equals() call)- Parameters:
source- a bean of type Itarget- a bean of type O- Returns:
- a list with all the properties which values differ in source and target. Properties with equal values are not included.
- Since:
- 2.3
-
diffExcluding
public java.util.List<PropertyDiff> diffExcluding(I source, O target, java.lang.String... propertyNames)Compare two beans property by property according to the model. List contains one element per property with different values (according to the result of an equals() call)- Parameters:
source- a bean of type Itarget- a bean of type OpropertyNames- property names to exclude from the diff- Returns:
- a list with all the properties which values differ in source and target. Properties with equal values and excluded properties will not be contained in the result
- Since:
- 2.3
-
getModel
Get the model of the binder.- Returns:
- the model of the binder
-
setModel
Set the model of the binder.- Parameters:
model- the model of the binder
-
getProperties
protected java.lang.String[] getProperties(java.lang.String... propertyNames)Obtain the properties, if none is given inpropertyNamesparameter, will use all property names defined in binder's model, otherwise, check that all given property names are safe (registred in binder's model).- Parameters:
propertyNames- optional subset of properties to get- Returns:
- the array of property names
-
getAllPropertiesExclude
protected java.lang.String[] getAllPropertiesExclude(java.lang.String... propertyNameExcludes)Obtains all properties from binder's model except thosepropertyNameExcludes. Unknown properties will be ignored.- Parameters:
propertyNameExcludes- name of properties to exclude- Returns:
- the array of property names without those in argument
-
getCollectionValue
protected java.lang.Object getCollectionValue(java.lang.String sourceProperty, java.lang.Object readValue) -
bindProperty
protected java.lang.Object bindProperty(java.lang.String sourceProperty, java.lang.Object read) throws java.lang.IllegalAccessException, java.lang.InstantiationException- Throws:
java.lang.IllegalAccessExceptionjava.lang.InstantiationException
-
bindCollection
protected java.lang.Object bindCollection(java.lang.String sourceProperty, java.lang.Object read) throws java.lang.IllegalAccessException, java.lang.InstantiationException- Throws:
java.lang.IllegalAccessExceptionjava.lang.InstantiationException
-
bind
protected java.lang.Object bind(Binder binder, java.lang.Object read) throws java.lang.IllegalAccessException, java.lang.InstantiationException- Throws:
java.lang.IllegalAccessExceptionjava.lang.InstantiationException
-