Package org.kiwiproject.collect
Class KiwiLists
- java.lang.Object
-
- org.kiwiproject.collect.KiwiLists
-
-
Constructor Summary
Constructors Constructor Description KiwiLists()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> voidcheckNonNullInputList(List<T> items)static <T> List<T>distinct(Collection<T> collection)Returns a list of the collection elements with duplicates stripped out.static <T> List<T>distinctOrNull(Collection<T> collection)Returns a list of the collection elements with duplicates stripped out or `null` if a null value is passed in.static <T> Tfifth(List<T> items)Return the fifth element in the specified list of items.static <T> Tfirst(List<T> items)Return the first element in the specified list of items.static <T> Optional<T>firstIfPresent(List<T> items)Returns anOptionalcontaining the first element in specified list of items, or an empty optional if the list is null or empty.static <T> Tfourth(List<T> items)Return the fourth element in the specified list of items.static <T> booleanhasOneElement(List<T> items)Checks whether the specified list is non-null and has only one item.static <T> booleanisNotNullOrEmpty(List<T> items)Checks whether the specified list is neither null nor empty.static <T> booleanisNullOrEmpty(List<T> items)Checks whether the specified list is null or empty.static <T> Tlast(List<T> items)Returns the last element in the specified list of items.static <T> Optional<T>lastIfPresent(List<T> items)Returns anOptionalcontaining the last element in specified list of items, or an empty optional if the list is null or empty.static <T> List<T>newListStartingAtCircularOffset(List<T> input, long startOffset)Returns a new list with the same elements and the same size as the original, however the initial position in the list is now the element specified by the "startOffset" and the list wraps around through the contents to end with "startOffset" - 1static <T> Tnth(List<T> items, int number)Return the nth element in the specified list of items, starting at one for the first element, two for the second, etc.static <T> Tpenultimate(List<T> items)Returns the penultimate (second to last) element in the specified list.static <T> Tsecond(List<T> items)Return the second element in the specified list of items.static <T> TsecondToLast(List<T> items)Synonym forpenultimate(List).static <T> List<T>sorted(List<T> items)Given a list, sort it according to the natural order, returning a new list.static <T> List<T>sorted(List<T> items, Comparator<T> comparator)Given a list, sort it according to the providedComparatorreturning a new list.static <T> Tthird(List<T> items)Return the third element in the specified list of items.
-
-
-
Method Detail
-
isNullOrEmpty
public static <T> boolean isNullOrEmpty(List<T> items)
Checks whether the specified list is null or empty.- Type Parameters:
T- the type of items in the list- Parameters:
items- the list- Returns:
trueif list is null or empty;falseotherwise
-
isNotNullOrEmpty
public static <T> boolean isNotNullOrEmpty(List<T> items)
Checks whether the specified list is neither null nor empty.- Type Parameters:
T- the type of items in the list- Parameters:
items- the list- Returns:
trueif list is NOT null or empty;falseotherwise
-
hasOneElement
public static <T> boolean hasOneElement(List<T> items)
Checks whether the specified list is non-null and has only one item.- Type Parameters:
T- the type of items in the list- Parameters:
items- the list- Returns:
trueif list is non-null and has exactly one item;falseotherwise
-
sorted
public static <T> List<T> sorted(List<T> items)
Given a list, sort it according to the natural order, returning a new list.- Type Parameters:
T- the type of items in the list- Parameters:
items- the list- Returns:
- a new sorted list
-
sorted
public static <T> List<T> sorted(List<T> items, Comparator<T> comparator)
Given a list, sort it according to the providedComparatorreturning a new list.- Type Parameters:
T- the type of items in the list- Parameters:
items- the listcomparator- a Comparator to be used to compare stream elements- Returns:
- a new sorted list
-
first
public static <T> T first(List<T> items)
Return the first element in the specified list of items.- Type Parameters:
T- the type of items in the list- Parameters:
items- the list- Returns:
- the first item in items
- Throws:
IllegalArgumentException- if the list does not contain at least one itemNullPointerException- if the list is null
-
firstIfPresent
public static <T> Optional<T> firstIfPresent(List<T> items)
Returns anOptionalcontaining the first element in specified list of items, or an empty optional if the list is null or empty.- Type Parameters:
T- the type of items in the list- Parameters:
items- the list- Returns:
- Optional containing first element if exists, otherwise Optional.empty()
-
second
public static <T> T second(List<T> items)
Return the second element in the specified list of items.- Type Parameters:
T- the type of items in the list- Parameters:
items- the list- Returns:
- the second item in items
- Throws:
IllegalArgumentException- if the list does not contain at least two itemsNullPointerException- if the list is null
-
third
public static <T> T third(List<T> items)
Return the third element in the specified list of items.- Type Parameters:
T- the type of items in the list- Parameters:
items- the list- Returns:
- the third item in items
- Throws:
IllegalArgumentException- if the list does not contain at least three itemsNullPointerException- if the list is null
-
fourth
public static <T> T fourth(List<T> items)
Return the fourth element in the specified list of items.- Type Parameters:
T- the type of items in the list- Parameters:
items- the list- Returns:
- the fourth item in items
- Throws:
IllegalArgumentException- if the list does not contain at least four itemsNullPointerException- if the list is null
-
fifth
public static <T> T fifth(List<T> items)
Return the fifth element in the specified list of items.- Type Parameters:
T- the type of items in the list- Parameters:
items- the list- Returns:
- the fifth item in items
- Throws:
IllegalArgumentException- if the list does not contain at least five itemsNullPointerException- if the list is null
-
penultimate
public static <T> T penultimate(List<T> items)
Returns the penultimate (second to last) element in the specified list.- Type Parameters:
T- the type of items in the list- Parameters:
items- the list- Returns:
- the penultimate item in items
- Throws:
IllegalArgumentException- if the list does not contain at least two itemsNullPointerException- if the list is null- See Also:
secondToLast(List)
-
secondToLast
public static <T> T secondToLast(List<T> items)
Synonym forpenultimate(List).- Type Parameters:
T- the type of items in the list- Parameters:
items- the list- Returns:
- the penultimate item in items
- Throws:
IllegalArgumentException- if the list does not contain at least two itemsNullPointerException- if the list is null- See Also:
penultimate(List)
-
last
public static <T> T last(List<T> items)
Returns the last element in the specified list of items.- Type Parameters:
T- the type of items in the list- Parameters:
items- the list- Returns:
- the last item in the list
- Throws:
IllegalArgumentException- if the list does not contain at least one itemNullPointerException- if the list is null
-
lastIfPresent
public static <T> Optional<T> lastIfPresent(List<T> items)
Returns anOptionalcontaining the last element in specified list of items, or an empty optional if the list is null or empty.- Type Parameters:
T- the type of items in the list- Parameters:
items- the list- Returns:
- Optional containing last element if exists, otherwise Optional.empty()
-
nth
public static <T> T nth(List<T> items, int number)
Return the nth element in the specified list of items, starting at one for the first element, two for the second, etc.- Type Parameters:
T- the type of items in the list- Parameters:
items- the listnumber- the number of the element to retrieve, starting at one (not zero)- Returns:
- the nth item in items
- Throws:
IllegalArgumentException- if the list does not contain at least number itemsNullPointerException- if the list is null
-
distinct
public static <T> List<T> distinct(Collection<T> collection)
Returns a list of the collection elements with duplicates stripped out.- Type Parameters:
T- the type of items in the collection- Parameters:
collection- the collection of values- Returns:
- a new list with only unique elements
- Throws:
IllegalArgumentException- if the collection is null
-
distinctOrNull
public static <T> List<T> distinctOrNull(Collection<T> collection)
Returns a list of the collection elements with duplicates stripped out or `null` if a null value is passed in.- Type Parameters:
T- the type of items in the collection- Parameters:
collection- the collection of values- Returns:
- a new list with only unique elements or null.
-
checkNonNullInputList
public static <T> void checkNonNullInputList(List<T> items)
-
newListStartingAtCircularOffset
public static <T> List<T> newListStartingAtCircularOffset(List<T> input, long startOffset)
Returns a new list with the same elements and the same size as the original, however the initial position in the list is now the element specified by the "startOffset" and the list wraps around through the contents to end with "startOffset" - 1- Type Parameters:
T- the type of the items in the list- Parameters:
input- the original liststartOffset- the desired offset to start the new list- Returns:
- a new list starting at the desired offset
-
-