Class PropertyMapperCreator<From,​To>

  • Type Parameters:
    From - The source class for the mapping
    To - 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
    • Constructor Detail

      • PropertyMapperCreator

        public PropertyMapperCreator()
        Creates a new empty property mapper creator and initializes all internal fields.
    • 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 mapping
        T2 - the type of the second mapping
        Parameters:
        from1 - the first from mapping
        from2 - 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 mapping
        T2 - the type of the second mapping
        Parameters:
        from1 - the first from mapping
        from2 - the second from mapping
        Returns:
        a property mapper creator from if