org.nuiton.util.beans
Class Binder<I,O>

java.lang.Object
  extended by org.nuiton.util.beans.Binder<I,O>
Type Parameters:
I - the source bean type
O - the destination bean type
All Implemented Interfaces:
Serializable

public class Binder<I,O>
extends Object
implements 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:
tchemit
See Also:
BinderFactory, BinderModelBuilder, Serialized Form

Nested Class Summary
static class Binder.BinderModel<S,T>
          Model of a Binder.
static class Binder.CollectionStrategy
          Types of loading of collections.
 class Binder.PropertyDiff
           
 
Field Summary
protected  Binder.BinderModel<I,O> model
          the model of the binder
 
Constructor Summary
Binder()
           
 
Method Summary
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<Binder.PropertyDiff> diff(I source, O target)
          Compare two beans property by property according to the model.
protected  List<Binder.PropertyDiff> diff(I source, O target, boolean excludeProperties, String... propertyNames)
           
 List<Binder.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).
 Class<I> getSourceType()
          Obtains the type of the source bean.
 Class<O> getTargetType()
          Obtains the type of the target bean.
 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.
protected  Object readProperty(String sourceProperty, Object source, Method readMethod)
           
protected  void setModel(Binder.BinderModel<I,O> model)
          Set the model of the binder.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

model

protected Binder.BinderModel<I,O> model
the model of the binder

Constructor Detail

Binder

public Binder()
Method Detail

getSourceType

public Class<I> getSourceType()
Obtains the type of the source bean.

Returns:
the type of the source bean

getTargetType

public Class<O> getTargetType()
Obtains the type of the target bean.

Returns:
the type of the target bean

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 read
propertyNames - subset of properties to load
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 from is null.
Since:
2.3

obtainProperties

public Map<String,Object> obtainProperties(I source,
                                           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 read
propertyNames - subset of properties to load
Returns:
the map of properties obtained indexed by their property name, or an empty map is the given from is null.

copy

public 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. If 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

Parameters:
source - the bean to read
target - the bean to write
propertyNames - optional subset of properties to copy (if none is specifed, will use all the properties defined in binder)
Throws:
NullPointerException - if target parameter is null

copyExcluding

public 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.

Note: If from object is null, then null values will be set to mapped properties into dst.

Parameters:
source - the bean to read
target - the bean to write
propertyNames - optional subset of properties to copy (if none is specifed, will use all the properties defined in binder)
Throws:
NullPointerException - if target parameter is null

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: If from object is null, then null values will be set to mapped properties into dst.

Parameters:
source - the bean to read
target - the bean to write
excludeProperties - true to exclude following propertyNames
propertyNames - optional subset of properties to copy (if none is specifed, will use all the properties defined in binder)
Throws:
NullPointerException - if target parameter is null
RuntimeException - if a property can not be copied to the target object

readProperty

protected Object readProperty(String sourceProperty,
                              Object source,
                              Method readMethod)

diff

protected List<Binder.PropertyDiff> diff(I source,
                                         O target,
                                         boolean excludeProperties,
                                         String... propertyNames)

diff

public List<Binder.PropertyDiff> diff(I source,
                                      O target)
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 I
target - 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 List<Binder.PropertyDiff> diffExcluding(I source,
                                               O target,
                                               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 I
target - a bean of type O
propertyNames - 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

protected Binder.BinderModel<I,O> getModel()
Get the model of the binder.

Returns:
the model of the binder

setModel

protected void setModel(Binder.BinderModel<I,O> model)
Set the model of the binder.

Parameters:
model - the model of the binder

getProperties

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).

Parameters:
propertyNames - optional subset of properties to get
Returns:
the array of property names

getAllPropertiesExclude

protected String[] getAllPropertiesExclude(String... propertyNameExcludes)
Obtains all properties from binder's model except those propertyNameExcludes. Unknown properties will be ignored.

Parameters:
propertyNameExcludes - name of properties to exclude
Returns:
the array of property names without those in argument

getCollectionValue

protected Object getCollectionValue(String sourceProperty,
                                    Object readValue)

bindProperty

protected Object bindProperty(String sourceProperty,
                              Object read)
                       throws IllegalAccessException,
                              InstantiationException
Throws:
IllegalAccessException
InstantiationException

bindCollection

protected Object bindCollection(String sourceProperty,
                                Object read)
                         throws IllegalAccessException,
                                InstantiationException
Throws:
IllegalAccessException
InstantiationException

bind

protected Object bind(Binder binder,
                      Object read)
               throws IllegalAccessException,
                      InstantiationException
Throws:
IllegalAccessException
InstantiationException


Copyright © 2004-2011 CodeLutin. All Rights Reserved.