I want to apply different logic to Optional value like below
Optional<String> alarm = getAlarm();
if (alarm.isPresent()) {
testAutoStopAndRollback(alarm.get());
} else {
testWithoutAutoStopAndRollback();
}
Is there a more concise way to implement the same logic?
I know I can move half of the logic to ifPresent()
alarm.ifPresent(name -> testAutoStopAndRollback(name));
How can I do the rest part?
To clarify the question, my code is stick to Java 8 so I won't be able to use ifPresentOrElse in Java 9.