Skip to content

Enumeration examples

avurro edited this page Oct 1, 2015 · 3 revisions

The following examples show the usage of the enumerations: NullPointerControl and MappingType.
The classes used are:

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..

}                                }

And the mapper used is created as follows:

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

MappingType

In this example you will see a case where you need to take only the valued fields of Source:

Destination destination = new Destination("empty", null, "Anonymous");
Source source = new Source("Alessandro", 30, null);

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

result:

Destination[ name="Alessandro", age=30, company="Anonymous"]

If you want just fill null fields of Destination:

Destination destination = new Destination("empty", null, "Anonymous");
Source source = new Source("Alessandro", 30, null);

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

result:

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

the possible combinations of mapping are many, try yourself to explore the other combinations.

NullPointerControl

You can check the source using NullPointerControl enumeration.
This enumeration avoids an explicit control, as shows by the following example:

Source source = null;

Destination destination = mapper.getDestination(source, NullPointerControl.SOURCE, MappingType.ALL_FIELDS);

assertNull(destination);

No error is thrown, the mapping isn't executed, a null is returned instead.

For more information see the enumerations wiki page.

Clone this wiki locally