Skip to content

Commit

Permalink
feat(collection): add last() and lastMinus() support for the List<T>
Browse files Browse the repository at this point in the history
work on #24
  • Loading branch information
bsorrentino committed Sep 11, 2024
1 parent f193da8 commit 52bfbec
Showing 1 changed file with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,29 @@
*/
public final class CollectionsUtils {

/**
* Returns the last value in the list, if present.
*
* @return an Optional containing the last value if present, otherwise an empty Optional
*/
public static <T> Optional<T> last( List<T> values ) {
return (values == null || values.isEmpty()) ?
Optional.empty() :
Optional.of(values.get(values.size() - 1));
}

/**
* Returns the value at the specified position from the end of the list, if present.
*
* @param n the position from the end of the list
* @return an Optional containing the value at the specified position if present, otherwise an empty Optional
*/
public static <T> Optional<T> lastMinus(List<T> values, int n) {
return (values == null || values.isEmpty()) ?
Optional.empty() :
Optional.of(values.get(values.size() - 1));
}

public static <T> List<T> listOf(Class<T> clazz) {
return Collections.emptyList();
}
Expand Down

0 comments on commit 52bfbec

Please sign in to comment.