Package com.android.internal.util
Class ArrayUtils
java.lang.Object
com.android.internal.util.ArrayUtils
Static utility methods for arrays that aren't already included in
Arrays.-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> android.util.ArraySet<T>add(android.util.ArraySet<T> cur, T val) static <T> ArrayList<T>static <T> ArrayList<T>static <T> android.util.ArraySet<T>addAll(android.util.ArraySet<T> cur, Collection<T> val) Similar toSet.addAll(Collection)}, but with support for set values ofnull.static <T> T[]appendElement(Class<T> kind, T[] array, T element) Adds value to given array if not already present, providing set-like behavior.static <T> T[]appendElement(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[]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(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<Integer> set) static int[]convertToIntArray(List<Integer> list) Deprecated.static long[]convertToLongArray(int[] intArray) static StringdeepToString(Object value) static int[]defeatNullable(int[] val) static File[]defeatNullable(File[] val) static String[]defeatNullable(String[] val) static <T> T[]emptyArray(Class<T> kind) Returns an empty array of the specified type.static <T> T[]emptyIfNull(T[] items, 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, IntFunction<T[]> arrayConstructor, Predicate<T> predicate) Returns an array containing elements from the given one that match the given predicate.static <T> T[]filterNotNull(T[] val, IntFunction<T[]> arrayConstructor) Returns an array with values fromvalminusnullvaluesstatic <T> TReturns 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(Collection<?> array) Checks if given array is null or has zero elements.static booleanChecks 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(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 Object[]newUnpaddedObjectArray(int minLen) static <T> booleanreferenceEquals(ArrayList<T> a, 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> ArrayList<T>static <T> T[]removeElement(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 String[]removeString(String[] cur, String val) Removes value from given array if present, providing set-like behavior.static intLength of the given array or 0 if it's null.static intsize(Collection<?> collection) Length of the given collection or 0 if it's null.static intLength 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> List<T>toList(T[] array) Creates aListfrom an array.static longtotal(long[] array) static <T> T[]trimToSize(T[] array, int size) static <T> intunstableRemoveIf(ArrayList<T> collection, Predicate<T> predicate) Removes elements that match the predicate in an efficient way that alters the order of elements in the collection.
-
Field Details
-
EMPTY_FILE
-
-
Method Details
-
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
-
newUnpaddedArray
-
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
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
Returns the same array or an empty one if it's null. -
isEmpty
Checks if given array is null or has zero elements. -
isEmpty
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
Length of the given array or 0 if it's null. -
size
Length of the given collection or 0 if it's null. -
size
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.useIntArrayinstead -
convertToIntArray
-
convertToLongArray
public static long[] convertToLongArray(int[] intArray) -
concat
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
Adds value to given array if not already present, providing set-like behavior. -
appendElement
Adds value to given array. -
removeElement
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
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
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
-
add
-
remove
-
contains
-
trimToSize
public static <T> T[] trimToSize(T[] array, int size) -
referenceEquals
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
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
-
defeatNullable
-
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:
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:
ArrayIndexOutOfBoundsException- if the range fromoffsetwith lengthcountis out of bounds of the array
-
filterNotNull
Returns an array with values fromvalminusnullvalues- Parameters:
arrayConstructor- typicallyT[]::newe.g.String[]::new
-
filter
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
Returns the first element from the array for which conditionpredicateis true, or null if there is no such element -
deepToString
-
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
Creates aListfrom an array. Different fromArrays.asList(Object[])as that will use the parameter as the backing array, meaning changes are not isolated.
-
IntArrayinstead