Package org.kiwiproject.beans
Class BeanConverter<T>
- java.lang.Object
-
- org.kiwiproject.beans.BeanConverter<T>
-
public class BeanConverter<T> extends Object
Simple way to convert one bean to another. This utility uses spring-beans to attempt the conversion at first. If attempting to maps, it will attempt to do simple copies of the key-value pairs.Exclusion lists can be provided to ignore specific fields.
Also custom mappers can be provided per field for more control over how the fields are converted.
NOTE: This class requires spring-beans as a dependency.
-
-
Constructor Summary
Constructors Constructor Description BeanConverter()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddPropertyMapper(String propertyName, Function<T,?> function)Adds a property mapper function for a specific property nameTconvert(T input)This conversion method taks a single parameter and modifies the object in place.<R> Rconvert(T input, R target)This conversion method takes two parameters and copies properties from one object to anotherprotected Set<String>getPropertyList(T input, org.springframework.beans.BeanWrapper inputWrapper)FunctiongetPropertyMapper(String propertyName)Get the property mapper function for a specific property namebooleanhasPropertyMapper(String propertyName)Checks to see if a property mapper exists for a given property nameprotected voidlogOrFail(String msg, String propName, RuntimeException e)protected ObjectreadBeanValue(T input, org.springframework.beans.BeanWrapper inputWrapper, String propName)protected <R> voidwriteBeanValue(R target, org.springframework.beans.BeanWrapper targetWrapper, String propName, Object inputValue)
-
-
-
Method Detail
-
convert
public T convert(T input)
This conversion method taks a single parameter and modifies the object in place.- Parameters:
input- the object to perform the conversion on- Returns:
- the same object that was passed in
-
convert
public <R> R convert(T input, R target)
This conversion method takes two parameters and copies properties from one object to another- Type Parameters:
R- the type of object being returned- Parameters:
input- the object to copy the properties fromtarget- the object to copy the properties too (destination)- Returns:
- the modified target object
-
getPropertyList
protected Set<String> getPropertyList(T input, org.springframework.beans.BeanWrapper inputWrapper)
-
hasPropertyMapper
public boolean hasPropertyMapper(String propertyName)
Checks to see if a property mapper exists for a given property name- Parameters:
propertyName- the property name- Returns:
- true if a property mapper was found for that property
-
getPropertyMapper
public Function getPropertyMapper(String propertyName)
Get the property mapper function for a specific property name- Parameters:
propertyName- the property name- Returns:
- the Function that will be triggered
-
readBeanValue
protected Object readBeanValue(T input, org.springframework.beans.BeanWrapper inputWrapper, String propName)
-
logOrFail
protected void logOrFail(String msg, String propName, RuntimeException e)
-
writeBeanValue
protected <R> void writeBeanValue(R target, org.springframework.beans.BeanWrapper targetWrapper, String propName, Object inputValue)
-
addPropertyMapper
public void addPropertyMapper(String propertyName, Function<T,?> function)
Adds a property mapper function for a specific property name- Parameters:
propertyName- the property namefunction- the Function that will be triggered- Throws:
IllegalStateException- if a mapper is already registered on the given property
-
-