I have fallowing code:
public Osoba updateOsoba(String input) throws Exception {
class FieldCopier { // in progress....
void updateField() {
}
}
Osoba in = null;
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
in = mapper.readValue(input, Osoba.class);
final int osobaId = Optional
.ofNullable(in.getOsobaId())
.orElseThrow(() -> new Exception("....."));
Osoba os = osobaDao.findOne(osobaId);
if (in.getOsobaImiona() != null) os.setOsobaImiona(in.getOsobaImiona());
if (in.getOsobaNazwisko() != null) os.setOsobaNazwisko(in.getOsobaNazwisko());
//....
return in;
}
This construction in my opinion is susceptible for mistakes:
if (in.getOsobaImiona() != null) os.setOsobaImiona(in.getOsobaImiona());
I have an idea to create a function that makes this change more simple, so I created inner class FieldCopier with method updateField(). I want to do something like that:
FieldCopier fc = new FieldCopier();
fc.updateField(in::getOsobaImiona, os::setOsobaImiona)
instead:
if (in.getOsobaImiona() != null) os.setOsobaImiona(in.getOsobaImiona());
But I have no idea how to implement method updateField(), I want to use reference to function but I don't know how. Is it possible to do it in that way? Or maybe there is a better way to do that?