How can I map with modelmapper some conditions?

Viewed 78

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

1 Answers

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);
...
Related