ArrayUtils.java revision 57dcf5b177b56195421535938544f32d8b591b42
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 dalvik.system.VMRuntime; 20import libcore.util.EmptyArray; 21 22import java.lang.reflect.Array; 23 24/** 25 * ArrayUtils contains some methods that you can call to find out 26 * the most efficient increments by which to grow arrays. 27 */ 28public class ArrayUtils 29{ 30 private static final int CACHE_SIZE = 73; 31 private static Object[] sCache = new Object[CACHE_SIZE]; 32 33 private ArrayUtils() { /* cannot be instantiated */ } 34 35 public static byte[] newUnpaddedByteArray(int minLen) { 36 return (byte[])VMRuntime.getRuntime().newUnpaddedArray(byte.class, minLen); 37 } 38 39 public static char[] newUnpaddedCharArray(int minLen) { 40 return (char[])VMRuntime.getRuntime().newUnpaddedArray(char.class, minLen); 41 } 42 43 public static int[] newUnpaddedIntArray(int minLen) { 44 return (int[])VMRuntime.getRuntime().newUnpaddedArray(int.class, minLen); 45 } 46 47 public static boolean[] newUnpaddedBooleanArray(int minLen) { 48 return (boolean[])VMRuntime.getRuntime().newUnpaddedArray(boolean.class, minLen); 49 } 50 51 public static long[] newUnpaddedLongArray(int minLen) { 52 return (long[])VMRuntime.getRuntime().newUnpaddedArray(long.class, minLen); 53 } 54 55 public static float[] newUnpaddedFloatArray(int minLen) { 56 return (float[])VMRuntime.getRuntime().newUnpaddedArray(float.class, minLen); 57 } 58 59 public static Object[] newUnpaddedObjectArray(int minLen) { 60 return (Object[])VMRuntime.getRuntime().newUnpaddedArray(Object.class, minLen); 61 } 62 63 @SuppressWarnings("unchecked") 64 public static <T> T[] newUnpaddedArray(Class<T> clazz, int minLen) { 65 return (T[])VMRuntime.getRuntime().newUnpaddedArray(clazz, minLen); 66 } 67 68 /** 69 * Checks if the beginnings of two byte arrays are equal. 70 * 71 * @param array1 the first byte array 72 * @param array2 the second byte array 73 * @param length the number of bytes to check 74 * @return true if they're equal, false otherwise 75 */ 76 public static boolean equals(byte[] array1, byte[] array2, int length) { 77 if (length < 0) { 78 throw new IllegalArgumentException(); 79 } 80 81 if (array1 == array2) { 82 return true; 83 } 84 if (array1 == null || array2 == null || array1.length < length || array2.length < length) { 85 return false; 86 } 87 for (int i = 0; i < length; i++) { 88 if (array1[i] != array2[i]) { 89 return false; 90 } 91 } 92 return true; 93 } 94 95 /** 96 * Returns an empty array of the specified type. The intent is that 97 * it will return the same empty array every time to avoid reallocation, 98 * although this is not guaranteed. 99 */ 100 @SuppressWarnings("unchecked") 101 public static <T> T[] emptyArray(Class<T> kind) { 102 if (kind == Object.class) { 103 return (T[]) EmptyArray.OBJECT; 104 } 105 106 int bucket = ((System.identityHashCode(kind) / 8) & 0x7FFFFFFF) % CACHE_SIZE; 107 Object cache = sCache[bucket]; 108 109 if (cache == null || cache.getClass().getComponentType() != kind) { 110 cache = Array.newInstance(kind, 0); 111 sCache[bucket] = cache; 112 113 // Log.e("cache", "new empty " + kind.getName() + " at " + bucket); 114 } 115 116 return (T[]) cache; 117 } 118 119 /** 120 * Checks if given array is null or has zero elements. 121 */ 122 public static <T> boolean isEmpty(T[] array) { 123 return array == null || array.length == 0; 124 } 125 126 /** 127 * Checks that value is present as at least one of the elements of the array. 128 * @param array the array to check in 129 * @param value the value to check for 130 * @return true if the value is present in the array 131 */ 132 public static <T> boolean contains(T[] array, T value) { 133 return indexOf(array, value) != -1; 134 } 135 136 /** 137 * Return first index of {@code value} in {@code array}, or {@code -1} if 138 * not found. 139 */ 140 public static <T> int indexOf(T[] array, T value) { 141 if (array == null) return -1; 142 for (int i = 0; i < array.length; i++) { 143 if (array[i] == null) { 144 if (value == null) return i; 145 } else { 146 if (value != null && array[i].equals(value)) return i; 147 } 148 } 149 return -1; 150 } 151 152 /** 153 * Test if all {@code check} items are contained in {@code array}. 154 */ 155 public static <T> boolean containsAll(T[] array, T[] check) { 156 for (T checkItem : check) { 157 if (!contains(array, checkItem)) { 158 return false; 159 } 160 } 161 return true; 162 } 163 164 public static boolean contains(int[] array, int value) { 165 if (array == null) return false; 166 for (int element : array) { 167 if (element == value) { 168 return true; 169 } 170 } 171 return false; 172 } 173 174 public static boolean contains(long[] array, long value) { 175 if (array == null) return false; 176 for (long element : array) { 177 if (element == value) { 178 return true; 179 } 180 } 181 return false; 182 } 183 184 public static long total(long[] array) { 185 long total = 0; 186 for (long value : array) { 187 total += value; 188 } 189 return total; 190 } 191 192 /** 193 * Appends an element to a copy of the array and returns the copy. 194 * @param array The original array, or null to represent an empty array. 195 * @param element The element to add. 196 * @return A new array that contains all of the elements of the original array 197 * with the specified element added at the end. 198 */ 199 @SuppressWarnings("unchecked") 200 public static <T> T[] appendElement(Class<T> kind, T[] array, T element) { 201 final T[] result; 202 final int end; 203 if (array != null) { 204 end = array.length; 205 result = (T[])Array.newInstance(kind, end + 1); 206 System.arraycopy(array, 0, result, 0, end); 207 } else { 208 end = 0; 209 result = (T[])Array.newInstance(kind, 1); 210 } 211 result[end] = element; 212 return result; 213 } 214 215 /** 216 * Removes an element from a copy of the array and returns the copy. 217 * If the element is not present, then the original array is returned unmodified. 218 * @param array The original array, or null to represent an empty array. 219 * @param element The element to remove. 220 * @return A new array that contains all of the elements of the original array 221 * except the first copy of the specified element removed. If the specified element 222 * was not present, then returns the original array. Returns null if the result 223 * would be an empty array. 224 */ 225 @SuppressWarnings("unchecked") 226 public static <T> T[] removeElement(Class<T> kind, T[] array, T element) { 227 if (array != null) { 228 final int length = array.length; 229 for (int i = 0; i < length; i++) { 230 if (array[i] == element) { 231 if (length == 1) { 232 return null; 233 } 234 T[] result = (T[])Array.newInstance(kind, length - 1); 235 System.arraycopy(array, 0, result, 0, i); 236 System.arraycopy(array, i + 1, result, i, length - i - 1); 237 return result; 238 } 239 } 240 } 241 return array; 242 } 243 244 /** 245 * Appends a new value to a copy of the array and returns the copy. If 246 * the value is already present, the original array is returned 247 * @param cur The original array, or null to represent an empty array. 248 * @param val The value to add. 249 * @return A new array that contains all of the values of the original array 250 * with the new value added, or the original array. 251 */ 252 public static int[] appendInt(int[] cur, int val) { 253 if (cur == null) { 254 return new int[] { val }; 255 } 256 final int N = cur.length; 257 for (int i = 0; i < N; i++) { 258 if (cur[i] == val) { 259 return cur; 260 } 261 } 262 int[] ret = new int[N + 1]; 263 System.arraycopy(cur, 0, ret, 0, N); 264 ret[N] = val; 265 return ret; 266 } 267 268 public static int[] removeInt(int[] cur, int val) { 269 if (cur == null) { 270 return null; 271 } 272 final int N = cur.length; 273 for (int i = 0; i < N; i++) { 274 if (cur[i] == val) { 275 int[] ret = new int[N - 1]; 276 if (i > 0) { 277 System.arraycopy(cur, 0, ret, 0, i); 278 } 279 if (i < (N - 1)) { 280 System.arraycopy(cur, i + 1, ret, i, N - i - 1); 281 } 282 return ret; 283 } 284 } 285 return cur; 286 } 287 288 /** 289 * Appends a new value to a copy of the array and returns the copy. If 290 * the value is already present, the original array is returned 291 * @param cur The original array, or null to represent an empty array. 292 * @param val The value to add. 293 * @return A new array that contains all of the values of the original array 294 * with the new value added, or the original array. 295 */ 296 public static long[] appendLong(long[] cur, long val) { 297 if (cur == null) { 298 return new long[] { val }; 299 } 300 final int N = cur.length; 301 for (int i = 0; i < N; i++) { 302 if (cur[i] == val) { 303 return cur; 304 } 305 } 306 long[] ret = new long[N + 1]; 307 System.arraycopy(cur, 0, ret, 0, N); 308 ret[N] = val; 309 return ret; 310 } 311 312 public static long[] removeLong(long[] cur, long val) { 313 if (cur == null) { 314 return null; 315 } 316 final int N = cur.length; 317 for (int i = 0; i < N; i++) { 318 if (cur[i] == val) { 319 long[] ret = new long[N - 1]; 320 if (i > 0) { 321 System.arraycopy(cur, 0, ret, 0, i); 322 } 323 if (i < (N - 1)) { 324 System.arraycopy(cur, i + 1, ret, i, N - i - 1); 325 } 326 return ret; 327 } 328 } 329 return cur; 330 } 331 332 public static long[] cloneOrNull(long[] array) { 333 return (array != null) ? array.clone() : null; 334 } 335} 336