Skip to content

Instantly share code, notes, and snippets.

@drodil
Last active October 18, 2021 09:02
Show Gist options
  • Save drodil/bce67cddf2a758e686cf68768c647cb4 to your computer and use it in GitHub Desktop.
Save drodil/bce67cddf2a758e686cf68768c647cb4 to your computer and use it in GitHub Desktop.
Optional.orElse failure
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