Class KiwiCollections

java.lang.Object
org.kiwiproject.collect.KiwiCollections

public final class KiwiCollections extends Object
Utility methods for working with Collection instances.
  • Method Details

    • isNullOrEmpty

      public static <T> boolean isNullOrEmpty(Collection<T> collection)
      Checks whether the specified collection is null or empty.
      Type Parameters:
      T - the type of items in the collection
      Parameters:
      collection - the collection
      Returns:
      true if collection is null or empty; false otherwise
    • isNotNullOrEmpty

      public static <T> boolean isNotNullOrEmpty(Collection<T> collection)
      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:
      true if collection is neither null nor empty; false otherwise
    • hasOneElement

      public static <T> boolean hasOneElement(Collection<T> collection)
      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:
      true if collection is non-null and has exactly one item; false
    • firstIfPresent

      public static <T> Optional<T> firstIfPresent(Collection<T> sequencedCollection)
      Returns an Optional containing 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:
      Optional containing first element if exists, otherwise Optional.empty()
      Throws:
      IllegalArgumentException - if sequencedCollection is not a sequenced collection
    • first

      public static <T> T first(Collection<T> sequencedCollection)
      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

      public static <T> Optional<T> lastIfPresent(Collection<T> sequencedCollection)
      Returns an Optional containing 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:
      Optional containing last element if exists, otherwise Optional.empty()
      Throws:
      IllegalArgumentException - if sequencedCollection is not a sequenced collection
    • last

      public static <T> T last(Collection<T> sequencedCollection)
      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 sequencedCollection is a LinkedHashSet, there is no direct way to obtain the last element. This implementation creates a Stream over the elements, skipping until the last element.
    • checkNotEmptyCollection

      public static <T> void checkNotEmptyCollection(Collection<T> collection)
      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

      public static <T> void checkNonNullCollection(Collection<T> collection)
      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

      public static <T> boolean isSequenced(Collection<T> collection)
      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 Collection types (and their subtypes and implementations) that are considered sequenced include SortedSet, LinkedHashSet, List, and Deque.

      Type Parameters:
      T - the type of elements in the collection
      Parameters:
      collection - the collection
      Returns:
      true if the given collection is "sequenced"