Skip to content

Instantly share code, notes, and snippets.

@pulse00
Created November 8, 2016 15:36
Show Gist options
  • Save pulse00/09332dd5f8df5878243b56465f3f330a to your computer and use it in GitHub Desktop.
Save pulse00/09332dd5f8df5878243b56465f3f330a to your computer and use it in GitHub Desktop.
// java < 8
if (document != null && document.getOrder() != null && document.getOrder().getAddress() != null && document.getOrder().getAddress().getStreet() != null) {
// boom
)
// java >= 8
Optional<Street> street = resolve(() -> document.getOrder().getAddress().getStreet());
if (street.isPresent() {
street.get();
} else {
// boom
}
public static <T> Optional<T> resolve(Supplier<T> resolver) {
try {
T result = resolver.get();
return Optional.ofNullable(result);
}
catch (NullPointerException e) {
return Optional.empty();
}
}
@pulse00
Copy link
Author

pulse00 commented Nov 8, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment