Class CollectionUtils

java.lang.Object
org.litebridge.commons.CollectionUtils

public final class CollectionUtils extends Object
Utility class for working with collections.
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    isEmpty(@Nullable Iterable<?> iterable)
    Checks if the provided iterable is null or empty.
    static boolean
    isEmpty(@Nullable Object @Nullable [] array)
    Checks if the provided array is null or empty.
    static boolean
    isEmpty(@Nullable Collection<?> collection)
    Checks if the provided collection is null or empty.
    static boolean
    isEmpty(@Nullable Map<?,?> map)
    Checks if the provided map is null or empty.
    static boolean
    isMutable(@Nullable Object collection)
    Checks if the specified collection is mutable.
    static <T extends Collection<U>, U>
    T
    requireNonEmpty(@Nullable T collection, String message)
    Ensures that the provided collection is not null or empty.
    static <T extends Collection<U>, U, X extends Throwable>
    T
    requireNonEmpty(@Nullable T collection, Supplier<? extends X> exceptionSupplier)
    Ensures that the provided collection is not null or empty.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • isEmpty

      public static boolean isEmpty(@Nullable Object @Nullable [] array)
      Checks if the provided array is null or empty.
      Parameters:
      array - the array to check; may be null
      Returns:
      true if the array is null or empty; false otherwise
    • isEmpty

      public static boolean isEmpty(@Nullable Iterable<?> iterable)
      Checks if the provided iterable is null or empty.
      Parameters:
      iterable - the iterable to check; may be null
      Returns:
      true if the iterable is null or empty; false otherwise
    • isEmpty

      public static boolean isEmpty(@Nullable Collection<?> collection)
      Checks if the provided collection is null or empty.
      Parameters:
      collection - the collection to check; may be null
      Returns:
      true if the collection is null or empty; false otherwise
    • isEmpty

      public static boolean isEmpty(@Nullable Map<?,?> map)
      Checks if the provided map is null or empty.
      Parameters:
      map - the map to check; may be null
      Returns:
      true if the map is null or empty; false otherwise
    • requireNonEmpty

      public static <T extends Collection<U>, U> T requireNonEmpty(@Nullable T collection, String message)
      Ensures that the provided collection is not null or empty.

      If the collection is null or empty, an IllegalArgumentException is thrown with the given message.

      Type Parameters:
      T - the type of the collection
      U - the type of the elements in the collection
      Parameters:
      collection - the collection to validate; may be null
      message - the exception message to use if the validation fails
      Returns:
      the validated non-null and non-empty collection
      Throws:
      IllegalArgumentException - if the collection is null or empty
    • requireNonEmpty

      public static <T extends Map<?, ?>> T requireNonEmpty(@Nullable T map, String message)
      Ensures that the provided map is not null or empty.

      If the map is null or empty, an IllegalArgumentException is thrown with the given message.

      Type Parameters:
      T - the type of the map
      Parameters:
      map - the map to validate; may be null
      message - the exception message to use if the validation fails
      Returns:
      the validated non-null and non-empty map
      Throws:
      IllegalArgumentException - if the map is null or empty
    • requireNonEmpty

      public static <T extends Collection<U>, U, X extends Throwable> T requireNonEmpty(@Nullable T collection, Supplier<? extends X> exceptionSupplier) throws X
      Ensures that the provided collection is not null or empty.

      If the collection is null or empty,the supplied exception is thrown.

      Type Parameters:
      T - the type of the collection
      U - the type of the elements in the collection
      X - the type of exception to be thrown
      Parameters:
      collection - the collection to validate; may be null
      exceptionSupplier - a supplier that provides the exception to be thrown if the validation fails
      Returns:
      the validated non-null and non-empty collection
      Throws:
      X - if the collection is null or empty
    • isMutable

      public static boolean isMutable(@Nullable Object collection)
      Checks if the specified collection is mutable.

      This is a limited check and recognises ArrayList, LinkedList, and HashSet.

      Parameters:
      collection - The collection to check.
      Returns:
      true if the collection is mutable, false otherwise (or if the collection is null).