Package me.croabeast.common.util
Class ArrayUtils
java.lang.Object
me.croabeast.common.util.ArrayUtils
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 -
Method Summary
Modifier and TypeMethodDescription<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 ofUnaryOperatortransformations 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> booleancontains(T[] array, T object) Checks whether the given array contains the specified element.<T> booleanisArrayEmpty(T... array) Checks whether the given array is null or contains no elements.<T> booleanisIterableEmpty(Iterable<T> iterable) Determines if anIterableis null or contains no elements.<T,I extends Collection<T>>
ItoCollection(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>>
ItoCollection(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 aList, applying an optional transformation to each element.<T> @NotNull List<T> toList(T... array) Converts the given array into aListwithout transformations.<T> @NotNull Set<T> toSet(T... array) Converts the given array into aSet.
-
Constructor Details
-
ArrayUtils
public ArrayUtils()
-
-
Method Details
-
isArrayEmpty
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:
trueifarrayis null or has length zero;falseotherwise
-
combineArrays
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
arrayfollowed by all elements of eachextraArrays
-
checkArray
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 typeI- 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
Converts the given varargs array into the provided collection without transformations.- Type Parameters:
T- the element typeI- 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
Converts the given array into aList, 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
Listcontaining the (possibly transformed) elements
-
toList
Converts the given array into aListwithout transformations.- Type Parameters:
T- the element type- Parameters:
array- the source array- Returns:
- a new
Listcontaining the elements
-
toSet
Converts the given array into aSet.- Type Parameters:
T- the element type- Parameters:
array- the source array- Returns:
- a new
Setcontaining the elements
-
applyToArray
Applies a sequence ofUnaryOperatortransformations 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
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 typeU- 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 searchobject- the element to look for- Returns:
trueifobjectis found inarray;falseotherwise
-
isIterableEmpty
Determines if anIterableis null or contains no elements.- Type Parameters:
T- the element type- Parameters:
iterable- the iterable to check- Returns:
trueifiterableis null or has no elements;falseotherwise
-