Created
May 18, 2018 18:43
-
-
Save kortov/da5ab1dd51b1c99c335cec60dcfa7e30 to your computer and use it in GitHub Desktop.
Null object pattern with Optional
This file contains 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
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