Class Binder<I,​O>

java.lang.Object
org.nuiton.util.beans.Binder<I,​O>
Type Parameters:
I - the source bean type
O - 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 class  Binder.BinderModel<S,​T>
    Model of a Binder.
    static class  Binder.CollectionStrategy
    Types of loading of collections.
  • Field Summary

    Fields 
    Modifier and Type Field Description
    protected Binder.BinderModel<I,​O> model
    the model of the binder
  • Constructor Summary

    Constructors 
    Constructor Description
    Binder()  
  • Method Summary

    Modifier and Type Method Description
    protected java.lang.Object bind​(Binder binder, java.lang.Object read)  
    protected java.lang.Object bindCollection​(java.lang.String sourceProperty, java.lang.Object read)  
    protected java.lang.Object bindProperty​(java.lang.String sourceProperty, java.lang.Object read)  
    protected void copy​(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.
    void copy​(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.
    void copyExcluding​(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 excluding propertyNames.
    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 those propertyNameExcludes.
    protected java.lang.Object getCollectionValue​(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 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).
    java.lang.reflect.Type getSourcePropertyGenericType​(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.Type getTargetPropertyGenericType​(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.
    void injectProperties​(java.util.Map<java.lang.String,​java.lang.Object> properties, O target)
    Inject all not null properties to the target bean.
    void injectProperties​(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> OO obtainSourceProperty​(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> OO obtainTargetProperty​(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.Object readProperty​(java.lang.String sourceProperty, java.lang.Object source, java.lang.reflect.Method readMethod)  
    protected void setModel​(Binder.BinderModel<I,​O> model)
    Set the model of the binder.
    protected java.lang.Object transform​(java.lang.String propertyName, java.lang.Object propertyValue)  

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

  • Method Details

    • getSourceType

      public java.lang.Class<I> getSourceType()
      Obtains the type of the source bean.
      Returns:
      the type of the source bean
    • getTargetType

      public java.lang.Class<O> 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 read
      propertyNames - subset of properties to load
      keepPrimitiveDefaultValues - 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 values
      Returns:
      the map of properties obtained indexed by their property name, or an empty map is the given from is null.
      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 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 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 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.
    • obtainSourceProperty

      public <OO> OO obtainSourceProperty​(I source, java.lang.String propertyName)
      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 inspect
      propertyName - name of the property to get
      Returns:
      the property value in the source object.
      Since:
      3.0
    • obtainTargetProperty

      public <OO> OO obtainTargetProperty​(O target, java.lang.String propertyName)
      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 inspect
      propertyName - 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 bean
      target - 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 bean
      target - the bean to set
      includeNullValues - true to set also null properties values
      Since:
      3.0
    • transform

      protected java.lang.Object transform​(java.lang.String propertyName, java.lang.Object propertyValue)
    • copy

      public void copy​(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. 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:
      java.lang.NullPointerException - if target parameter is null
    • copyExcluding

      public void copyExcluding​(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 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:
      java.lang.NullPointerException - if target parameter is null
    • 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.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:
      java.lang.NullPointerException - if target parameter is null
      java.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

      public java.util.List<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 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 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 java.lang.String[] getProperties​(java.lang.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 java.lang.String[] getAllPropertiesExclude​(java.lang.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 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.IllegalAccessException
      java.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.IllegalAccessException
      java.lang.InstantiationException
    • bind

      protected java.lang.Object bind​(Binder binder, java.lang.Object read) throws java.lang.IllegalAccessException, java.lang.InstantiationException
      Throws:
      java.lang.IllegalAccessException
      java.lang.InstantiationException