Last active
October 18, 2021 09:02
-
-
Save drodil/bce67cddf2a758e686cf68768c647cb4 to your computer and use it in GitHub Desktop.
Optional.orElse failure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Session { | |
public String getCustomerName() { | |
// Some checks and throws exception if session not started | |
throw new SessionNotStartedException(); | |
} | |
public static Session getCurrentSession() { | |
return new Session(); | |
} | |
} | |
public class App { | |
public static void main(String[] args) { | |
Application application = new Application(); // coming as function parameter | |
Session session = Session.getCurrentSession(); | |
String customerName = Optional.ofNullable(application) | |
.map(Application::getCustomerName) | |
.orElse(session.getCustomerName()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment