-
Notifications
You must be signed in to change notification settings - Fork 41
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);
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.
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.
© 2016 Alessandro Vurro
- Home
- How to map
- Relations
- Conversions
- creation/enrichment
- XML
- Annotation
- API
- Configurations
- Utilities
- Examples
- Articles
- More information
- Performance tests
- Release Notes