I
- the source bean typeO
- the destination bean typepublic class Binder<I,O> extends Object implements Serializable
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...BinderFactory
,
BinderModelBuilder
,
Serialized FormModifier and Type | Class and Description |
---|---|
static class |
Binder.BinderModel<S,T>
Model of a
Binder . |
static class |
Binder.CollectionStrategy
Types of loading of collections.
|
Modifier and Type | Field and Description |
---|---|
protected Binder.BinderModel<I,O> |
model
the model of the binder
|
Constructor and Description |
---|
Binder() |
Modifier and Type | Method and Description |
---|---|
protected Object |
bind(Binder binder,
Object read) |
protected Object |
bindCollection(String sourceProperty,
Object read) |
protected Object |
bindProperty(String sourceProperty,
Object read) |
protected void |
copy(I source,
O target,
boolean excludeProperties,
String... propertyNames)
Copy properties from a source bean to a destination one according to
the model of the binder.
|
void |
copy(I source,
O target,
String... propertyNames)
Copy properties from a source bean to a destination one according to
the model of the binder.
|
void |
copyExcluding(I source,
O target,
String... propertyNames)
Copy properties from a source bean to a destination one according to
the model of the binder excluding
propertyNames . |
List<PropertyDiff> |
diff(I source,
O target)
Compare two beans property by property according to the model.
|
protected List<PropertyDiff> |
diff(I source,
O target,
boolean excludeProperties,
String... propertyNames) |
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 those
propertyNameExcludes . |
protected Object |
getCollectionValue(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 in
propertyNames
parameter, will use all property names defined in binder's model,
otherwise, check that all given property names are safe (registred in
binder's model). |
Type |
getSourcePropertyGenericType(String propertyName)
Get the generic type of a source property.
|
Class<?> |
getSourcePropertyType(String propertyName)
Get the type of a source property.
|
Class<I> |
getSourceType()
Obtains the type of the source bean.
|
Type |
getTargetPropertyGenericType(String propertyName)
Get the generic type of a target property.
|
Class<?> |
getTargetPropertyType(String propertyName)
Get the type of a target property.
|
Class<O> |
getTargetType()
Obtains the type of the target bean.
|
void |
injectProperties(Map<String,Object> properties,
O target)
Inject all not null properties to the target bean.
|
void |
injectProperties(Map<String,Object> properties,
O target,
boolean includeNullValues)
Inject all properties to the target bean.
|
Map<String,Object> |
obtainProperties(I source,
boolean keepPrimitiveDefaultValues,
boolean includeNullValues,
String... propertyNames)
Obtain from the given object all properties registered in the binder model.
|
Map<String,Object> |
obtainProperties(I source,
boolean includeNullValues,
String... propertyNames)
Obtain from the given object all properties registered in the binder
model.
|
Map<String,Object> |
obtainProperties(I source,
String... propertyNames)
Obtain from the given object all properties registered in the binder
model.
|
<OO> OO |
obtainSourceProperty(I source,
String propertyName)
Obtain a property from a source object (A source object type reflect
the source type of the binder).
|
<OO> OO |
obtainTargetProperty(O target,
String propertyName)
Obtain a property from a target object (A target object type reflect
the target type of the binder).
|
protected Object |
readProperty(String sourceProperty,
Object source,
Method readMethod) |
protected void |
setModel(Binder.BinderModel<I,O> model)
Set the model of the binder.
|
protected Object |
transform(String propertyName,
Object propertyValue) |
protected Binder.BinderModel<I,O> model
public Class<I> getSourceType()
public Class<O> getTargetType()
public Map<String,Object> obtainProperties(I source, boolean keepPrimitiveDefaultValues, boolean includeNullValues, String... propertyNames)
source
- the bean to readpropertyNames
- subset of properties to loadkeepPrimitiveDefaultValues
- to keep primitive default value and not replace them by a null
value.includeNullValues
- get all the properties and values for the given bean. If false, you'll get only the valuesfrom
is null
.public Map<String,Object> obtainProperties(I source, boolean includeNullValues, String... propertyNames)
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 valuesfrom
is null
.public Map<String,Object> obtainProperties(I source, String... propertyNames)
source
- the bean to readpropertyNames
- subset of properties to loadfrom
is null
.public <OO> OO obtainSourceProperty(I source, String propertyName)
OO
- type of property to getsource
- the source object to inspectpropertyName
- name of the property to getpublic <OO> OO obtainTargetProperty(O target, String propertyName)
OO
- type of property to gettarget
- the target object to inspectpropertyName
- name of the property to getpublic void injectProperties(Map<String,Object> properties, O target)
properties
- properties to set into beantarget
- the bean to setpublic void injectProperties(Map<String,Object> properties, O target, boolean includeNullValues)
properties
- properties to set into beantarget
- the bean to setincludeNullValues
- true
to set also null properties valuespublic void copy(I source, O target, String... propertyNames)
propertyNames
is defined, only
those properties will be copied.
Note: If from
object is null, then null
values
will be set to mapped properties into dst
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)NullPointerException
- if target parameter is null
public void copyExcluding(I source, O target, String... propertyNames)
propertyNames
.
Note: If from
object is null, then null
values
will be set to mapped properties into dst
.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)NullPointerException
- if target parameter is null
public Class<?> getSourcePropertyType(String propertyName)
propertyName
- name of the source propertyIllegalArgumentException
- if binder does not define this source propertypublic Type getSourcePropertyGenericType(String propertyName)
propertyName
- name of the source propertyIllegalArgumentException
- if binder does not define this source propertypublic Class<?> getTargetPropertyType(String propertyName)
propertyName
- name of the target propertyIllegalArgumentException
- if binder does not define this target propertypublic Type getTargetPropertyGenericType(String propertyName)
propertyName
- name of the target propertyIllegalArgumentException
- if binder does not define this target propertyprotected void copy(I source, O target, boolean excludeProperties, String... propertyNames) throws RuntimeException
from
object is null, then null
values
will be set to mapped properties into dst
.source
- the bean to readtarget
- the bean to writeexcludeProperties
- true to exclude following propertyNames
propertyNames
- optional subset of properties to copy (if none is
specifed, will use all the properties defined in
binder)NullPointerException
- if target parameter is null
RuntimeException
- if a property can not be copied to the target objectprotected List<PropertyDiff> diff(I source, O target, boolean excludeProperties, String... propertyNames)
public List<PropertyDiff> diff(I source, O target)
source
- a bean of type Itarget
- a bean of type Opublic List<PropertyDiff> diffExcluding(I source, O target, String... propertyNames)
source
- a bean of type Itarget
- a bean of type OpropertyNames
- property names to exclude from the diffprotected Binder.BinderModel<I,O> getModel()
protected void setModel(Binder.BinderModel<I,O> model)
model
- the model of the binderprotected String[] getProperties(String... propertyNames)
propertyNames
parameter, will use all property names defined in binder's model,
otherwise, check that all given property names are safe (registred in
binder's model).propertyNames
- optional subset of properties to getprotected String[] getAllPropertiesExclude(String... propertyNameExcludes)
propertyNameExcludes
. Unknown properties will be ignored.propertyNameExcludes
- name of properties to excludeprotected Object bindProperty(String sourceProperty, Object read) throws IllegalAccessException, InstantiationException
protected Object bindCollection(String sourceProperty, Object read) throws IllegalAccessException, InstantiationException
protected Object bind(Binder binder, Object read) throws IllegalAccessException, InstantiationException
Copyright © 2004–2020 CodeLutin. All rights reserved.