Top Description Constructors Methods
com.google.common.collect

public final Class ObjectArrays

extends Object
Class Inheritance
Annotations
@GwtCompatible
emulated:true
@ElementTypesAreNonnullByDefault
@SuppressWarnings:AvoidObjectArrays
Static Imports
com.google.common.base.Preconditions.checkPositionIndexes

Static utility methods pertaining to object arrays.
Author
Kevin Bourrillion
Since
2.0

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
pack-priv static Object
checkElementNotNull(Object element, int index)

pack-priv static Object[]
pack-priv static Object[]
checkElementsNotNull(Object[] array, int length)

public static <T extends @Nullable Object> T[]
concat(T[]
the first array of elements to concatenate
first
,
T[]
the second array of elements to concatenate
second
,
Class<@NonNull T>
the component type of the returned array
type
)

Returns a new array that contains the concatenated contents of two arrays.

public static <T extends @Nullable Object> T[]

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
(T
the element to prepend to the front of array
element
,
T[]
the array of elements to append
array
)

Returns a new array that prepends element to array.

public static <T extends @Nullable Object> T[]

Returns:

an array whose size is one larger than array, with the same contents as array, plus element occupying the last position.
concat
(T[]
the array of elements to prepend
array
,
T
the element to append to the end
element
)

Returns a new array that appends element to array.

pack-priv static @Nullable Object[]
copyAsObjectArray(@Nullable 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[].

private static @Nullable Object[]
fillArray(Iterable<?> elements, @Nullable Object[] array)

public static <T extends @Nullable Object> T[]
newArray(Class<@NonNull T>
the component type
type
,
int
the length of the new array
length
)

Returns a new array of the given length with the specified component type.

public static <T extends @Nullable Object> T[]
newArray(T[]
any array of the desired type
reference
,
int
the length of the new array
length
)

Returns a new array of the given length with the same type as a reference array.

pack-priv static void
swap(Object[] array, int i, int j)

Swaps array[i] with array[j].

pack-priv static <T extends @Nullable Object> T[]
toArrayImpl(Collection<?>
the collection for which to return an array of elements
c
,
T[]
the array in which to place the collection elements
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.

pack-priv static <T extends @Nullable Object> T[]
toArrayImpl(@Nullable Object[] src, int offset, int len, T[] dst)

Implementation of Collection#toArray(Object[]) for collections backed by an object array.

pack-priv static @Nullable Object[]
toArrayImpl(Collection<?>
the collection for which to return an array of elements
c
)

Returns an array containing all of the elements in the specified collection.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

ObjectArraysback to summary
private ObjectArrays()

Method Detail

checkElementNotNullback to summary
pack-priv static Object checkElementNotNull(Object element, int index)
Annotations
@CanIgnoreReturnValue
checkElementsNotNullback to summary
pack-priv static Object[] checkElementsNotNull(Object... array)
Annotations
@CanIgnoreReturnValue
checkElementsNotNullback to summary
pack-priv static Object[] checkElementsNotNull(Object[] array, int length)
Annotations
@CanIgnoreReturnValue
concatback to summary
public static <T extends @Nullable Object> T[] concat(T[] first, T[] second, Class<@NonNull T> type)

Returns a new array that contains the concatenated contents of two arrays.

Parameters
first:T[]

the first array of elements to concatenate

second:T[]

the second array of elements to concatenate

type:Class<@NonNull T>

the component type of the returned array

Annotations
@GwtIncompatible
concatback to summary
public static <T extends @Nullable Object> T[] concat(T element, T[] array)

Returns a new array that prepends element to array.

Parameters
element:T

the element to prepend to the front of array

array:T[]

the array of elements to append

Returns:T[]

an array whose size is one larger than array, with element occupying the first position, and the elements of array occupying the remaining elements.

concatback to summary
public static <T extends @Nullable Object> T[] concat(T[] array, T element)

Returns a new array that appends element to array.

Parameters
array:T[]

the array of elements to prepend

element:T

the element to append to the end

Returns:T[]

an array whose size is one larger than array, with the same contents as array, plus element occupying the last position.

copyAsObjectArrayback to summary
pack-priv static @Nullable Object[] copyAsObjectArray(@Nullable 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[].

fillArrayback to summary
private static @Nullable Object[] fillArray(Iterable<?> elements, @Nullable Object[] array)
Annotations
@CanIgnoreReturnValue
newArrayback to summary
public static <T extends @Nullable Object> T[] newArray(Class<@NonNull T> type, int length)

Returns a new array of the given length with the specified component type.

Parameters
type:Class<@NonNull T>

the component type

length:int

the length of the new array

Annotations
@GwtIncompatible
@SuppressWarnings:unchecked
newArrayback to summary
public static <T extends @Nullable Object> T[] newArray(T[] reference, int length)

Returns a new array of the given length with the same type as a reference array.

Parameters
reference:T[]

any array of the desired type

length:int

the length of the new array

swapback to summary
pack-priv static void swap(Object[] array, int i, int j)

Swaps array[i] with array[j].

toArrayImplback to summary
pack-priv static <T extends @Nullable Object> 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:Collection<?>

the collection for which to return an array of elements

array:T[]

the array in which to place the collection elements

Exceptions
ArrayStoreException:
if the runtime type of the specified array is not a supertype of the runtime type of every element in the specified collection
toArrayImplback to summary
pack-priv static <T extends @Nullable Object> T[] toArrayImpl(@Nullable 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.

toArrayImplback to summary
pack-priv static @Nullable 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:Collection<?>

the collection for which to return an array of elements