I am facing a problem while converting old-school if usage to Optional.ifPresent
. Here is the previous version of the code.
State state = State.OK;
final Optional<Person> checkExistingPerson = checkIt();
if(checkExistingPerson.isPresent()) {
Person person = checkExistingPerson.get();
if("blah".equals(person.getName())) {
state = State.DUPLICATE;
} else {
state = State.RESTORED;
}
return Member(person.getId(), state);
}
return Member(null, state);
And here is the Optional.ifPresent
usage
State state = State.OK;
final Optional<Person> checkExistingPerson = checkIt();
checkExistingPerson.ifPresent(person -> {
if("blah".equals(person.getName())) {
state = State.DUPLICATE;
} else {
state = State.NEW;
}
return Member(person.getId(), state);
});
return Member(null, state);
And also, here is the screenshot what IntelliJ forces me to change it.
What is the best approach to use Optional
regarding to my problem? Thx all!