Class ObjectArrays


  • @GwtCompatible(emulated=true)
    public final class ObjectArrays
    extends java.lang.Object
    Static utility methods pertaining to object arrays.
    Since:
    2.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      (package private) static java.lang.Object[] EMPTY_ARRAY  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ObjectArrays()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) static <T> T[] arraysCopyOf​(T[] original, int newLength)
      GWT safe version of Arrays.copyOf.
      (package private) static java.lang.Object checkElementNotNull​(java.lang.Object element, int index)  
      (package private) static java.lang.Object[] checkElementsNotNull​(java.lang.Object... array)  
      (package private) static java.lang.Object[] checkElementsNotNull​(java.lang.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, java.lang.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 java.lang.Object[] copyAsObjectArray​(java.lang.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 java.lang.Object[] fillArray​(java.lang.Iterable<?> elements, java.lang.Object[] array)  
      static <T> T[] newArray​(java.lang.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​(java.lang.Object[] array, int i, int j)
      Swaps array[i] with array[j].
      (package private) static <T> T[] toArrayImpl​(java.lang.Object[] src, int offset, int len, T[] dst)
      Implementation of Collection.toArray(Object[]) for collections backed by an object array.
      (package private) static java.lang.Object[] toArrayImpl​(java.util.Collection<?> c)
      Returns an array containing all of the elements in the specified collection.
      (package private) static <T> T[] toArrayImpl​(java.util.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
    • Field Detail

      • EMPTY_ARRAY

        static final java.lang.Object[] EMPTY_ARRAY
    • Constructor Detail

      • ObjectArrays

        private ObjectArrays()
    • Method Detail

      • newArray

        @GwtIncompatible
        public static <T> T[] newArray​(java.lang.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

        @GwtIncompatible
        public static <T> T[] concat​(T[] first,
                                     T[] second,
                                     java.lang.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​(@Nullable
                                     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,
                                     @Nullable
                                     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.
      • arraysCopyOf

        static <T> T[] arraysCopyOf​(T[] original,
                                    int newLength)
        GWT safe version of Arrays.copyOf.
      • toArrayImpl

        static <T> T[] toArrayImpl​(java.util.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:
        java.lang.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​(java.lang.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 java.lang.Object[] toArrayImpl​(java.util.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 java.lang.Object[] copyAsObjectArray​(java.lang.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 java.lang.Object[] fillArray​(java.lang.Iterable<?> elements,
                                                    java.lang.Object[] array)
      • swap

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

        static java.lang.Object[] checkElementsNotNull​(java.lang.Object... array)
      • checkElementsNotNull

        static java.lang.Object[] checkElementsNotNull​(java.lang.Object[] array,
                                                       int length)
      • checkElementNotNull

        static java.lang.Object checkElementNotNull​(java.lang.Object element,
                                                    int index)