-
Notifications
You must be signed in to change notification settings - Fork 41
Global Mapping examples
avurro edited this page Apr 1, 2016
·
5 revisions
The global mapping permits to reduce the configuration in cases that the fields have the same name or all fields are mapped in direction to one.
We have two beans with some variables that have the same names.
@JGlobalMap(excluded={"other"})
class Destination { class Source {
String[] authors; List<String> authors;
Date releaseDate; Date releaseDate;
String releaseVersion; String releaseVersion;
String other; String other;
// getters and setters.. // getters and setters..
} }
The XSD has not been added in order to improve readability, for more information see XSD wiki page.
<jmapper>
<class name="package.Destination">
<global>
<excluded>
<attribute name="other"/>
</excluded>
</global>
</class>
</jmapper>
...
import static com.googlecode.jmapper.api.JMapperAPI.*;
...
new JMapperAPI()
.add(mappedClass(Destination.class)
.add(global()
.excludedAttributes("other")));
List<String> authors = new ArrayList<String>();
authors.add("Alessandro Vurro");
Source source = new Source(authors, new GregorianCalendar(2016, 3, 22).getTime(), "1.6.0", "other");
JMapper<Destination, Source> mapper = new JMapper<>(Destination.class, Source.class);
Destination destination = mapper.getDestination(source);
output:
Destination [authors=[Alessandro Vurro], releaseDate=Mon Mar 22 00:00:00 CET 2016, releaseVersion=1.2.0, other=null]
We have one bean with some variables that point to a single field.
class Destination {
Double overallCost;
public Destination(){
overallCost = 0D;
}
public void setOverallCost(Double singleCost){
this.overallCost += singleCost;
}
// getter..
}
@JGlobalMap("overallCost")
class Source {
Double costEmployees;
Double costStructure;
Double costAdvertising;
// getters and setters..
}
<jmapper>
<class name="package.Source">
<global>
<value name="overallCost"/>
</global>
</class>
</jmapper>
...
import static com.googlecode.jmapper.api.JMapperAPI.*;
...
new JMapperAPI()
.add(mappedClass(Source.class)
.add(global()
.value("overallCost")));
JMapper<Destination, Source> mapper = new JMapper<>(Destination.class, Source.class);
Destination destination = mapper.getDestination(new Source(154080.80D,24000D,12570.20D));
output:
Destination [overallCost=190651.0]
© 2016 Alessandro Vurro
- Home
- How to map
- Relations
- Conversions
- creation/enrichment
- XML
- Annotation
- API
- Configurations
- Utilities
- Examples
- Articles
- More information
- Performance tests
- Release Notes