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:
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classModel of aBinder.static enumTypes of loading of collections. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected Objectprotected ObjectbindCollection(String sourceProperty, Object read) protected ObjectbindProperty(String sourceProperty, Object read) protected voidCopy properties from a source bean to a destination one according to the model of the binder.voidCopy properties from a source bean to a destination one according to the model of the binder.voidcopyExcluding(I source, O target, String... propertyNames) Copy properties from a source bean to a destination one according to the model of the binder excludingpropertyNames.Compare two beans property by property according to the model.protected List<PropertyDiff> diffExcluding(I source, O target, String... propertyNames) Compare two beans property by property according to the model.protected String[]getAllPropertiesExclude(String... propertyNameExcludes) Obtains all properties from binder's model except thosepropertyNameExcludes.protected ObjectgetCollectionValue(String sourceProperty, Object readValue) protected Binder.BinderModel<I, O> getModel()Get the model of the binder.protected String[]getProperties(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).getSourcePropertyGenericType(String propertyName) Get the generic type of a source property.Class<?> getSourcePropertyType(String propertyName) Get the type of a source property.Obtains the type of the source bean.getTargetPropertyGenericType(String propertyName) Get the generic type of a target property.Class<?> getTargetPropertyType(String propertyName) Get the type of a target property.Obtains the type of the target bean.voidinjectProperties(Map<String, Object> properties, O target) Inject all not null properties to the target bean.voidinjectProperties(Map<String, Object> properties, O target, boolean includeNullValues) Inject all properties to the target bean.obtainProperties(I source, boolean keepPrimitiveDefaultValues, boolean includeNullValues, String... propertyNames) Obtain from the given object all properties registered in the binder model.obtainProperties(I source, boolean includeNullValues, String... propertyNames) Obtain from the given object all properties registered in the binder model.obtainProperties(I source, String... propertyNames) Obtain from the given object all properties registered in the binder model.<OO> OOobtainSourceProperty(I source, String propertyName) Obtain a property from a source object (A source object type reflect the source type of the binder).<OO> OOobtainTargetProperty(O target, String propertyName) Obtain a property from a target object (A target object type reflect the target type of the binder).protected ObjectreadProperty(String sourceProperty, Object source, Method readMethod) protected voidsetModel(Binder.BinderModel<I, O> model) Set the model of the binder.protected Object
-
Field Details
-
model
the model of the binder
-
-
Constructor Details
-
Binder
public Binder()
-
-
Method Details
-
getSourceType
-
getTargetType
-
obtainProperties
public Map<String,Object> obtainProperties(I source, boolean keepPrimitiveDefaultValues, boolean includeNullValues, String... propertyNames) Obtain from the given object all properties registered in the binder model.- Parameters:
source- the bean to readkeepPrimitiveDefaultValues- 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 valuespropertyNames- subset of properties to load- Returns:
- the map of properties obtained indexed by their property name,
or an empty map is the given
fromisnull. - Since:
- 3.0
-
obtainProperties
public Map<String,Object> obtainProperties(I source, boolean includeNullValues, String... propertyNames) Obtain from the given object all properties registered in the binder model.- Parameters:
source- the bean to readincludeNullValues- get all the properties and values for the given bean. If false, you'll get only the valuespropertyNames- subset of properties to load- Returns:
- the map of properties obtained indexed by their property name,
or an empty map is the given
fromisnull. - Since:
- 2.3
-
obtainProperties
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
-
injectProperties
-
transform
-
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:
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:
NullPointerException- if target parameter isnull
-
getSourcePropertyType
Get the type of a source property.- Parameters:
propertyName- name of the source property- Returns:
- the type of the source property
- Throws:
IllegalArgumentException- if binder does not define this source property
-
getSourcePropertyGenericType
Get the generic type of a source property.- Parameters:
propertyName- name of the source property- Returns:
- the generic type of the source property
- Throws:
IllegalArgumentException- if binder does not define this source property
-
getTargetPropertyType
Get the type of a target property.- Parameters:
propertyName- name of the target property- Returns:
- the type of the target property
- Throws:
IllegalArgumentException- if binder does not define this target property
-
getTargetPropertyGenericType
Get the generic type of a target property.- Parameters:
propertyName- name of the target property- Returns:
- the generic type of the target property
- Throws:
IllegalArgumentException- if binder does not define this target property
-
copy
protected void copy(I source, O target, boolean excludeProperties, String... propertyNames) throws RuntimeException Copy 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:
NullPointerException- if target parameter isnullRuntimeException- if a property can not be copied to the target object
-
readProperty
-
diff
protected List<PropertyDiff> diff(I source, O target, boolean excludeProperties, 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
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
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
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
-
bindProperty
protected Object bindProperty(String sourceProperty, Object read) throws IllegalAccessException, InstantiationException -
bindCollection
protected Object bindCollection(String sourceProperty, Object read) throws IllegalAccessException, InstantiationException -
bind
protected Object bind(Binder binder, Object read) throws IllegalAccessException, InstantiationException
-