'How can I map with modelmapper some conditions?
I have some Game entity that I want to map in GameDto. In Game I have another Game field basedOn. In GameDto I have basedOnName, basedOnId and basedOnCheckBox fields.
Can I somehow map basedOnCheckBox to true in case of Game has basedOn != null or in GameDto basedOnName/Id != null?
Now my code for this operation:
if(gameDto.getBaseOnName() != null) {
gameDto.setBaseOnCheckBox(true);
}
I'm usind modelMapper v.3.1.0
Solution 1:[1]
Looks like found an answer how to do it:
Converter<Game, Boolean> notNullFieldToBoolean = c -> c.getSource() != null;
...
mapper.using(notNullFieldToBoolean).map(Game::getBaseOn, GameDto::setBaseOnCheckBox);
...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | BTRco |
