Class ArrayHelper

java.lang.Object
org.javimmutable.collections.common.ArrayHelper

public final class ArrayHelper extends Object
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> T[]
    allocate(int size)
     
    static <T> ArrayHelper.Allocator<T>
    allocator(Class<T> klass)
    Creates an Allocator for arrays of the given class.
    static <T> T[]
    append(ArrayHelper.Allocator<T> allocator, T[] orig, T value)
    Creates a copy of orig with one extra value added at the end.
    static <T> T[]
    assign(T[] orig, int index, T value)
    Creates a copy of orig with the value at index replaced by value.
    static <T> T[]
    assignAppend(ArrayHelper.Allocator<T> allocator, T[] orig, T assignValue, T appendValue)
    Replace the last value in orig (which cannot be empty) with assignValue and also append appendValue to the end.
    static <T> T[]
    assignDelete(ArrayHelper.Allocator<T> allocator, T[] orig, int index, T newNode)
    Deletes the node at index and sets the value of the resulting array at index to newNode.
    static <T> T[]
    assignInsert(ArrayHelper.Allocator<T> allocator, T[] orig, int index, T assignValue, T insertValue)
    Replace the value at index with assignValue and insert insertValue immediately at index + 1.
    static <T> T[]
    assignTwo(T[] orig, int index, T first, T second)
    Replaces the node at index with first and the node at index + 1 with second.
    static <T> void
    checkBounds(T[] values, int index)
     
    static <T> T[]
    concat(ArrayHelper.Allocator<T> allocator, T[] a, T[] b)
    Creates a new array containing all the values of a followed by all the values of b.
    static <T> T[]
    delete(ArrayHelper.Allocator<T> allocator, T[] orig, int index)
    Creates a copy of orig with one value deleted at index.
    static <T> T[]
    insert(ArrayHelper.Allocator<T> allocator, T[] orig, int index, T value)
    Creates a copy of orig with one extra value inserted at index.
    static <T> T[]
    newArray(T a)
     
    static <T> T[]
    newArray(T a, T b)
     
    static <T> T[]
    prefix(ArrayHelper.Allocator<T> allocator, T[] orig, int limit)
    Returns a copy of orig containing of length limit containing all values from [0,limit).
    static <T> T[]
    prefixInsert(ArrayHelper.Allocator<T> allocator, T[] orig, int limit, int index, T value)
    Returns a copy of orig containing of length limit+1 containing all values from [0,limit) but with value inserted at index and values after that shifted to the right.
    static <T> T[]
    reverse(ArrayHelper.Allocator<T> allocator, T[] orig)
     
    static <T> T[]
    subArray(ArrayHelper.Allocator<T> allocator, T[] orig, int offset, int limit)
    Allocate a new array containing the subarray of orig starting at offset and ending before limit.
    static <T> T[]
    subArray(ArrayHelper.Allocator<T> allocator, T[] a, T[] b, int offset, int limit)
    Allocate an array containing values from a logical array formed by concatenating the two input arrays.
    static <T> T[]
    suffix(ArrayHelper.Allocator<T> allocator, T[] orig, int offset)
    Returns a copy of orig containing of length orig.length-offset containing all values from [offset,orig.length).
    static <T> T[]
    suffixInsert(ArrayHelper.Allocator<T> allocator, T[] orig, int offset, int index, T value)
    Returns a copy of orig containing of length orig.length-offset containing all values from [offset,orig.length) but with value inserted at index relative to orig (index-offset relative to resulting array) and all values past that point shifted to the right.

    Methods inherited from class java.lang.Object

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

    • ArrayHelper

      public ArrayHelper()
  • Method Details

    • subArray

      @Nonnull public static <T> T[] subArray(@Nonnull ArrayHelper.Allocator<T> allocator, @Nonnull T[] orig, int offset, int limit)
      Allocate a new array containing the subarray of orig starting at offset and ending before limit. To copy the first value offset would be 0, limit would be 1.
      Parameters:
      allocator - used to allocate the resulting array
      orig - array from which to copy
      offset - 0 based offset into array
      limit - 0 based offset of first value NOT copied (length of copy is limit - offset)
    • subArray

      @Nonnull public static <T> T[] subArray(@Nonnull ArrayHelper.Allocator<T> allocator, @Nonnull T[] a, @Nonnull T[] b, int offset, int limit)
      Allocate an array containing values from a logical array formed by concatenating the two input arrays. Functionally equivalent to calling append(a,b) and then calling subArray() with the result
      Parameters:
      allocator - used to allocate the resulting array
      a - first input array
      b - second input array
      offset - 0 based offset into logical concatenated array
      limit - 0 based offset of first value NOT copied (length of copy is limit - offset)
    • assign

      @Nonnull public static <T> T[] assign(@Nonnull T[] orig, int index, T value)
      Creates a copy of orig with the value at index replaced by value.
      Parameters:
      orig - array to copy
      index - index of value to change
      value - value to assign at index
    • append

      @Nonnull public static <T> T[] append(@Nonnull ArrayHelper.Allocator<T> allocator, @Nonnull T[] orig, T value)
      Creates a copy of orig with one extra value added at the end. Length of result is orig.length + 1.
      Parameters:
      allocator - used to allocate the resulting array
      orig - array to copy
      value - value to assign at end of new array
    • insert

      @Nonnull public static <T> T[] insert(@Nonnull ArrayHelper.Allocator<T> allocator, @Nonnull T[] orig, int index, T value)
      Creates a copy of orig with one extra value inserted at index. All values from index->length are shifted to the right by 1.
      Parameters:
      allocator - used to allocate the resulting array
      orig - array to copy
      index - index where new value should be inserted
      value - value to assign at end of new array
    • delete

      @Nonnull public static <T> T[] delete(@Nonnull ArrayHelper.Allocator<T> allocator, @Nonnull T[] orig, int index)
      Creates a copy of orig with one value deleted at index. All values from index->length are shifted to the left by 1.
      Parameters:
      allocator - used to allocate the resulting array
      orig - array to copy
      index - index where new value should be inserted
    • concat

      @Nonnull public static <T> T[] concat(@Nonnull ArrayHelper.Allocator<T> allocator, @Nonnull T[] a, @Nonnull T[] b)
      Creates a new array containing all the values of a followed by all the values of b.
      Parameters:
      allocator - used to allocate the resulting array
      a - first input array
      b - second input array
    • assignAppend

      @Nonnull public static <T> T[] assignAppend(@Nonnull ArrayHelper.Allocator<T> allocator, @Nonnull T[] orig, T assignValue, T appendValue)
      Replace the last value in orig (which cannot be empty) with assignValue and also append appendValue to the end. Always returns a new array of length orig.length + 1.
    • assignTwo

      @Nonnull public static <T> T[] assignTwo(@Nonnull T[] orig, int index, T first, T second)
      Replaces the node at index with first and the node at index + 1 with second. Always returns a new array. Array must have size >= index + 2.
    • assignInsert

      @Nonnull public static <T> T[] assignInsert(@Nonnull ArrayHelper.Allocator<T> allocator, @Nonnull T[] orig, int index, T assignValue, T insertValue)
      Replace the value at index with assignValue and insert insertValue immediately at index + 1. Always returns a new array. Array size must be at least 1.
    • assignDelete

      @Nonnull public static <T> T[] assignDelete(@Nonnull ArrayHelper.Allocator<T> allocator, @Nonnull T[] orig, int index, T newNode)
      Deletes the node at index and sets the value of the resulting array at index to newNode. Always returns a new array.
    • prefix

      @Nonnull public static <T> T[] prefix(@Nonnull ArrayHelper.Allocator<T> allocator, @Nonnull T[] orig, int limit)
      Returns a copy of orig containing of length limit containing all values from [0,limit).
    • prefixInsert

      @Nonnull public static <T> T[] prefixInsert(@Nonnull ArrayHelper.Allocator<T> allocator, @Nonnull T[] orig, int limit, int index, T value)
      Returns a copy of orig containing of length limit+1 containing all values from [0,limit) but with value inserted at index and values after that shifted to the right.
    • suffix

      @Nonnull public static <T> T[] suffix(@Nonnull ArrayHelper.Allocator<T> allocator, @Nonnull T[] orig, int offset)
      Returns a copy of orig containing of length orig.length-offset containing all values from [offset,orig.length).
    • suffixInsert

      @Nonnull public static <T> T[] suffixInsert(@Nonnull ArrayHelper.Allocator<T> allocator, @Nonnull T[] orig, int offset, int index, T value)
      Returns a copy of orig containing of length orig.length-offset containing all values from [offset,orig.length) but with value inserted at index relative to orig (index-offset relative to resulting array) and all values past that point shifted to the right.
    • reverse

      @Nonnull public static <T> T[] reverse(@Nonnull ArrayHelper.Allocator<T> allocator, @Nonnull T[] orig)
    • allocator

      @Nonnull public static <T> ArrayHelper.Allocator<T> allocator(Class<T> klass)
      Creates an Allocator for arrays of the given class.
    • checkBounds

      public static <T> void checkBounds(T[] values, int index)
    • allocate

      public static <T> T[] allocate(int size)
    • newArray

      public static <T> T[] newArray(T a)
    • newArray

      public static <T> T[] newArray(T a, T b)