Package com.android.internal.util
Class ArrayUtils
- java.lang.Object
-
- com.android.internal.util.ArrayUtils
-
public class ArrayUtils extends java.lang.ObjectStatic utility methods for arrays that aren't already included inArrays.
-
-
Field Summary
Fields Modifier and Type Field Description static java.io.File[]EMPTY_FILE
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static <T> android.util.ArraySet<T>add(android.util.ArraySet<T> cur, T val)static <T> java.util.ArrayList<T>add(java.util.ArrayList<T> cur, int index, T val)static <T> java.util.ArrayList<T>add(java.util.ArrayList<T> cur, T val)static <T> android.util.ArraySet<T>addAll(android.util.ArraySet<T> cur, java.util.Collection<T> val)Similar toSet.addAll(Collection)}, but with support for set values ofnull.static <T> T[]appendElement(java.lang.Class<T> kind, T[] array, T element)Adds value to given array if not already present, providing set-like behavior.static <T> T[]appendElement(java.lang.Class<T> kind, T[] array, T element, boolean allowDuplicates)Adds value to given array.static int[]appendInt(int[] cur, int val)Adds value to given array if not already present, providing set-like behavior.static int[]appendInt(int[] cur, int val, boolean allowDuplicates)Adds value to given array.static long[]appendLong(long[] cur, long val)Adds value to given array if not already present, providing set-like behavior.static long[]appendLong(long[] cur, long val, boolean allowDuplicates)Adds value to given array if not already present, providing set-like behavior.static voidcheckBounds(int len, int index)ThrowsArrayIndexOutOfBoundsExceptionif the index is out of bounds.static long[]cloneOrNull(long[] array)static <T> android.util.ArraySet<T>cloneOrNull(android.util.ArraySet<T> array)static <T> T[]cloneOrNull(T[] array)Clones an array or returns null if the array is null.static byte[]concat(byte[]... arrays)Returns the concatenation of the given byte arrays.static <T> T[]concat(java.lang.Class<T> kind, T[]... arrays)Returns the concatenation of the given arrays.static booleancontains(char[] array, char value)static booleancontains(int[] array, int value)static booleancontains(long[] array, long value)static <T> booleancontains(java.util.Collection<T> cur, T val)static <T> booleancontains(T[] array, T value)Checks that value is present as at least one of the elements of the array.static <T> booleancontainsAll(char[] array, char[] check)Test if allcheckitems are contained inarray.static <T> booleancontainsAll(T[] array, T[] check)Test if allcheckitems are contained inarray.static <T> booleancontainsAny(T[] array, T[] check)Test if anycheckitems are contained inarray.static int[]convertToIntArray(android.util.ArraySet<java.lang.Integer> set)static int[]convertToIntArray(java.util.List<java.lang.Integer> list)Deprecated.useIntArrayinsteadstatic long[]convertToLongArray(int[] intArray)static java.lang.StringdeepToString(java.lang.Object value)static int[]defeatNullable(int[] val)static java.io.File[]defeatNullable(java.io.File[] val)static java.lang.String[]defeatNullable(java.lang.String[] val)static <T> T[]emptyArray(java.lang.Class<T> kind)Returns an empty array of the specified type.static <T> T[]emptyIfNull(T[] items, java.lang.Class<T> kind)Returns the same array or an empty one if it's null.static booleanequals(byte[] array1, byte[] array2, int length)Checks if the beginnings of two byte arrays are equal.static <T> T[]filter(T[] items, java.util.function.IntFunction<T[]> arrayConstructor, java.util.function.Predicate<T> predicate)Returns an array containing elements from the given one that match the given predicate.static <T> T[]filterNotNull(T[] val, java.util.function.IntFunction<T[]> arrayConstructor)Returns an array with values fromvalminusnullvaluesstatic <T> Tfind(T[] items, java.util.function.Predicate<T> predicate)Returns the first element from the array for which conditionpredicateis true, or null if there is no such elementstatic <T> TfirstOrNull(T[] items)static <T> TgetOrNull(T[] items, int i)Returns thei-th item initems, if it exists anditemsis notnull, otherwise returnsnull.static <T> intindexOf(T[] array, T value)Return first index ofvalueinarray, or-1if not found.static booleanisEmpty(boolean[] array)Checks if given array is null or has zero elements.static booleanisEmpty(byte[] array)Checks if given array is null or has zero elements.static booleanisEmpty(int[] array)Checks if given array is null or has zero elements.static booleanisEmpty(long[] array)Checks if given array is null or has zero elements.static booleanisEmpty(java.util.Collection<?> array)Checks if given array is null or has zero elements.static booleanisEmpty(java.util.Map<?,?> map)Checks if given map is null or has zero elements.static <T> booleanisEmpty(T[] array)Checks if given array is null or has zero elements.static <T> T[]newUnpaddedArray(java.lang.Class<T> clazz, int minLen)static boolean[]newUnpaddedBooleanArray(int minLen)static byte[]newUnpaddedByteArray(int minLen)static char[]newUnpaddedCharArray(int minLen)static float[]newUnpaddedFloatArray(int minLen)static int[]newUnpaddedIntArray(int minLen)static long[]newUnpaddedLongArray(int minLen)static java.lang.Object[]newUnpaddedObjectArray(int minLen)static <T> booleanreferenceEquals(java.util.ArrayList<T> a, java.util.ArrayList<T> b)Returns true if the two ArrayLists are equal with respect to the objects they contain.static <T> android.util.ArraySet<T>remove(android.util.ArraySet<T> cur, T val)static <T> java.util.ArrayList<T>remove(java.util.ArrayList<T> cur, T val)static <T> T[]removeElement(java.lang.Class<T> kind, T[] array, T element)Removes value from given array if present, providing set-like behavior.static int[]removeInt(int[] cur, int val)Removes value from given array if present, providing set-like behavior.static long[]removeLong(long[] cur, long val)Removes value from given array if present, providing set-like behavior.static java.lang.String[]removeString(java.lang.String[] cur, java.lang.String val)Removes value from given array if present, providing set-like behavior.static intsize(java.lang.Object[] array)Length of the given array or 0 if it's null.static intsize(java.util.Collection<?> collection)Length of the given collection or 0 if it's null.static intsize(java.util.Map<?,?> map)Length of the given map or 0 if it's null.static booleanstartsWith(byte[] cur, byte[] val)static voidthrowsIfOutOfBounds(int len, int offset, int count)ThrowsArrayIndexOutOfBoundsExceptionif the range is out of bounds.static <T> java.util.List<T>toList(T[] array)Creates aListfrom an array.static longtotal(long[] array)static <T> T[]trimToSize(T[] array, int size)static <T> intunstableRemoveIf(java.util.ArrayList<T> collection, java.util.function.Predicate<T> predicate)Removes elements that match the predicate in an efficient way that alters the order of elements in the collection.
-
-
-
Method Detail
-
newUnpaddedByteArray
public static byte[] newUnpaddedByteArray(int minLen)
-
newUnpaddedCharArray
public static char[] newUnpaddedCharArray(int minLen)
-
newUnpaddedIntArray
public static int[] newUnpaddedIntArray(int minLen)
-
newUnpaddedBooleanArray
public static boolean[] newUnpaddedBooleanArray(int minLen)
-
newUnpaddedLongArray
public static long[] newUnpaddedLongArray(int minLen)
-
newUnpaddedFloatArray
public static float[] newUnpaddedFloatArray(int minLen)
-
newUnpaddedObjectArray
public static java.lang.Object[] newUnpaddedObjectArray(int minLen)
-
newUnpaddedArray
public static <T> T[] newUnpaddedArray(java.lang.Class<T> clazz, int minLen)
-
equals
public static boolean equals(byte[] array1, byte[] array2, int length)Checks if the beginnings of two byte arrays are equal.- Parameters:
array1- the first byte arrayarray2- the second byte arraylength- the number of bytes to check- Returns:
- true if they're equal, false otherwise
-
emptyArray
public static <T> T[] emptyArray(java.lang.Class<T> kind)
Returns an empty array of the specified type. The intent is that it will return the same empty array every time to avoid reallocation, although this is not guaranteed.
-
emptyIfNull
public static <T> T[] emptyIfNull(T[] items, java.lang.Class<T> kind)Returns the same array or an empty one if it's null.
-
isEmpty
public static boolean isEmpty(java.util.Collection<?> array)
Checks if given array is null or has zero elements.
-
isEmpty
public static boolean isEmpty(java.util.Map<?,?> map)
Checks if given map is null or has zero elements.
-
isEmpty
public static <T> boolean isEmpty(T[] array)
Checks if given array is null or has zero elements.
-
isEmpty
public static boolean isEmpty(int[] array)
Checks if given array is null or has zero elements.
-
isEmpty
public static boolean isEmpty(long[] array)
Checks if given array is null or has zero elements.
-
isEmpty
public static boolean isEmpty(byte[] array)
Checks if given array is null or has zero elements.
-
isEmpty
public static boolean isEmpty(boolean[] array)
Checks if given array is null or has zero elements.
-
size
public static int size(java.lang.Object[] array)
Length of the given array or 0 if it's null.
-
size
public static int size(java.util.Collection<?> collection)
Length of the given collection or 0 if it's null.
-
size
public static int size(java.util.Map<?,?> map)
Length of the given map or 0 if it's null.
-
contains
public static <T> boolean contains(T[] array, T value)Checks that value is present as at least one of the elements of the array.- Parameters:
array- the array to check invalue- the value to check for- Returns:
- true if the value is present in the array
-
indexOf
public static <T> int indexOf(T[] array, T value)Return first index ofvalueinarray, or-1if not found.
-
containsAll
public static <T> boolean containsAll(T[] array, T[] check)Test if allcheckitems are contained inarray.
-
containsAny
public static <T> boolean containsAny(T[] array, T[] check)Test if anycheckitems are contained inarray.
-
contains
public static boolean contains(int[] array, int value)
-
contains
public static boolean contains(long[] array, long value)
-
contains
public static boolean contains(char[] array, char value)
-
containsAll
public static <T> boolean containsAll(char[] array, char[] check)Test if allcheckitems are contained inarray.
-
total
public static long total(long[] array)
-
convertToIntArray
@Deprecated public static int[] convertToIntArray(java.util.List<java.lang.Integer> list)
Deprecated.useIntArrayinstead
-
convertToIntArray
public static int[] convertToIntArray(android.util.ArraySet<java.lang.Integer> set)
-
convertToLongArray
public static long[] convertToLongArray(int[] intArray)
-
concat
public static <T> T[] concat(java.lang.Class<T> kind, T[]... arrays)Returns the concatenation of the given arrays. Only works for object arrays, not for primitive arrays. Seeconcat(byte[]...)for a variant that works on byte arrays.- Type Parameters:
T- The class of the array elements (inferred from kind).- Parameters:
kind- The class of the array elementsarrays- The arrays to concatenate. Null arrays are treated as empty.- Returns:
- A single array containing all the elements of the parameter arrays.
-
concat
public static byte[] concat(byte[]... arrays)
Returns the concatenation of the given byte arrays. Null arrays are treated as empty.
-
appendElement
public static <T> T[] appendElement(java.lang.Class<T> kind, T[] array, T element)Adds value to given array if not already present, providing set-like behavior.
-
appendElement
public static <T> T[] appendElement(java.lang.Class<T> kind, T[] array, T element, boolean allowDuplicates)Adds value to given array.
-
removeElement
public static <T> T[] removeElement(java.lang.Class<T> kind, T[] array, T element)Removes value from given array if present, providing set-like behavior.
-
appendInt
public static int[] appendInt(int[] cur, int val, boolean allowDuplicates)Adds value to given array.
-
appendInt
public static int[] appendInt(int[] cur, int val)Adds value to given array if not already present, providing set-like behavior.
-
removeInt
public static int[] removeInt(int[] cur, int val)Removes value from given array if present, providing set-like behavior.
-
removeString
public static java.lang.String[] removeString(java.lang.String[] cur, java.lang.String val)Removes value from given array if present, providing set-like behavior.
-
appendLong
public static long[] appendLong(long[] cur, long val, boolean allowDuplicates)Adds value to given array if not already present, providing set-like behavior.
-
appendLong
public static long[] appendLong(long[] cur, long val)Adds value to given array if not already present, providing set-like behavior.
-
removeLong
public static long[] removeLong(long[] cur, long val)Removes value from given array if present, providing set-like behavior.
-
cloneOrNull
public static long[] cloneOrNull(long[] array)
-
cloneOrNull
public static <T> T[] cloneOrNull(T[] array)
Clones an array or returns null if the array is null.
-
cloneOrNull
public static <T> android.util.ArraySet<T> cloneOrNull(android.util.ArraySet<T> array)
-
add
public static <T> android.util.ArraySet<T> add(android.util.ArraySet<T> cur, T val)
-
addAll
public static <T> android.util.ArraySet<T> addAll(android.util.ArraySet<T> cur, java.util.Collection<T> val)Similar toSet.addAll(Collection)}, but with support for set values ofnull.
-
remove
public static <T> android.util.ArraySet<T> remove(android.util.ArraySet<T> cur, T val)
-
add
public static <T> java.util.ArrayList<T> add(java.util.ArrayList<T> cur, T val)
-
add
public static <T> java.util.ArrayList<T> add(java.util.ArrayList<T> cur, int index, T val)
-
remove
public static <T> java.util.ArrayList<T> remove(java.util.ArrayList<T> cur, T val)
-
contains
public static <T> boolean contains(java.util.Collection<T> cur, T val)
-
trimToSize
public static <T> T[] trimToSize(T[] array, int size)
-
referenceEquals
public static <T> boolean referenceEquals(java.util.ArrayList<T> a, java.util.ArrayList<T> b)Returns true if the two ArrayLists are equal with respect to the objects they contain. The objects must be in the same order and be reference equal (== not .equals()).
-
unstableRemoveIf
public static <T> int unstableRemoveIf(java.util.ArrayList<T> collection, java.util.function.Predicate<T> predicate)Removes elements that match the predicate in an efficient way that alters the order of elements in the collection. This should only be used if order is not important.- Parameters:
collection- The ArrayList from which to remove elements.predicate- The predicate that each element is tested against.- Returns:
- the number of elements removed.
-
defeatNullable
public static int[] defeatNullable(int[] val)
-
defeatNullable
public static java.lang.String[] defeatNullable(java.lang.String[] val)
-
defeatNullable
public static java.io.File[] defeatNullable(java.io.File[] val)
-
checkBounds
public static void checkBounds(int len, int index)ThrowsArrayIndexOutOfBoundsExceptionif the index is out of bounds.- Parameters:
len- length of the array. Must be non-negativeindex- the index to check- Throws:
java.lang.ArrayIndexOutOfBoundsException- if theindexis out of bounds of the array
-
throwsIfOutOfBounds
public static void throwsIfOutOfBounds(int len, int offset, int count)ThrowsArrayIndexOutOfBoundsExceptionif the range is out of bounds.- Parameters:
len- length of the array. Must be non-negativeoffset- start index of the range. Must be non-negativecount- length of the range. Must be non-negative- Throws:
java.lang.ArrayIndexOutOfBoundsException- if the range fromoffsetwith lengthcountis out of bounds of the array
-
filterNotNull
public static <T> T[] filterNotNull(T[] val, java.util.function.IntFunction<T[]> arrayConstructor)Returns an array with values fromvalminusnullvalues- Parameters:
arrayConstructor- typicallyT[]::newe.g.String[]::new
-
filter
public static <T> T[] filter(T[] items, java.util.function.IntFunction<T[]> arrayConstructor, java.util.function.Predicate<T> predicate)Returns an array containing elements from the given one that match the given predicate. The returned array may, in some cases, be the reference to the input array.
-
startsWith
public static boolean startsWith(byte[] cur, byte[] val)
-
find
public static <T> T find(T[] items, java.util.function.Predicate<T> predicate)Returns the first element from the array for which conditionpredicateis true, or null if there is no such element
-
deepToString
public static java.lang.String deepToString(java.lang.Object value)
-
getOrNull
public static <T> T getOrNull(T[] items, int i)Returns thei-th item initems, if it exists anditemsis notnull, otherwise returnsnull.
-
firstOrNull
public static <T> T firstOrNull(T[] items)
-
toList
public static <T> java.util.List<T> toList(T[] array)
Creates aListfrom an array. Different fromArrays.asList(Object[])as that will use the parameter as the backing array, meaning changes are not isolated.
-
-