Class ArrayUtils

java.lang.Object
me.croabeast.common.util.ArrayUtils

public class ArrayUtils extends Object
Utility methods for working with Java arrays and converting them to collections.

Provides operations such as combining multiple arrays, checking for emptiness, validating array contents, converting arrays to lists or sets, and applying transformations to array elements.

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    <T, U> U[]
    applyToArray(T[] array, Function<T,U> function)
    Applies a mapping function to each element of the source array and returns a new array of the results.
    <T> T[]
    applyToArray(T[] array, UnaryOperator<T>... operators)
    Applies a sequence of UnaryOperator transformations to each element of the array in place.
    <T> T[]
    checkArray(T... array)
    Validates that the array is not empty and returns it.
    <T> T[]
    combineArrays(T[] array, T[]... extraArrays)
    Combines a base array with one or more additional arrays into a single array.
    <T> boolean
    contains(T[] array, T object)
    Checks whether the given array contains the specified element.
    <T> boolean
    isArrayEmpty(T... array)
    Checks whether the given array is null or contains no elements.
    <T> boolean
    Determines if an Iterable is null or contains no elements.
    <T, I extends Collection<T>>
    I
    toCollection(I collection, UnaryOperator<T> function, T... array)
    Converts the given varargs array into the provided collection, optionally applying a transformation function to each element.
    <T, I extends Collection<T>>
    I
    toCollection(I collection, T... array)
    Converts the given varargs array into the provided collection without transformations.
    <T> @NotNull List<T>
    toList(UnaryOperator<T> operator, T... array)
    Converts the given array into a List, applying an optional transformation to each element.
    <T> @NotNull List<T>
    toList(T... array)
    Converts the given array into a List without transformations.
    <T> @NotNull Set<T>
    toSet(T... array)
    Converts the given array into a Set.

    Methods inherited from class java.lang.Object

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

    • ArrayUtils

      public ArrayUtils()
  • Method Details

    • isArrayEmpty

      @SafeVarargs public <T> boolean isArrayEmpty(T... array)
      Checks whether the given array is null or contains no elements.
      Type Parameters:
      T - the component type of the array
      Parameters:
      array - the array to check
      Returns:
      true if array is null or has length zero; false otherwise
    • combineArrays

      @SafeVarargs public <T> T[] combineArrays(@NotNull T[] array, T[]... extraArrays)
      Combines a base array with one or more additional arrays into a single array.

      Null or empty extra arrays are ignored. The resulting array is of the same component type as the base array.

      Type Parameters:
      T - the component type of the arrays
      Parameters:
      array - the base array (must not be null)
      extraArrays - zero or more additional arrays to append
      Returns:
      a new array containing all elements of array followed by all elements of each extraArrays
    • checkArray

      @SafeVarargs public <T> T[] checkArray(T... array)
      Validates that the array is not empty and returns it.

      Throws an exception if the array is null or empty.

      Type Parameters:
      T - the component type of the array
      Parameters:
      array - the array to validate
      Returns:
      the same array if it is not empty
      Throws:
      IllegalArgumentException - if the array is null or empty
    • toCollection

      @SafeVarargs public <T, I extends Collection<T>> I toCollection(I collection, UnaryOperator<T> function, T... array)
      Converts the given varargs array into the provided collection, optionally applying a transformation function to each element.
      Type Parameters:
      T - the element type
      I - the type of the target collection
      Parameters:
      collection - the collection to populate (must not be null)
      function - an optional transformation to apply to each element (may be null)
      array - the source array of elements
      Returns:
      the populated collection
    • toCollection

      @SafeVarargs public <T, I extends Collection<T>> I toCollection(I collection, T... array)
      Converts the given varargs array into the provided collection without transformations.
      Type Parameters:
      T - the element type
      I - the type of the target collection
      Parameters:
      collection - the collection to populate (must not be null)
      array - the source array of elements
      Returns:
      the populated collection
    • toList

      @SafeVarargs @NotNull public <T> @NotNull List<T> toList(UnaryOperator<T> operator, T... array)
      Converts the given array into a List, applying an optional transformation to each element.
      Type Parameters:
      T - the element type
      Parameters:
      operator - the transformation function to apply to each element (may be null)
      array - the source array
      Returns:
      a new List containing the (possibly transformed) elements
    • toList

      @SafeVarargs @NotNull public <T> @NotNull List<T> toList(T... array)
      Converts the given array into a List without transformations.
      Type Parameters:
      T - the element type
      Parameters:
      array - the source array
      Returns:
      a new List containing the elements
    • toSet

      @SafeVarargs @NotNull public <T> @NotNull Set<T> toSet(T... array)
      Converts the given array into a Set.
      Type Parameters:
      T - the element type
      Parameters:
      array - the source array
      Returns:
      a new Set containing the elements
    • applyToArray

      @SafeVarargs @NotNull public <T> T[] applyToArray(T[] array, UnaryOperator<T>... operators)
      Applies a sequence of UnaryOperator transformations to each element of the array in place.
      Type Parameters:
      T - the element type
      Parameters:
      array - the array whose elements will be transformed (must not be null)
      operators - the operators to apply in order to each element
      Returns:
      the same array instance with transformed elements
    • applyToArray

      @NotNull public <T, U> U[] applyToArray(T[] array, Function<T,U> function)
      Applies a mapping function to each element of the source array and returns a new array of the results.
      Type Parameters:
      T - the source element type
      U - the target element type
      Parameters:
      array - the source array (must not be null)
      function - the mapping function to apply to each element (must not be null)
      Returns:
      a new array containing the mapped elements
    • contains

      public <T> boolean contains(T[] array, T object)
      Checks whether the given array contains the specified element.
      Type Parameters:
      T - the element type
      Parameters:
      array - the array to search
      object - the element to look for
      Returns:
      true if object is found in array; false otherwise
    • isIterableEmpty

      public <T> boolean isIterableEmpty(Iterable<T> iterable)
      Determines if an Iterable is null or contains no elements.
      Type Parameters:
      T - the element type
      Parameters:
      iterable - the iterable to check
      Returns:
      true if iterable is null or has no elements; false otherwise