Package org.kiwiproject.beans
Class BeanConverter<T>
java.lang.Object
org.kiwiproject.beans.BeanConverter<T>
Simple way to convert one bean to another. This utility uses spring-beans to attempt the conversion at first.
If attempting to convert 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 -
Method Summary
Modifier and TypeMethodDescriptionvoidaddPropertyMapper(String propertyName, Function<T, ?> function) Adds a property mapper function for a specific property nameThis conversion method takes a single parameter and modifies the object in place.<R> RThis conversion method takes two parameters and copies properties from one object to anotherSet of property names to exclude from copying over.Custom mappers that map specific field names to a function that will accept the data and convert it to the new value.getPropertyMapper(String propertyName) Get the property mapper function for a specific property name.getPropertySet(T input, org.springframework.beans.BeanWrapper inputWrapper) booleanhasPropertyMapper(String propertyName) Checks to see if a property mapper exists for a given property namebooleanAllows for a failed conversion to throw an exception iftrue; otherwise just logs a failure iffalseprotected voidlogOrFail(String msg, String propName, RuntimeException e) protected ObjectreadBeanValue(T input, org.springframework.beans.BeanWrapper inputWrapper, String propName) voidsetExclusions(Set<String> exclusions) Set of property names to exclude from copying over.voidsetFailOnError(boolean failOnError) Allows for a failed conversion to throw an exception iftrue; otherwise just logs a failure iffalseprotected <R> voidwriteBeanValue(R target, org.springframework.beans.BeanWrapper targetWrapper, String propName, Object inputValue)
-
Constructor Details
-
BeanConverter
public BeanConverter()
-
-
Method Details
-
convert
This conversion method takes 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
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
-
getPropertySet
-
hasPropertyMapper
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
Get the property mapper function for a specific property name.It is assumed the caller knows the correct return type for the mapper, otherwise a
ClassCastExceptionwill be thrown at runtime.- Type Parameters:
R- the result type of the mapper for the given property- Parameters:
propertyName- the property name- Returns:
- the Function that will be triggered
-
readBeanValue
-
logOrFail
-
writeBeanValue
-
addPropertyMapper
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
-
getMappers
Custom mappers that map specific field names to a function that will accept the data and convert it to the new value. -
getExclusions
Set of property names to exclude from copying over. Defaults to["class", "new"] -
setExclusions
Set of property names to exclude from copying over. Defaults to["class", "new"] -
isFailOnError
public boolean isFailOnError()Allows for a failed conversion to throw an exception iftrue; otherwise just logs a failure iffalse -
setFailOnError
public void setFailOnError(boolean failOnError) Allows for a failed conversion to throw an exception iftrue; otherwise just logs a failure iffalse
-