ArrayUtils.java revision 5217cacbd9f382068bb9e176cd5a0b15388a335c
1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.internal.util;
18
19import android.annotation.NonNull;
20import android.annotation.Nullable;
21import android.util.ArraySet;
22
23import dalvik.system.VMRuntime;
24
25import libcore.util.EmptyArray;
26
27import java.lang.reflect.Array;
28import java.util.ArrayList;
29import java.util.Objects;
30
31/**
32 * ArrayUtils contains some methods that you can call to find out
33 * the most efficient increments by which to grow arrays.
34 */
35public class ArrayUtils {
36    private static final int CACHE_SIZE = 73;
37    private static Object[] sCache = new Object[CACHE_SIZE];
38
39    private ArrayUtils() { /* cannot be instantiated */ }
40
41    public static byte[] newUnpaddedByteArray(int minLen) {
42        return (byte[])VMRuntime.getRuntime().newUnpaddedArray(byte.class, minLen);
43    }
44
45    public static char[] newUnpaddedCharArray(int minLen) {
46        return (char[])VMRuntime.getRuntime().newUnpaddedArray(char.class, minLen);
47    }
48
49    public static int[] newUnpaddedIntArray(int minLen) {
50        return (int[])VMRuntime.getRuntime().newUnpaddedArray(int.class, minLen);
51    }
52
53    public static boolean[] newUnpaddedBooleanArray(int minLen) {
54        return (boolean[])VMRuntime.getRuntime().newUnpaddedArray(boolean.class, minLen);
55    }
56
57    public static long[] newUnpaddedLongArray(int minLen) {
58        return (long[])VMRuntime.getRuntime().newUnpaddedArray(long.class, minLen);
59    }
60
61    public static float[] newUnpaddedFloatArray(int minLen) {
62        return (float[])VMRuntime.getRuntime().newUnpaddedArray(float.class, minLen);
63    }
64
65    public static Object[] newUnpaddedObjectArray(int minLen) {
66        return (Object[])VMRuntime.getRuntime().newUnpaddedArray(Object.class, minLen);
67    }
68
69    @SuppressWarnings("unchecked")
70    public static <T> T[] newUnpaddedArray(Class<T> clazz, int minLen) {
71        return (T[])VMRuntime.getRuntime().newUnpaddedArray(clazz, minLen);
72    }
73
74    /**
75     * Checks if the beginnings of two byte arrays are equal.
76     *
77     * @param array1 the first byte array
78     * @param array2 the second byte array
79     * @param length the number of bytes to check
80     * @return true if they're equal, false otherwise
81     */
82    public static boolean equals(byte[] array1, byte[] array2, int length) {
83        if (length < 0) {
84            throw new IllegalArgumentException();
85        }
86
87        if (array1 == array2) {
88            return true;
89        }
90        if (array1 == null || array2 == null || array1.length < length || array2.length < length) {
91            return false;
92        }
93        for (int i = 0; i < length; i++) {
94            if (array1[i] != array2[i]) {
95                return false;
96            }
97        }
98        return true;
99    }
100
101    /**
102     * Returns an empty array of the specified type.  The intent is that
103     * it will return the same empty array every time to avoid reallocation,
104     * although this is not guaranteed.
105     */
106    @SuppressWarnings("unchecked")
107    public static <T> T[] emptyArray(Class<T> kind) {
108        if (kind == Object.class) {
109            return (T[]) EmptyArray.OBJECT;
110        }
111
112        int bucket = (kind.hashCode() & 0x7FFFFFFF) % CACHE_SIZE;
113        Object cache = sCache[bucket];
114
115        if (cache == null || cache.getClass().getComponentType() != kind) {
116            cache = Array.newInstance(kind, 0);
117            sCache[bucket] = cache;
118
119            // Log.e("cache", "new empty " + kind.getName() + " at " + bucket);
120        }
121
122        return (T[]) cache;
123    }
124
125    /**
126     * Checks if given array is null or has zero elements.
127     */
128    public static <T> boolean isEmpty(@Nullable T[] array) {
129        return array == null || array.length == 0;
130    }
131
132    /**
133     * Checks if given array is null or has zero elements.
134     */
135    public static boolean isEmpty(@Nullable int[] array) {
136        return array == null || array.length == 0;
137    }
138
139    /**
140     * Checks if given array is null or has zero elements.
141     */
142    public static boolean isEmpty(@Nullable long[] array) {
143        return array == null || array.length == 0;
144    }
145
146    /**
147     * Checks if given array is null or has zero elements.
148     */
149    public static boolean isEmpty(@Nullable byte[] array) {
150        return array == null || array.length == 0;
151    }
152
153    /**
154     * Checks that value is present as at least one of the elements of the array.
155     * @param array the array to check in
156     * @param value the value to check for
157     * @return true if the value is present in the array
158     */
159    public static <T> boolean contains(@Nullable T[] array, T value) {
160        return indexOf(array, value) != -1;
161    }
162
163    /**
164     * Return first index of {@code value} in {@code array}, or {@code -1} if
165     * not found.
166     */
167    public static <T> int indexOf(@Nullable T[] array, T value) {
168        if (array == null) return -1;
169        for (int i = 0; i < array.length; i++) {
170            if (Objects.equals(array[i], value)) return i;
171        }
172        return -1;
173    }
174
175    /**
176     * Test if all {@code check} items are contained in {@code array}.
177     */
178    public static <T> boolean containsAll(@Nullable T[] array, T[] check) {
179        if (check == null) return true;
180        for (T checkItem : check) {
181            if (!contains(array, checkItem)) {
182                return false;
183            }
184        }
185        return true;
186    }
187
188    public static boolean contains(@Nullable int[] array, int value) {
189        if (array == null) return false;
190        for (int element : array) {
191            if (element == value) {
192                return true;
193            }
194        }
195        return false;
196    }
197
198    public static boolean contains(@Nullable long[] array, long value) {
199        if (array == null) return false;
200        for (long element : array) {
201            if (element == value) {
202                return true;
203            }
204        }
205        return false;
206    }
207
208    public static long total(@Nullable long[] array) {
209        long total = 0;
210        if (array != null) {
211            for (long value : array) {
212                total += value;
213            }
214        }
215        return total;
216    }
217
218    /**
219     * Adds value to given array if not already present, providing set-like
220     * behavior.
221     */
222    @SuppressWarnings("unchecked")
223    public static @NonNull <T> T[] appendElement(Class<T> kind, @Nullable T[] array, T element) {
224        final T[] result;
225        final int end;
226        if (array != null) {
227            if (contains(array, element)) return array;
228            end = array.length;
229            result = (T[])Array.newInstance(kind, end + 1);
230            System.arraycopy(array, 0, result, 0, end);
231        } else {
232            end = 0;
233            result = (T[])Array.newInstance(kind, 1);
234        }
235        result[end] = element;
236        return result;
237    }
238
239    /**
240     * Removes value from given array if present, providing set-like behavior.
241     */
242    @SuppressWarnings("unchecked")
243    public static @Nullable <T> T[] removeElement(Class<T> kind, @Nullable T[] array, T element) {
244        if (array != null) {
245            if (!contains(array, element)) return array;
246            final int length = array.length;
247            for (int i = 0; i < length; i++) {
248                if (Objects.equals(array[i], element)) {
249                    if (length == 1) {
250                        return null;
251                    }
252                    T[] result = (T[])Array.newInstance(kind, length - 1);
253                    System.arraycopy(array, 0, result, 0, i);
254                    System.arraycopy(array, i + 1, result, i, length - i - 1);
255                    return result;
256                }
257            }
258        }
259        return array;
260    }
261
262    /**
263     * Adds value to given array if not already present, providing set-like
264     * behavior.
265     */
266    public static @NonNull int[] appendInt(@Nullable int[] cur, int val) {
267        if (cur == null) {
268            return new int[] { val };
269        }
270        final int N = cur.length;
271        for (int i = 0; i < N; i++) {
272            if (cur[i] == val) {
273                return cur;
274            }
275        }
276        int[] ret = new int[N + 1];
277        System.arraycopy(cur, 0, ret, 0, N);
278        ret[N] = val;
279        return ret;
280    }
281
282    /**
283     * Removes value from given array if present, providing set-like behavior.
284     */
285    public static @Nullable int[] removeInt(@Nullable int[] cur, int val) {
286        if (cur == null) {
287            return null;
288        }
289        final int N = cur.length;
290        for (int i = 0; i < N; i++) {
291            if (cur[i] == val) {
292                int[] ret = new int[N - 1];
293                if (i > 0) {
294                    System.arraycopy(cur, 0, ret, 0, i);
295                }
296                if (i < (N - 1)) {
297                    System.arraycopy(cur, i + 1, ret, i, N - i - 1);
298                }
299                return ret;
300            }
301        }
302        return cur;
303    }
304
305    /**
306     * Removes value from given array if present, providing set-like behavior.
307     */
308    public static @Nullable String[] removeString(@Nullable String[] cur, String val) {
309        if (cur == null) {
310            return null;
311        }
312        final int N = cur.length;
313        for (int i = 0; i < N; i++) {
314            if (Objects.equals(cur[i], val)) {
315                String[] ret = new String[N - 1];
316                if (i > 0) {
317                    System.arraycopy(cur, 0, ret, 0, i);
318                }
319                if (i < (N - 1)) {
320                    System.arraycopy(cur, i + 1, ret, i, N - i - 1);
321                }
322                return ret;
323            }
324        }
325        return cur;
326    }
327
328    /**
329     * Adds value to given array if not already present, providing set-like
330     * behavior.
331     */
332    public static @NonNull long[] appendLong(@Nullable long[] cur, long val) {
333        if (cur == null) {
334            return new long[] { val };
335        }
336        final int N = cur.length;
337        for (int i = 0; i < N; i++) {
338            if (cur[i] == val) {
339                return cur;
340            }
341        }
342        long[] ret = new long[N + 1];
343        System.arraycopy(cur, 0, ret, 0, N);
344        ret[N] = val;
345        return ret;
346    }
347
348    /**
349     * Removes value from given array if present, providing set-like behavior.
350     */
351    public static @Nullable long[] removeLong(@Nullable long[] cur, long val) {
352        if (cur == null) {
353            return null;
354        }
355        final int N = cur.length;
356        for (int i = 0; i < N; i++) {
357            if (cur[i] == val) {
358                long[] ret = new long[N - 1];
359                if (i > 0) {
360                    System.arraycopy(cur, 0, ret, 0, i);
361                }
362                if (i < (N - 1)) {
363                    System.arraycopy(cur, i + 1, ret, i, N - i - 1);
364                }
365                return ret;
366            }
367        }
368        return cur;
369    }
370
371    public static @Nullable long[] cloneOrNull(@Nullable long[] array) {
372        return (array != null) ? array.clone() : null;
373    }
374
375    public static @NonNull <T> ArraySet<T> add(@Nullable ArraySet<T> cur, T val) {
376        if (cur == null) {
377            cur = new ArraySet<>();
378        }
379        cur.add(val);
380        return cur;
381    }
382
383    public static @Nullable <T> ArraySet<T> remove(@Nullable ArraySet<T> cur, T val) {
384        if (cur == null) {
385            return null;
386        }
387        cur.remove(val);
388        if (cur.isEmpty()) {
389            return null;
390        } else {
391            return cur;
392        }
393    }
394
395    public static <T> boolean contains(@Nullable ArraySet<T> cur, T val) {
396        return (cur != null) ? cur.contains(val) : false;
397    }
398
399    public static @NonNull <T> ArrayList<T> add(@Nullable ArrayList<T> cur, T val) {
400        if (cur == null) {
401            cur = new ArrayList<>();
402        }
403        cur.add(val);
404        return cur;
405    }
406
407    public static @Nullable <T> ArrayList<T> remove(@Nullable ArrayList<T> cur, T val) {
408        if (cur == null) {
409            return null;
410        }
411        cur.remove(val);
412        if (cur.isEmpty()) {
413            return null;
414        } else {
415            return cur;
416        }
417    }
418
419    public static <T> boolean contains(@Nullable ArrayList<T> cur, T val) {
420        return (cur != null) ? cur.contains(val) : false;
421    }
422
423    /**
424     * Returns true if the two ArrayLists are equal with respect to the objects they contain.
425     * The objects must be in the same order and be reference equal (== not .equals()).
426     */
427    public static <T> boolean referenceEquals(ArrayList<T> a, ArrayList<T> b) {
428        if (a == b) {
429            return true;
430        }
431
432        final int sizeA = a.size();
433        final int sizeB = b.size();
434        if (a == null || b == null || sizeA != sizeB) {
435            return false;
436        }
437
438        boolean diff = false;
439        for (int i = 0; i < sizeA && !diff; i++) {
440            diff |= a.get(i) != b.get(i);
441        }
442        return !diff;
443    }
444}
445