Package org.kiwiproject.beta.collect
Class KiwiArrays2
java.lang.Object
org.kiwiproject.beta.collect.KiwiArrays2
Utilities related to arrays.
These utilities can be considered for inclusion into kiwi's KiwiArrays class.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T[]emptyArray(Class<T> type) Creates an empty array of the specified type.static <T> T[]Creates a new array of the specified type and length.static <T> T[]primitiveToObjectArray(Object primitiveArray, Class<T> wrapperType) Convert an array of primitives to an array of the corresponding wrapper type.static <T> Optional<T[]>primitiveToObjectArrayOrEmpty(@Nullable Object primitiveArray, Class<T> wrapperType) Convert an array of primitives to an array of the corresponding wrapper type.static <T> @Nullable T[]primitiveToObjectArrayOrNull(@Nullable Object primitiveArray, Class<T> wrapperType) Convert an array of primitives to an array of the corresponding wrapper type.
-
Method Details
-
emptyArray
Creates an empty array of the specified type.- Type Parameters:
T- the type parameter representing the component type of the array- Parameters:
type- the class object representing the component type of the array- Returns:
- an empty array of the specified type
- Throws:
IllegalArgumentException- if type is null or isVoid.TYPE- See Also:
- Implementation Note:
- This method exists because
Array.newInstance(Class, int)returns Object and thus requires a cast. Using this method, code can be a little cleaner without a cast.
-
newArray
Creates a new array of the specified type and length. All values in the array are null.- Type Parameters:
T- the type parameter representing the component type of the array- Parameters:
type- the class object representing the component type of the arraylength- the length of the new array- Returns:
- a new array of the specified type and length
- Throws:
IllegalArgumentException- if type is null or isVoid.TYPE, or length is negative- See Also:
- Implementation Note:
- This method exists because
Array.newInstance(Class, int)returns Object and thus requires a cast. Using this method, code can be a little cleaner without a cast.
-
primitiveToObjectArrayOrNull
public static <T> @Nullable T[] primitiveToObjectArrayOrNull(@Nullable Object primitiveArray, Class<T> wrapperType) Convert an array of primitives to an array of the corresponding wrapper type.- Type Parameters:
T- the wrapper type corresponding to the input array's primitive type- Parameters:
primitiveArray- an array of primitiveswrapperType- the wrapper type of the primitive type- Returns:
- an array of the wrapper type, or
nullif the input array is null - Throws:
IllegalArgumentException- if the input array is not an array of primitives, or the wrapper type is not a primitive wrapper class- Implementation Note:
- Intentionally suppressing Sonar java:S1168 (Return an empty array instead of null) because the method is explicit that it returns null when given null input.
-
primitiveToObjectArrayOrEmpty
public static <T> Optional<T[]> primitiveToObjectArrayOrEmpty(@Nullable Object primitiveArray, Class<T> wrapperType) Convert an array of primitives to an array of the corresponding wrapper type.- Type Parameters:
T- the wrapper type corresponding to the input array's primitive type- Parameters:
primitiveArray- an array of primitiveswrapperType- the wrapper type of the primitive type- Returns:
- an Optional containing an array of the wrapper type, or an empty Optional if the input array is null
- Throws:
IllegalArgumentException- if the input array is not an array of primitives or the wrapper type is not a primitive wrapper class
-
primitiveToObjectArray
Convert an array of primitives to an array of the corresponding wrapper type.- Type Parameters:
T- the wrapper type corresponding to the input array's primitive type- Parameters:
primitiveArray- an array of primitiveswrapperType- the wrapper type of the primitive type- Returns:
- an array of the wrapper type
- Throws:
IllegalArgumentException- if the input array is null or is not an array of primitives or the wrapper type is not a primitive wrapper class- Implementation Note:
- The internal logic is not pretty, but I cannot find an existing utility that does this (e.g., in
Apache Commons or Google Guava). Apache Commons Lang's
ArrayUtilsis used internally to convert primitive arrays, but because of needing to handle all Java primitive types, I can't think of a cleaner way to do this other than a conditional covering all eight primitive types.
-