Class CollectionUtils

java.lang.Object
org.litebridgedb.commons.CollectionUtils

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

    • isEmpty

      public static boolean isEmpty(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.

      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.

      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