ArrayUtils.java revision 630a1712168f402653039e368259cb9480454fa8
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 java.lang.reflect.Array;
20import java.util.Collection;
21
22// XXX these should be changed to reflect the actual memory allocator we use.
23// it looks like right now objects want to be powers of 2 minus 8
24// and the array size eats another 4 bytes
25
26/**
27 * ArrayUtils contains some methods that you can call to find out
28 * the most efficient increments by which to grow arrays.
29 */
30public class ArrayUtils
31{
32    private static Object[] EMPTY = new Object[0];
33    private static final int CACHE_SIZE = 73;
34    private static Object[] sCache = new Object[CACHE_SIZE];
35
36    private ArrayUtils() { /* cannot be instantiated */ }
37
38    public static int idealByteArraySize(int need) {
39        for (int i = 4; i < 32; i++)
40            if (need <= (1 << i) - 12)
41                return (1 << i) - 12;
42
43        return need;
44    }
45
46    public static int idealBooleanArraySize(int need) {
47        return idealByteArraySize(need);
48    }
49
50    public static int idealShortArraySize(int need) {
51        return idealByteArraySize(need * 2) / 2;
52    }
53
54    public static int idealCharArraySize(int need) {
55        return idealByteArraySize(need * 2) / 2;
56    }
57
58    public static int idealIntArraySize(int need) {
59        return idealByteArraySize(need * 4) / 4;
60    }
61
62    public static int idealFloatArraySize(int need) {
63        return idealByteArraySize(need * 4) / 4;
64    }
65
66    public static int idealObjectArraySize(int need) {
67        return idealByteArraySize(need * 4) / 4;
68    }
69
70    public static int idealLongArraySize(int need) {
71        return idealByteArraySize(need * 8) / 8;
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 (array1 == array2) {
84            return true;
85        }
86        if (array1 == null || array2 == null || array1.length < length || array2.length < length) {
87            return false;
88        }
89        for (int i = 0; i < length; i++) {
90            if (array1[i] != array2[i]) {
91                return false;
92            }
93        }
94        return true;
95    }
96
97    /**
98     * Returns an empty array of the specified type.  The intent is that
99     * it will return the same empty array every time to avoid reallocation,
100     * although this is not guaranteed.
101     */
102    public static <T> T[] emptyArray(Class<T> kind) {
103        if (kind == Object.class) {
104            return (T[]) EMPTY;
105        }
106
107        int bucket = ((System.identityHashCode(kind) / 8) & 0x7FFFFFFF) % CACHE_SIZE;
108        Object cache = sCache[bucket];
109
110        if (cache == null || cache.getClass().getComponentType() != kind) {
111            cache = Array.newInstance(kind, 0);
112            sCache[bucket] = cache;
113
114            // Log.e("cache", "new empty " + kind.getName() + " at " + bucket);
115        }
116
117        return (T[]) cache;
118    }
119
120    /**
121     * Checks that value is present as at least one of the elements of the array.
122     * @param array the array to check in
123     * @param value the value to check for
124     * @return true if the value is present in the array
125     */
126    public static <T> boolean contains(T[] array, T value) {
127        for (T element : array) {
128            if (element == null) {
129                if (value == null) return true;
130            } else {
131                if (value != null && element.equals(value)) return true;
132            }
133        }
134        return false;
135    }
136
137    public static boolean contains(int[] array, int value) {
138        for (int element : array) {
139            if (element == value) {
140                return true;
141            }
142        }
143        return false;
144    }
145}
146