Package org.kiwiproject.collect
Class KiwiCollections
java.lang.Object
org.kiwiproject.collect.KiwiCollections
Utility methods for working with
Collection instances.-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> voidcheckNonNullCollection(Collection<T> collection) Checks that the given collection is not null.static <T> voidcheckNotEmptyCollection(Collection<T> collection) Checks that the given collection is not empty.static <T> Tfirst(Collection<T> sequencedCollection) Return the first element in the given sequenced collection.static <T> Optional<T>firstIfPresent(Collection<T> sequencedCollection) Returns anOptionalcontaining the first element in the given sequenced collection, or an empty optional if the collection is null or empty.static <T> booleanhasOneElement(Collection<T> collection) Checks whether the specified collection is non-null and has only one item.static <T> booleanisNotNullOrEmpty(Collection<T> collection) Checks whether the specified collection is neither null nor empty.static <T> booleanisNullOrEmpty(Collection<T> collection) Checks whether the specified collection is null or empty.static <T> booleanisSequenced(Collection<T> collection) Checks whether the given collection is "sequenced".static <T> Tlast(Collection<T> sequencedCollection) Return the last element in the given sequenced collection.static <T> Optional<T>lastIfPresent(Collection<T> sequencedCollection) Returns anOptionalcontaining the last element in the given sequenced collection, or an empty optional if the collection is null or empty.
-
Method Details
-
isNullOrEmpty
Checks whether the specified collection is null or empty.- Type Parameters:
T- the type of items in the collection- Parameters:
collection- the collection- Returns:
trueif collection is null or empty;falseotherwise
-
isNotNullOrEmpty
Checks whether the specified collection is neither null nor empty.- Type Parameters:
T- the type of items in the collection- Parameters:
collection- the collection- Returns:
trueif collection is neither null nor empty;falseotherwise
-
hasOneElement
Checks whether the specified collection is non-null and has only one item.- Type Parameters:
T- the type of items in the collection- Parameters:
collection- the collection- Returns:
trueif collection is non-null and has exactly one item;false
-
firstIfPresent
Returns anOptionalcontaining the first element in the given sequenced collection, or an empty optional if the collection is null or empty.- Type Parameters:
T- the type of elements in the collection- Parameters:
sequencedCollection- the sequenced collection- Returns:
Optionalcontaining first element if exists, otherwise Optional.empty()- Throws:
IllegalArgumentException- if sequencedCollection is not a sequenced collection
-
first
Return the first element in the given sequenced collection.- Type Parameters:
T- the type of elements in the collection- Parameters:
sequencedCollection- the sequenced collection- Returns:
- the first element of the collection
- Throws:
IllegalArgumentException- if sequencedCollection is null, empty, or not a sequenced collection- See Also:
-
lastIfPresent
Returns anOptionalcontaining the last element in the given sequenced collection, or an empty optional if the collection is null or empty.- Type Parameters:
T- the type of elements in the collection- Parameters:
sequencedCollection- the sequenced collection- Returns:
Optionalcontaining last element if exists, otherwise Optional.empty()- Throws:
IllegalArgumentException- if sequencedCollection is not a sequenced collection
-
last
Return the last element in the given sequenced collection.- Type Parameters:
T- the type of elements in the collection- Parameters:
sequencedCollection- the sequenced collection- Returns:
- the last element of the collection
- Throws:
IllegalArgumentException- if sequencedCollection is null, empty, or not a sequenced collection- See Also:
- Implementation Note:
- If
sequencedCollectionis aLinkedHashSet, there is no direct way to obtain the last element. This implementation creates aStreamover the elements, skipping until the last element.
-
checkNotEmptyCollection
Checks that the given collection is not empty.- Type Parameters:
T- the type of elements in the collection- Parameters:
collection- the collection- Throws:
IllegalArgumentException- if the given collection is null or empty
-
checkNonNullCollection
Checks that the given collection is not null.- Type Parameters:
T- the type of elements in the collection- Parameters:
collection- the collection- Throws:
NullPointerException- if the collection is null
-
isSequenced
Checks whether the given collection is "sequenced".The definition of "sequenced" is based on JEP 431: Sequenced Collections (as it existed on 2022-11-09).
The current
Collectiontypes (and their subtypes and implementations) that are considered sequenced includeSortedSet,LinkedHashSet,List, andDeque.- Type Parameters:
T- the type of elements in the collection- Parameters:
collection- the collection- Returns:
- true if the given collection is "sequenced"
-