Class ObjectArrays

java.lang.Object
com.google.common.collect.ObjectArrays

public final class ObjectArrays extends Object
Static utility methods pertaining to object arrays.
Since:
2.0
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
     
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) static Object
    checkElementNotNull(Object element, int index)
     
    (package private) static Object[]
     
    (package private) static Object[]
    checkElementsNotNull(Object[] array, int length)
     
    static <T> T[]
    concat(T[] array, T element)
    Returns a new array that appends element to array.
    static <T> T[]
    concat(T[] first, T[] second, Class<T> type)
    Returns a new array that contains the concatenated contents of two arrays.
    static <T> T[]
    concat(T element, T[] array)
    Returns a new array that prepends element to array.
    (package private) static Object[]
    copyAsObjectArray(Object[] elements, int offset, int length)
    Returns a copy of the specified subrange of the specified array that is literally an Object[], and not e.g.
    private static Object[]
    fillArray(Iterable<?> elements, Object[] array)
     
    static <T> T[]
    newArray(Class<T> type, int length)
    Returns a new array of the given length with the specified component type.
    static <T> T[]
    newArray(T[] reference, int length)
    Returns a new array of the given length with the same type as a reference array.
    (package private) static void
    swap(Object[] array, int i, int j)
    Swaps array[i] with array[j].
    (package private) static <T> T[]
    toArrayImpl(Object[] src, int offset, int len, T[] dst)
    Implementation of Collection.toArray(Object[]) for collections backed by an object array.
    (package private) static Object[]
    Returns an array containing all of the elements in the specified collection.
    (package private) static <T> T[]
    toArrayImpl(Collection<?> c, T[] array)
    Returns an array containing all of the elements in the specified collection; the runtime type of the returned array is that of the specified array.

    Methods inherited from class java.lang.Object

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

    • ObjectArrays

      private ObjectArrays()
  • Method Details

    • newArray

      public static <T> T[] newArray(Class<T> type, int length)
      Returns a new array of the given length with the specified component type.
      Parameters:
      type - the component type
      length - the length of the new array
    • newArray

      public static <T> T[] newArray(T[] reference, int length)
      Returns a new array of the given length with the same type as a reference array.
      Parameters:
      reference - any array of the desired type
      length - the length of the new array
    • concat

      public static <T> T[] concat(T[] first, T[] second, Class<T> type)
      Returns a new array that contains the concatenated contents of two arrays.
      Parameters:
      first - the first array of elements to concatenate
      second - the second array of elements to concatenate
      type - the component type of the returned array
    • concat

      public static <T> T[] concat(T element, T[] array)
      Returns a new array that prepends element to array.
      Parameters:
      element - the element to prepend to the front of array
      array - the array of elements to append
      Returns:
      an array whose size is one larger than array, with element occupying the first position, and the elements of array occupying the remaining elements.
    • concat

      public static <T> T[] concat(T[] array, T element)
      Returns a new array that appends element to array.
      Parameters:
      array - the array of elements to prepend
      element - the element to append to the end
      Returns:
      an array whose size is one larger than array, with the same contents as array, plus element occupying the last position.
    • toArrayImpl

      static <T> T[] toArrayImpl(Collection<?> c, T[] array)
      Returns an array containing all of the elements in the specified collection; the runtime type of the returned array is that of the specified array. If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of the specified collection.

      If the collection fits in the specified array with room to spare (i.e., the array has more elements than the collection), the element in the array immediately following the end of the collection is set to null. This is useful in determining the length of the collection only if the caller knows that the collection does not contain any null elements.

      This method returns the elements in the order they are returned by the collection's iterator.

      TODO(kevinb): support concurrently modified collections?

      Parameters:
      c - the collection for which to return an array of elements
      array - the array in which to place the collection elements
      Throws:
      ArrayStoreException - if the runtime type of the specified array is not a supertype of the runtime type of every element in the specified collection
    • toArrayImpl

      static <T> T[] toArrayImpl(Object[] src, int offset, int len, T[] dst)
      Implementation of Collection.toArray(Object[]) for collections backed by an object array. the runtime type of the returned array is that of the specified array. If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of the specified collection.

      If the collection fits in the specified array with room to spare (i.e., the array has more elements than the collection), the element in the array immediately following the end of the collection is set to null. This is useful in determining the length of the collection only if the caller knows that the collection does not contain any null elements.

    • toArrayImpl

      static Object[] toArrayImpl(Collection<?> c)
      Returns an array containing all of the elements in the specified collection. This method returns the elements in the order they are returned by the collection's iterator. The returned array is "safe" in that no references to it are maintained by the collection. The caller is thus free to modify the returned array.

      This method assumes that the collection size doesn't change while the method is running.

      TODO(kevinb): support concurrently modified collections?

      Parameters:
      c - the collection for which to return an array of elements
    • copyAsObjectArray

      static Object[] copyAsObjectArray(Object[] elements, int offset, int length)
      Returns a copy of the specified subrange of the specified array that is literally an Object[], and not e.g. a String[].
    • fillArray

      private static Object[] fillArray(Iterable<?> elements, Object[] array)
    • swap

      static void swap(Object[] array, int i, int j)
      Swaps array[i] with array[j].
    • checkElementsNotNull

      static Object[] checkElementsNotNull(Object... array)
    • checkElementsNotNull

      static Object[] checkElementsNotNull(Object[] array, int length)
    • checkElementNotNull

      static Object checkElementNotNull(Object element, int index)