Created
August 12, 2016 08:45
-
-
Save lfo/8369943b2b91f28cc391c543cf38abed to your computer and use it in GitHub Desktop.
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
// Avoid NPE simply taken from : http://winterbe.com/posts/2015/03/15/avoid-null-checks-in-java/ | |
public static <T> Optional<T> resolve(Supplier<T> resolver) { | |
try { | |
T result = resolver.get(); | |
return Optional.ofNullable(result); | |
} | |
catch (NullPointerException e) { | |
return Optional.empty(); | |
} | |
} | |
Usage : | |
resolve(() -> obj.getNested().getInner().getFoo()).ifPresent(System.out::println); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment