java.lang.Object
net.i2p.router.crypto.ratchet.GrowingArrayUtils

final class GrowingArrayUtilsextends Object
Helper methods for efficient primitive array growth and insertion. All methods assume that an array's length is its capacity, NOT its element count; the current size is always passed in as a parameter.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Utility class, not intended for instantiation.
  • Method Summary

    All Methods
    Modifier and Type
    Method
    Description
    static char[]
    append(char[] array, int currentSize, char element)
    Primitive char version of append(Object[], int, Object).
    static <T> T[]
    append(T[] array, int currentSize, T element)
    Appends an element to the end of the array, growing the array if there is no more room.
    static int
    growSize(int currentSize)
    Given the current size of an array, returns an ideal size to which the array should grow.
    static char[]
    insert(char[] array, int currentSize, int index, char element)
    Primitive char version of insert(Object[], int, int, Object).
    static <T> T[]
    insert(T[] array, int currentSize, int index, T element)
    Inserts an element into the array at the specified index, growing the array if there is no more room.

    Methods inherited from class Object

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

    • GrowingArrayUtils

      private GrowingArrayUtils()
      Utility class, not intended for instantiation.
  • Method Details

    • append

      public static <T> T[] append(T[] array, int currentSize, T element)
      Appends an element to the end of the array, growing the array if there is no more room.
      Type Parameters:
      T - the element type held by the array
      Parameters:
      array - The array to which to append the element. This must NOT be null.
      currentSize - The number of elements in the array. Must be less than or equal to array.length.
      element - The element to append.
      Returns:
      the array to which the element was appended. This may be different than the given array.
    • append

      public static char[] append(char[] array, int currentSize, char element)
      Primitive char version of append(Object[], int, Object).
      Parameters:
      array - The array to which to append the element. This must NOT be null.
      currentSize - Number of elements in the array, at most array.length.
      element - The element to append.
      Returns:
      the array with the element appended, which may be a larger copy
    • insert

      public static <T> T[] insert(T[] array, int currentSize, int index, T element)
      Inserts an element into the array at the specified index, growing the array if there is no more room.
      Type Parameters:
      T - the element type held by the array
      Parameters:
      array - The array to which to append the element. Must NOT be null.
      currentSize - The number of elements in the array. Must be less than or equal to array.length.
      index - Position to insert at, from 0 through currentSize.
      element - The element to insert.
      Returns:
      the array to which the element was appended. This may be different than the given array.
    • insert

      public static char[] insert(char[] array, int currentSize, int index, char element)
      Primitive char version of insert(Object[], int, int, Object).
      Parameters:
      array - The array to insert into. This must NOT be null.
      currentSize - Number of elements in the array, at most array.length.
      index - Position to insert at, from 0 through currentSize.
      element - The element to insert.
      Returns:
      the array with the element inserted, which may be a larger copy
    • growSize

      public static int growSize(int currentSize)
      Given the current size of an array, returns an ideal size to which the array should grow. This is typically double the given size, but should not be relied upon to do so in the future.
      Parameters:
      currentSize - Number of elements currently held in the array.
      Returns:
      the new capacity: 8 when currentSize is 4 or less, otherwise twice currentSize