giovedì 1 ottobre 2015

Partial mapping with JMapper

Hi guys,

with this post i will illustrate how is possible define a mapping strategy with JMapper.
The classic scenario is a mapping where value from source must be passed to destination, but there are cases where what we need is pass from source only valorized fields or.. we want that only the null fields of destination will be mapped.. this cases are handled very well by JMapper.

The following example will use these classes:

class Destination {              class Source {

   @JMap String name;               String name;
   @JMap Integer age;               Integer age;
   @JMap String company;            String company;

   // getters and setters..         // getters and setters..

}                                }

JMapper istance generation:

JMapper<Destination, Source> mapper = new JMapper<>(Destination.class, Source.class);

Ok, it's arrived the moment to see what Jmapper permits to do.
This are the filled beans:
Destination destination = new Destination("empty", null, "Anonymous");
Source source = new Source("Alessandro", 30, null);

We want to take only the valued fields of Source:

Destination result = mapper.getDestination(destination, source, MappingType.ALL_FIELDS, MappingType.ONLY_VALUED_FIELDS);
Result:
Destination[ name="Alessandro", age=30, company="Anonymous"]
We want just fill null fields of Destination:

Destination result = mapper.getDestination(destination, source, MappingType.ONLY_NULL_FIELDS, MappingType.ALL_FIELDS);

Result:
Destination[ name="empty", age=30, company="Anonymous"]

There are a lot of other combinations.. try to find yourself    :)

Nessun commento:

Posta un commento