Class ArrayUtils

java.lang.Object
codechicken.lib.util.ArrayUtils

public class ArrayUtils extends Object
Created by covers1624 on 3/27/2016.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> List<T>
    addAllNoNull(T[] array, List<T> list)
    Adds all elements from the array that are not null to the list.
    static <T> T[]
    addToArrayFirstNull(T[] array, T value)
    Adds the value at the first null index in the array.
    static <I, O> List<O>
    applyArray(Function<I,O> function, I... array)
    Apples the Specified function to the entire array and returns a new List of the result.
    static void
    arrayCopy(Object src, int srcPos, Object dst, int destPos, int length)
    Basically a wrapper for System.arraycopy with support for CCL Copyable's
    static String[]
    Converts an String array to lowercase.
    static <T> boolean
    contains(T[] input, T element)
    Checks if an array contains any of the specified element.
    static <T> boolean
    containsKeys(Map<T,?> map, T... keys)
    Checks if a map contains all keys passed in.
    static Map<String,String>
    Converts and array of "key=value" to a map.
    static <T> int
    count(T[] array, Function<T,Boolean> check)
    Counts elements in the array that conform to the Function check.
    static <T> int
    countNoNull(T[] array)
    Counts the elements in the array that are not null.
    static <T> T[]
    createNewArray(T[] array)
    Create a new array using the provided array as a template for both type and length.
    static <T> T[]
    createNewArray(T[] array, int length)
    Create a new array using the provided array as a template for the type and with the provided length.
    static <T> T[]
    fill(T[] array, T value)
    Fills the array with the specified value.
    static <T> void
    fillArray(T[] array, T value, Function<T,Boolean> check)
    Fills the array with the specified value.
    static <T> int
    indexOf(T[] array, T object)
    Returns the index of the first occurrence of the specified element in the array.
    static <T> int
    indexOfRef(T[] array, T object)
    Returns the index of the first occurrence of the specified element in the array with the same identity.
    static <T> T[]
    inverse(T[] input, T[] allElements)
    Creates the inverse of an array.
    static <T> boolean
    isEmpty(T[] array)
    Checks if the array is all null.
    static <T> boolean
    isNullOrContainsNull(T @Nullable [] input)
    Checks if the specified array is empty or contains a null entry.
    static <T> T[]
    newArray(Class<T> arrayClass, int length)
    Creates a new array form its component class.
    static List<String>
    Prefixes a string array with the desired value.
    static <T> T[]
    rollArray(T[] input, int shift)
    Rolls the array based on the shift.
    static List<Integer>
    toList(int[] arr)
    Convert an int array to a list of Integers.

    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

    • arrayToLowercase

      public static String[] arrayToLowercase(String[] array)
      Converts an String array to lowercase.
      Parameters:
      array - Array to convert.
      Returns:
      Converted array.
    • convertKeyValueArrayToMap

      public static Map<String,String> convertKeyValueArrayToMap(String[] array)
      Converts and array of "key=value" to a map.
      Parameters:
      array - Array to convert.
      Returns:
      Map of values.
    • prefixStringList

      public static List<String> prefixStringList(String prefix, List<String> list)
      Prefixes a string array with the desired value.
      Parameters:
      prefix - The prefix to apply.
      list - The list to apply the prefix to.
      Returns:
      The list with the prefix applied.
    • containsKeys

      @SafeVarargs public static <T> boolean containsKeys(Map<T,?> map, T... keys)
      Checks if a map contains all keys passed in.
      Type Parameters:
      T - The type of data in the map key.
      Parameters:
      map - Map to check.
      keys - Keys that must exist.
      Returns:
      False if fail.
    • addToArrayFirstNull

      public static <T> T[] addToArrayFirstNull(T[] array, T value)
      Adds the value at the first null index in the array.
      Type Parameters:
      T - Type of value.
      Parameters:
      array - Array to add to.
      value - Value to add.
      Returns:
      Returns a new array in the event the input was expanded.
    • addAllNoNull

      public static <T> List<T> addAllNoNull(T[] array, List<T> list)
      Adds all elements from the array that are not null to the list.
      Type Parameters:
      T - What we are dealing with.
      Parameters:
      array - Array to grab from.
      list - List to add to.
      Returns:
      The modified list.
    • isEmpty

      public static <T> boolean isEmpty(T[] array)
      Checks if the array is all null.
      Type Parameters:
      T - What we are dealing with.
      Parameters:
      array - The array to check.
      Returns:
      True if the array only contains nulls.
    • countNoNull

      public static <T> int countNoNull(T[] array)
      Counts the elements in the array that are not null.
      Type Parameters:
      T - What we are dealing with.
      Parameters:
      array - The array to check.
      Returns:
      The count of non-null objects in the array.
    • count

      public static <T> int count(T[] array, Function<T,Boolean> check)
      Counts elements in the array that conform to the Function check.
      Type Parameters:
      T - What we are dealing with.
      Parameters:
      array - The array to check.
      check - The Function to apply to each element.
      Returns:
      The count.
    • fill

      public static <T> T[] fill(T[] array, T value)
      Fills the array with the specified value. If the value is an instance of Copyable it will call copy.
      Type Parameters:
      T - What we are dealing with.
      Parameters:
      array - Array to fill.
      value - Value to fill with.
    • fillArray

      public static <T> void fillArray(T[] array, T value, Function<T,Boolean> check)
      Fills the array with the specified value. A Function is used to check if the value should be replaced. If the value is an instance of Copyable it will call copy.
      Type Parameters:
      T - What we are dealing with.
      Parameters:
      array - Array to fill.
      value - Value to fill with.
      check - Called to decide if the value should be replaced.
    • applyArray

      public static <I, O> List<O> applyArray(Function<I,O> function, I... array)
      Apples the Specified function to the entire array and returns a new List of the result. The input to the function may be null.
      Type Parameters:
      I - The Input generic.
      O - The Output generic.
      Parameters:
      function - The function to apply.
      array - The array to apply the function to.
      Returns:
      A list of the output of the specified function.
    • arrayCopy

      public static void arrayCopy(Object src, int srcPos, Object dst, int destPos, int length)
      Basically a wrapper for System.arraycopy with support for CCL Copyable's
      Parameters:
      src - The source array.
      srcPos - Starting position in the source array.
      dst - The destination array.
      destPos - Starting position in the destination array.
      length - The number of elements to copy.
    • indexOf

      public static <T> int indexOf(T[] array, @Nullable T object)
      Returns the index of the first occurrence of the specified element in the array. Will return -1 if the element is non existent in the array.
      Type Parameters:
      T - What we are dealing with.
      Parameters:
      array - The array to search.
      object - Element to find.
      Returns:
      The index in the array of the object.
    • indexOfRef

      public static <T> int indexOfRef(T[] array, T object)
      Returns the index of the first occurrence of the specified element in the array with the same identity. (Ref compare). Will return -1 if the element is non existent in the array.
      Parameters:
      array - The array to search.
      object - Element to find.
      Returns:
      The index in the array of the object.
    • createNewArray

      public static <T> T[] createNewArray(T[] array)
      Create a new array using the provided array as a template for both type and length.
      Type Parameters:
      T - The type.
      Parameters:
      array - The template.
      Returns:
      The new array.
    • createNewArray

      public static <T> T[] createNewArray(T[] array, int length)
      Create a new array using the provided array as a template for the type and with the provided length.
      Type Parameters:
      T - The type.
      Parameters:
      array - The type template.
      length - The new array's length.
      Returns:
      The new array.
    • newArray

      public static <T> T[] newArray(Class<T> arrayClass, int length)
      Creates a new array form its component class.
      Type Parameters:
      T - The thing.
      Parameters:
      arrayClass - The component class.
      length - The length.
      Returns:
      The new array.
    • rollArray

      public static <T> T[] rollArray(T[] input, int shift)
      Rolls the array based on the shift. Positive shift means the array will roll to the right. Negative shift means the array will roll to the left.
      Type Parameters:
      T - The thing.
      Parameters:
      input - The input array.
      shift - The shift amount.
      Returns:
      The new array.
    • contains

      public static <T> boolean contains(T[] input, @Nullable T element)
      Checks if an array contains any of the specified element.
      Type Parameters:
      T - The thing.
      Parameters:
      input - The input
      element - The thing to test against.
      Returns:
      If the element exists at all.
    • inverse

      public static <T> T[] inverse(T[] input, T[] allElements)
      Creates the inverse of an array. If the input array does not contain an element from the allElements array, then it is added to the output.
      Type Parameters:
      T - The thing.
      Parameters:
      input - The input.
      allElements - All possible values.
      Returns:
      The inverse array.
    • isNullOrContainsNull

      public static <T> boolean isNullOrContainsNull(T @Nullable [] input)
      Checks if the specified array is empty or contains a null entry.
      Type Parameters:
      T - The thing.
      Parameters:
      input - The input.
      Returns:
      If the array is null or contains null.
    • toList

      public static List<Integer> toList(int[] arr)
      Convert an int array to a list of Integers.
      Parameters:
      arr - in.
      Returns:
      out.