Package science.aist.jack.general
Class PropertyMapperCreator<From,To>
- java.lang.Object
-
- science.aist.jack.general.PropertyMapperCreator<From,To>
-
- Type Parameters:
From
- The source class for the mappingTo
- The target class for the mapping
public class PropertyMapperCreator<From,To> extends Object
This class creates a PropertyMapper, that is based on the ideas of Andreas Schuler PropertyMapper from the archetype
The property mapper creator is able to define mappings from one class into another class. Therefore different mappings can be defined, then the actual PropertyMapper can be created using
create()
method, which is going to create a Function, which then contains the mapping between those to elements.
Example:// Both classes have getters and setters for the properties. class Person { String firstName; String lastName; LocalDate dateOfBirth; } class PersonDTO { String firstName; String lastName; String dateOfBirth; } // Mapping from a Person to a PersonDTO Function<Person, PersonDTO> propertyMapper = new PropertyMapperCreator<Person, PersonDTO>() .from(Person::getFirstName).to(PersonDTO::setFirstName) // defines a normal mapping, if types are matching .from(Person::getLastName).to(PersonDTO::setLastName) // defines a normal mapping, if types are matching .from(Person::getDateOfBirth).toWith(PersonDTO::setDateOfBirth).with(ld -> ld.format(DateTimeFormatter.ofPattern("dd.MM.yyyy"))) // defines a complexer mapping with a mapping function (if types are not matching or additional transformation is required) .create(PersonDTO::new);
- Since:
- 2.0, 2.0
- Version:
- 1.0
- Author:
- Andreas Pointner
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
PropertyMapperCreator.PropertyMapperCreatorFromIf<T>
class
PropertyMapperCreator.PropertyMapperCreatorFromIf2<T1,T2>
class
PropertyMapperCreator.PropertyMapperCreatorFromIfN
class
PropertyMapperCreator.PropertyMapperCreatorTo<T>
The inner class which is used to create the to-mapping.class
PropertyMapperCreator.PropertyMapperCreatorTo2<T1,T2>
class
PropertyMapperCreator.PropertyMapperCreatorToN
-
Constructor Summary
Constructors Constructor Description PropertyMapperCreator()
Creates a new empty property mapper creator and initializes all internal fields.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description BiFunction<From,To,To>
create()
Creates the PropertyMapperFunction<From,To>
create(Supplier<To> elementCreator)
Creates the PropertyMapperPropertyMapperCreator.PropertyMapperCreatorToN
from(Function<From,?>... from)
<T> PropertyMapperCreator.PropertyMapperCreatorTo<T>
from(Function<From,T> from)
From which property the mapping shell start<T1,T2>
PropertyMapperCreator.PropertyMapperCreatorTo2<T1,T2>from(Function<From,T1> from1, Function<From,T2> from2)
Defines two from mappings, that can then be mapped to a single to mappingPropertyMapperCreator.PropertyMapperCreatorFromIfN
fromIf(Function<From,?>... from)
<T> PropertyMapperCreator.PropertyMapperCreatorFromIf<T>
fromIf(Function<From,T> from)
From which property the mapping shell start<T1,T2>
PropertyMapperCreator.PropertyMapperCreatorFromIf2<T1,T2>fromIf(Function<From,T1> from1, Function<From,T2> from2)
Defines two from mappings, that can then be mapped to a single to mapping
-
-
-
Method Detail
-
create
public Function<From,To> create(Supplier<To> elementCreator)
Creates the PropertyMapper
- Parameters:
elementCreator
- The Supplier for the To element, which is used to create the element before the mapping- Returns:
- returns the property mapper as function
-
create
public BiFunction<From,To,To> create()
Creates the PropertyMapper
- Returns:
- returns the property mapper as bi function, where the to element is the second parameter in the function. The function result, then returns the to element after mapping.
-
from
public <T> PropertyMapperCreator.PropertyMapperCreatorTo<T> from(Function<From,T> from)
From which property the mapping shell start
Example:.from(Person::getFirstName)
- Type Parameters:
T
- the type of the property- Parameters:
from
- The from mapping function- Returns:
- the mapping creator for the to mapping
-
fromIf
public <T> PropertyMapperCreator.PropertyMapperCreatorFromIf<T> fromIf(Function<From,T> from)
From which property the mapping shell start
Example:.fromIf(Person::getFirstName)
- Type Parameters:
T
- the type of the property- Parameters:
from
- The from mapping function- Returns:
- the mapping creator for the to mapping
-
from
public <T1,T2> PropertyMapperCreator.PropertyMapperCreatorTo2<T1,T2> from(Function<From,T1> from1, Function<From,T2> from2)
Defines two from mappings, that can then be mapped to a single to mapping
propertyMapperCreator .from(Person::getFirstName, Person::getLastName);
- Type Parameters:
T1
- the type of the first mappingT2
- the type of the second mapping- Parameters:
from1
- the first from mappingfrom2
- the second from mapping- Returns:
- a property mapper creator to
-
fromIf
public <T1,T2> PropertyMapperCreator.PropertyMapperCreatorFromIf2<T1,T2> fromIf(Function<From,T1> from1, Function<From,T2> from2)
Defines two from mappings, that can then be mapped to a single to mapping
propertyMapperCreator .fromIff(Person::getFirstName, Person::getLastName);
- Type Parameters:
T1
- the type of the first mappingT2
- the type of the second mapping- Parameters:
from1
- the first from mappingfrom2
- the second from mapping- Returns:
- a property mapper creator from if
-
from
@SafeVarargs public final PropertyMapperCreator.PropertyMapperCreatorToN from(Function<From,?>... from)
-
fromIf
@SafeVarargs public final PropertyMapperCreator.PropertyMapperCreatorFromIfN fromIf(Function<From,?>... from)
-
-