Skip to content

Instantly share code, notes, and snippets.

@kortov
Created May 18, 2018 18:43
Show Gist options
  • Save kortov/da5ab1dd51b1c99c335cec60dcfa7e30 to your computer and use it in GitHub Desktop.
Save kortov/da5ab1dd51b1c99c335cec60dcfa7e30 to your computer and use it in GitHub Desktop.
Null object pattern with Optional
Optional.ofNullable(order.getCurrency()).orElse("USD");
Optional.ofNullable(params.get("abc")).orElseThrow(() -> new IllegalArgumentException(message));
public interface User {
String name();
Integer age();
User EMPTY = new User() {
@Override
public String name() {
return "";
}
@Override
public Integer age() {
return 0;
}
};
}
Optional.ofNullable(user).orElse(User.EMPTY)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment