Class KiwiArrays2

java.lang.Object
org.kiwiproject.beta.collect.KiwiArrays2

@Beta public final class KiwiArrays2 extends Object
Utilities related to arrays.

These utilities can be considered for inclusion into kiwi's KiwiArrays class.

  • Method Details

    • emptyArray

      public static <T> T[] emptyArray(Class<T> type)
      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 is Void.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

      public static <T> T[] newArray(Class<T> type, int length)
      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 array
      length - the length of the new array
      Returns:
      a new array of the specified type and length
      Throws:
      IllegalArgumentException - if type is null or is Void.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 primitives
      wrapperType - the wrapper type of the primitive type
      Returns:
      an array of the wrapper type, or null 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
      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 primitives
      wrapperType - 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

      public static <T> T[] primitiveToObjectArray(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 primitives
      wrapperType - 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 ArrayUtils is 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.