Last active
September 12, 2016 05:15
-
-
Save MikeChristianson/e999b37f92edc3cc52b05117b4e6f160 to your computer and use it in GitHub Desktop.
Get an Optional first element of a List.
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
/** | |
* Safely gets the first element of a {@link List} | |
* @param list a {@code List} | |
* @param <E> type of elements in list | |
* @return the first element of list or {@link Optional#absent()} if absent or null | |
*/ | |
@Nonnull | |
private <E> Optional<E> first(@Nullable List<E> list) { | |
if (list == null || list.isEmpty()) { | |
return Optional.absent(); | |
} | |
return Optional.fromNullable(list.get(0)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment