Lets assume I have class MySource
:
public class MySource {
public String fieldA;
public String fieldB;
public MySource(String A, String B) {
this.fieldA = A;
this.fieldB = B;
}
}
and I would like to translate it to object MyTarget
:
public class MyTarget {
public String fieldA;
public String fieldB;
}
using default ModelMapper settings I can achieve it in the following way:
ModelMapper modelMapper = new ModelMapper();
MySource src = new MySource("A field", "B field");
MyTarget trg = modelMapper.map(src, MyTarget.class); //success! fields are copied
It can happen however, that MySource
object will be null
. In this case, MyTarget will be also null
:
ModelMapper modelMapper = new ModelMapper();
MySource src = null;
MyTarget trg = modelMapper.map(src, MyTarget.class); //trg = null
I would like to specify custom mapping in such a way, that (pseudo code):
MySource src != null ? [perform default mapping] : [return new MyTarget()]
Does anybody have idea how to write an appropriate converter to achieve that?