The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| 17 | package com.android.internal.util; |
| 18 | |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Jeff Sharkey | da96e13 | 2014-07-15 14:54:09 -0700 | [diff] [blame] | 21 | import android.util.ArraySet; |
| 22 | |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 23 | import dalvik.system.VMRuntime; |
Jeff Sharkey | da96e13 | 2014-07-15 14:54:09 -0700 | [diff] [blame] | 24 | |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 25 | import libcore.util.EmptyArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | |
Jeff Sharkey | b0c363b2 | 2018-12-15 11:53:03 -0700 | [diff] [blame] | 27 | import java.io.File; |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 28 | import java.lang.reflect.Array; |
Jeff Sharkey | da96e13 | 2014-07-15 14:54:09 -0700 | [diff] [blame] | 29 | import java.util.ArrayList; |
Jeff Sharkey | 2bd31db | 2016-01-09 16:58:14 -0700 | [diff] [blame] | 30 | import java.util.Arrays; |
Jeff Sharkey | 4288419 | 2016-04-09 16:12:01 -0600 | [diff] [blame] | 31 | import java.util.Collection; |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 32 | import java.util.Collections; |
Jeff Sharkey | 3e19589 | 2016-03-05 19:48:59 -0700 | [diff] [blame] | 33 | import java.util.List; |
Makoto Onuki | 3aaed29 | 2017-11-15 16:31:24 -0800 | [diff] [blame] | 34 | import java.util.Map; |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 35 | import java.util.Objects; |
Eugene Susla | 31c9cd1 | 2018-12-03 15:41:31 -0800 | [diff] [blame] | 36 | import java.util.function.IntFunction; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * ArrayUtils contains some methods that you can call to find out |
| 40 | * the most efficient increments by which to grow arrays. |
| 41 | */ |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 42 | public class ArrayUtils { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | private static final int CACHE_SIZE = 73; |
| 44 | private static Object[] sCache = new Object[CACHE_SIZE]; |
| 45 | |
Jeff Sharkey | b0c363b2 | 2018-12-15 11:53:03 -0700 | [diff] [blame] | 46 | public static final File[] EMPTY_FILE = new File[0]; |
| 47 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | private ArrayUtils() { /* cannot be instantiated */ } |
| 49 | |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 50 | public static byte[] newUnpaddedByteArray(int minLen) { |
| 51 | return (byte[])VMRuntime.getRuntime().newUnpaddedArray(byte.class, minLen); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 54 | public static char[] newUnpaddedCharArray(int minLen) { |
| 55 | return (char[])VMRuntime.getRuntime().newUnpaddedArray(char.class, minLen); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 58 | public static int[] newUnpaddedIntArray(int minLen) { |
| 59 | return (int[])VMRuntime.getRuntime().newUnpaddedArray(int.class, minLen); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 62 | public static boolean[] newUnpaddedBooleanArray(int minLen) { |
| 63 | return (boolean[])VMRuntime.getRuntime().newUnpaddedArray(boolean.class, minLen); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 66 | public static long[] newUnpaddedLongArray(int minLen) { |
| 67 | return (long[])VMRuntime.getRuntime().newUnpaddedArray(long.class, minLen); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 70 | public static float[] newUnpaddedFloatArray(int minLen) { |
| 71 | return (float[])VMRuntime.getRuntime().newUnpaddedArray(float.class, minLen); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 74 | public static Object[] newUnpaddedObjectArray(int minLen) { |
| 75 | return (Object[])VMRuntime.getRuntime().newUnpaddedArray(Object.class, minLen); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 78 | @SuppressWarnings("unchecked") |
| 79 | public static <T> T[] newUnpaddedArray(Class<T> clazz, int minLen) { |
| 80 | return (T[])VMRuntime.getRuntime().newUnpaddedArray(clazz, minLen); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Checks if the beginnings of two byte arrays are equal. |
| 85 | * |
| 86 | * @param array1 the first byte array |
| 87 | * @param array2 the second byte array |
| 88 | * @param length the number of bytes to check |
| 89 | * @return true if they're equal, false otherwise |
| 90 | */ |
| 91 | public static boolean equals(byte[] array1, byte[] array2, int length) { |
Christopher Tate | fc05434 | 2013-03-22 15:01:13 -0700 | [diff] [blame] | 92 | if (length < 0) { |
| 93 | throw new IllegalArgumentException(); |
| 94 | } |
| 95 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | if (array1 == array2) { |
| 97 | return true; |
| 98 | } |
| 99 | if (array1 == null || array2 == null || array1.length < length || array2.length < length) { |
| 100 | return false; |
| 101 | } |
| 102 | for (int i = 0; i < length; i++) { |
| 103 | if (array1[i] != array2[i]) { |
| 104 | return false; |
| 105 | } |
| 106 | } |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Returns an empty array of the specified type. The intent is that |
| 112 | * it will return the same empty array every time to avoid reallocation, |
| 113 | * although this is not guaranteed. |
| 114 | */ |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 115 | @SuppressWarnings("unchecked") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | public static <T> T[] emptyArray(Class<T> kind) { |
| 117 | if (kind == Object.class) { |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 118 | return (T[]) EmptyArray.OBJECT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Mathieu Chartier | b4e5061 | 2014-09-10 12:56:21 -0700 | [diff] [blame] | 121 | int bucket = (kind.hashCode() & 0x7FFFFFFF) % CACHE_SIZE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | Object cache = sCache[bucket]; |
| 123 | |
| 124 | if (cache == null || cache.getClass().getComponentType() != kind) { |
| 125 | cache = Array.newInstance(kind, 0); |
| 126 | sCache[bucket] = cache; |
| 127 | |
| 128 | // Log.e("cache", "new empty " + kind.getName() + " at " + bucket); |
| 129 | } |
| 130 | |
| 131 | return (T[]) cache; |
| 132 | } |
| 133 | |
| 134 | /** |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 135 | * Checks if given array is null or has zero elements. |
| 136 | */ |
Jeff Sharkey | 4288419 | 2016-04-09 16:12:01 -0600 | [diff] [blame] | 137 | public static boolean isEmpty(@Nullable Collection<?> array) { |
Jeff Sharkey | 3e19589 | 2016-03-05 19:48:59 -0700 | [diff] [blame] | 138 | return array == null || array.isEmpty(); |
| 139 | } |
| 140 | |
| 141 | /** |
Makoto Onuki | 3aaed29 | 2017-11-15 16:31:24 -0800 | [diff] [blame] | 142 | * Checks if given map is null or has zero elements. |
| 143 | */ |
| 144 | public static boolean isEmpty(@Nullable Map<?, ?> map) { |
| 145 | return map == null || map.isEmpty(); |
| 146 | } |
| 147 | |
| 148 | /** |
Jeff Sharkey | 3e19589 | 2016-03-05 19:48:59 -0700 | [diff] [blame] | 149 | * Checks if given array is null or has zero elements. |
| 150 | */ |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 151 | public static <T> boolean isEmpty(@Nullable T[] array) { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 152 | return array == null || array.length == 0; |
| 153 | } |
| 154 | |
| 155 | /** |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 156 | * Checks if given array is null or has zero elements. |
| 157 | */ |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 158 | public static boolean isEmpty(@Nullable int[] array) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 159 | return array == null || array.length == 0; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Checks if given array is null or has zero elements. |
| 164 | */ |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 165 | public static boolean isEmpty(@Nullable long[] array) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 166 | return array == null || array.length == 0; |
| 167 | } |
| 168 | |
| 169 | /** |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 170 | * Checks if given array is null or has zero elements. |
| 171 | */ |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 172 | public static boolean isEmpty(@Nullable byte[] array) { |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 173 | return array == null || array.length == 0; |
| 174 | } |
| 175 | |
| 176 | /** |
Svetoslav Ganov | a8bbd76 | 2016-05-13 17:08:16 -0700 | [diff] [blame] | 177 | * Checks if given array is null or has zero elements. |
| 178 | */ |
| 179 | public static boolean isEmpty(@Nullable boolean[] array) { |
| 180 | return array == null || array.length == 0; |
| 181 | } |
| 182 | |
| 183 | /** |
Eugene Susla | aa43a4b | 2017-04-21 14:12:00 -0700 | [diff] [blame] | 184 | * Length of the given array or 0 if it's null. |
| 185 | */ |
| 186 | public static int size(@Nullable Object[] array) { |
| 187 | return array == null ? 0 : array.length; |
| 188 | } |
| 189 | |
| 190 | /** |
Makoto Onuki | 5d93b83 | 2018-01-10 16:12:39 -0800 | [diff] [blame] | 191 | * Length of the given collection or 0 if it's null. |
| 192 | */ |
| 193 | public static int size(@Nullable Collection<?> collection) { |
| 194 | return collection == null ? 0 : collection.size(); |
| 195 | } |
| 196 | |
| 197 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | * Checks that value is present as at least one of the elements of the array. |
| 199 | * @param array the array to check in |
| 200 | * @param value the value to check for |
| 201 | * @return true if the value is present in the array |
| 202 | */ |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 203 | public static <T> boolean contains(@Nullable T[] array, T value) { |
Jeff Sharkey | 94c91dc | 2013-03-06 16:25:50 -0800 | [diff] [blame] | 204 | return indexOf(array, value) != -1; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Return first index of {@code value} in {@code array}, or {@code -1} if |
| 209 | * not found. |
| 210 | */ |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 211 | public static <T> int indexOf(@Nullable T[] array, T value) { |
Jeff Sharkey | 57dcf5b | 2014-06-18 17:46:05 -0700 | [diff] [blame] | 212 | if (array == null) return -1; |
Jeff Sharkey | 94c91dc | 2013-03-06 16:25:50 -0800 | [diff] [blame] | 213 | for (int i = 0; i < array.length; i++) { |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 214 | if (Objects.equals(array[i], value)) return i; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | } |
Jeff Sharkey | 94c91dc | 2013-03-06 16:25:50 -0800 | [diff] [blame] | 216 | return -1; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Test if all {@code check} items are contained in {@code array}. |
| 221 | */ |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 222 | public static <T> boolean containsAll(@Nullable T[] array, T[] check) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 223 | if (check == null) return true; |
Jeff Sharkey | 94c91dc | 2013-03-06 16:25:50 -0800 | [diff] [blame] | 224 | for (T checkItem : check) { |
| 225 | if (!contains(array, checkItem)) { |
| 226 | return false; |
| 227 | } |
| 228 | } |
| 229 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | } |
Jeff Sharkey | 630a171 | 2011-09-26 10:47:10 -0700 | [diff] [blame] | 231 | |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 232 | /** |
| 233 | * Test if any {@code check} items are contained in {@code array}. |
| 234 | */ |
| 235 | public static <T> boolean containsAny(@Nullable T[] array, T[] check) { |
| 236 | if (check == null) return false; |
| 237 | for (T checkItem : check) { |
| 238 | if (contains(array, checkItem)) { |
| 239 | return true; |
| 240 | } |
| 241 | } |
| 242 | return false; |
| 243 | } |
| 244 | |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 245 | public static boolean contains(@Nullable int[] array, int value) { |
Jeff Sharkey | 57dcf5b | 2014-06-18 17:46:05 -0700 | [diff] [blame] | 246 | if (array == null) return false; |
Jeff Sharkey | 630a171 | 2011-09-26 10:47:10 -0700 | [diff] [blame] | 247 | for (int element : array) { |
| 248 | if (element == value) { |
| 249 | return true; |
| 250 | } |
| 251 | } |
| 252 | return false; |
| 253 | } |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 254 | |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 255 | public static boolean contains(@Nullable long[] array, long value) { |
Jeff Sharkey | 57dcf5b | 2014-06-18 17:46:05 -0700 | [diff] [blame] | 256 | if (array == null) return false; |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 257 | for (long element : array) { |
| 258 | if (element == value) { |
| 259 | return true; |
| 260 | } |
| 261 | } |
| 262 | return false; |
| 263 | } |
| 264 | |
Roozbeh Pournader | 889c650 | 2017-02-15 18:19:09 -0800 | [diff] [blame] | 265 | public static boolean contains(@Nullable char[] array, char value) { |
| 266 | if (array == null) return false; |
| 267 | for (char element : array) { |
| 268 | if (element == value) { |
| 269 | return true; |
| 270 | } |
| 271 | } |
| 272 | return false; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Test if all {@code check} items are contained in {@code array}. |
| 277 | */ |
| 278 | public static <T> boolean containsAll(@Nullable char[] array, char[] check) { |
| 279 | if (check == null) return true; |
| 280 | for (char checkItem : check) { |
| 281 | if (!contains(array, checkItem)) { |
| 282 | return false; |
| 283 | } |
| 284 | } |
| 285 | return true; |
| 286 | } |
| 287 | |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 288 | public static long total(@Nullable long[] array) { |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 289 | long total = 0; |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 290 | if (array != null) { |
| 291 | for (long value : array) { |
| 292 | total += value; |
| 293 | } |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 294 | } |
| 295 | return total; |
| 296 | } |
| 297 | |
Chong Zhang | 167bbfac | 2016-03-31 09:44:34 -0700 | [diff] [blame] | 298 | public static int[] convertToIntArray(List<Integer> list) { |
| 299 | int[] array = new int[list.size()]; |
| 300 | for (int i = 0; i < list.size(); i++) { |
| 301 | array[i] = list.get(i); |
| 302 | } |
| 303 | return array; |
| 304 | } |
| 305 | |
Phil Weaver | b9f0612 | 2017-11-30 10:48:17 -0800 | [diff] [blame] | 306 | public static @Nullable long[] convertToLongArray(@Nullable int[] intArray) { |
| 307 | if (intArray == null) return null; |
| 308 | long[] array = new long[intArray.length]; |
| 309 | for (int i = 0; i < intArray.length; i++) { |
| 310 | array[i] = (long) intArray[i]; |
| 311 | } |
| 312 | return array; |
| 313 | } |
| 314 | |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 315 | @SuppressWarnings("unchecked") |
Jeff Sharkey | b89df9e | 2018-07-26 14:36:59 -0600 | [diff] [blame] | 316 | public static @NonNull <T> T[] concatElements(Class<T> kind, @Nullable T[] a, @Nullable T[] b) { |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 317 | final int an = (a != null) ? a.length : 0; |
| 318 | final int bn = (b != null) ? b.length : 0; |
| 319 | if (an == 0 && bn == 0) { |
| 320 | if (kind == String.class) { |
| 321 | return (T[]) EmptyArray.STRING; |
| 322 | } else if (kind == Object.class) { |
| 323 | return (T[]) EmptyArray.OBJECT; |
| 324 | } |
| 325 | } |
| 326 | final T[] res = (T[]) Array.newInstance(kind, an + bn); |
| 327 | if (an > 0) System.arraycopy(a, 0, res, 0, an); |
| 328 | if (bn > 0) System.arraycopy(b, 0, res, an, bn); |
| 329 | return res; |
| 330 | } |
| 331 | |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 332 | /** |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 333 | * Adds value to given array if not already present, providing set-like |
| 334 | * behavior. |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 335 | */ |
| 336 | @SuppressWarnings("unchecked") |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 337 | public static @NonNull <T> T[] appendElement(Class<T> kind, @Nullable T[] array, T element) { |
Svet Ganov | 8a10ef5 | 2017-02-16 20:58:09 -0800 | [diff] [blame] | 338 | return appendElement(kind, array, element, false); |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Adds value to given array. |
| 343 | */ |
| 344 | @SuppressWarnings("unchecked") |
| 345 | public static @NonNull <T> T[] appendElement(Class<T> kind, @Nullable T[] array, T element, |
| 346 | boolean allowDuplicates) { |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 347 | final T[] result; |
| 348 | final int end; |
| 349 | if (array != null) { |
Svet Ganov | 8a10ef5 | 2017-02-16 20:58:09 -0800 | [diff] [blame] | 350 | if (!allowDuplicates && contains(array, element)) return array; |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 351 | end = array.length; |
| 352 | result = (T[])Array.newInstance(kind, end + 1); |
| 353 | System.arraycopy(array, 0, result, 0, end); |
| 354 | } else { |
| 355 | end = 0; |
| 356 | result = (T[])Array.newInstance(kind, 1); |
| 357 | } |
| 358 | result[end] = element; |
| 359 | return result; |
| 360 | } |
| 361 | |
| 362 | /** |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 363 | * Removes value from given array if present, providing set-like behavior. |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 364 | */ |
| 365 | @SuppressWarnings("unchecked") |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 366 | public static @Nullable <T> T[] removeElement(Class<T> kind, @Nullable T[] array, T element) { |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 367 | if (array != null) { |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 368 | if (!contains(array, element)) return array; |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 369 | final int length = array.length; |
| 370 | for (int i = 0; i < length; i++) { |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 371 | if (Objects.equals(array[i], element)) { |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 372 | if (length == 1) { |
| 373 | return null; |
| 374 | } |
| 375 | T[] result = (T[])Array.newInstance(kind, length - 1); |
| 376 | System.arraycopy(array, 0, result, 0, i); |
| 377 | System.arraycopy(array, i + 1, result, i, length - i - 1); |
| 378 | return result; |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | return array; |
| 383 | } |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 384 | |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 385 | /** |
Svet Ganov | 8a10ef5 | 2017-02-16 20:58:09 -0800 | [diff] [blame] | 386 | * Adds value to given array. |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 387 | */ |
Svet Ganov | 8a10ef5 | 2017-02-16 20:58:09 -0800 | [diff] [blame] | 388 | public static @NonNull int[] appendInt(@Nullable int[] cur, int val, |
| 389 | boolean allowDuplicates) { |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 390 | if (cur == null) { |
| 391 | return new int[] { val }; |
| 392 | } |
| 393 | final int N = cur.length; |
Svet Ganov | 8a10ef5 | 2017-02-16 20:58:09 -0800 | [diff] [blame] | 394 | if (!allowDuplicates) { |
| 395 | for (int i = 0; i < N; i++) { |
| 396 | if (cur[i] == val) { |
| 397 | return cur; |
| 398 | } |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | int[] ret = new int[N + 1]; |
| 402 | System.arraycopy(cur, 0, ret, 0, N); |
| 403 | ret[N] = val; |
| 404 | return ret; |
| 405 | } |
| 406 | |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 407 | /** |
Svet Ganov | 8a10ef5 | 2017-02-16 20:58:09 -0800 | [diff] [blame] | 408 | * Adds value to given array if not already present, providing set-like |
| 409 | * behavior. |
| 410 | */ |
| 411 | public static @NonNull int[] appendInt(@Nullable int[] cur, int val) { |
| 412 | return appendInt(cur, val, false); |
| 413 | } |
| 414 | |
| 415 | /** |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 416 | * Removes value from given array if present, providing set-like behavior. |
| 417 | */ |
| 418 | public static @Nullable int[] removeInt(@Nullable int[] cur, int val) { |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 419 | if (cur == null) { |
| 420 | return null; |
| 421 | } |
| 422 | final int N = cur.length; |
| 423 | for (int i = 0; i < N; i++) { |
| 424 | if (cur[i] == val) { |
| 425 | int[] ret = new int[N - 1]; |
| 426 | if (i > 0) { |
| 427 | System.arraycopy(cur, 0, ret, 0, i); |
| 428 | } |
| 429 | if (i < (N - 1)) { |
| 430 | System.arraycopy(cur, i + 1, ret, i, N - i - 1); |
| 431 | } |
| 432 | return ret; |
| 433 | } |
| 434 | } |
| 435 | return cur; |
| 436 | } |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 437 | |
| 438 | /** |
Svet Ganov | 52153f4 | 2015-08-11 08:59:12 -0700 | [diff] [blame] | 439 | * Removes value from given array if present, providing set-like behavior. |
| 440 | */ |
| 441 | public static @Nullable String[] removeString(@Nullable String[] cur, String val) { |
| 442 | if (cur == null) { |
| 443 | return null; |
| 444 | } |
| 445 | final int N = cur.length; |
| 446 | for (int i = 0; i < N; i++) { |
| 447 | if (Objects.equals(cur[i], val)) { |
| 448 | String[] ret = new String[N - 1]; |
| 449 | if (i > 0) { |
| 450 | System.arraycopy(cur, 0, ret, 0, i); |
| 451 | } |
| 452 | if (i < (N - 1)) { |
| 453 | System.arraycopy(cur, i + 1, ret, i, N - i - 1); |
| 454 | } |
| 455 | return ret; |
| 456 | } |
| 457 | } |
| 458 | return cur; |
| 459 | } |
| 460 | |
| 461 | /** |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 462 | * Adds value to given array if not already present, providing set-like |
| 463 | * behavior. |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 464 | */ |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 465 | public static @NonNull long[] appendLong(@Nullable long[] cur, long val, |
| 466 | boolean allowDuplicates) { |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 467 | if (cur == null) { |
| 468 | return new long[] { val }; |
| 469 | } |
| 470 | final int N = cur.length; |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 471 | if (!allowDuplicates) { |
| 472 | for (int i = 0; i < N; i++) { |
| 473 | if (cur[i] == val) { |
| 474 | return cur; |
| 475 | } |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | long[] ret = new long[N + 1]; |
| 479 | System.arraycopy(cur, 0, ret, 0, N); |
| 480 | ret[N] = val; |
| 481 | return ret; |
| 482 | } |
| 483 | |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 484 | /** |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 485 | * Adds value to given array if not already present, providing set-like |
| 486 | * behavior. |
| 487 | */ |
| 488 | public static @NonNull long[] appendLong(@Nullable long[] cur, long val) { |
| 489 | return appendLong(cur, val, false); |
| 490 | } |
| 491 | |
| 492 | /** |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 493 | * Removes value from given array if present, providing set-like behavior. |
| 494 | */ |
| 495 | public static @Nullable long[] removeLong(@Nullable long[] cur, long val) { |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 496 | if (cur == null) { |
| 497 | return null; |
| 498 | } |
| 499 | final int N = cur.length; |
| 500 | for (int i = 0; i < N; i++) { |
| 501 | if (cur[i] == val) { |
| 502 | long[] ret = new long[N - 1]; |
| 503 | if (i > 0) { |
| 504 | System.arraycopy(cur, 0, ret, 0, i); |
| 505 | } |
| 506 | if (i < (N - 1)) { |
| 507 | System.arraycopy(cur, i + 1, ret, i, N - i - 1); |
| 508 | } |
| 509 | return ret; |
| 510 | } |
| 511 | } |
| 512 | return cur; |
| 513 | } |
Jeff Sharkey | 57dcf5b | 2014-06-18 17:46:05 -0700 | [diff] [blame] | 514 | |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 515 | public static @Nullable long[] cloneOrNull(@Nullable long[] array) { |
Jeff Sharkey | 57dcf5b | 2014-06-18 17:46:05 -0700 | [diff] [blame] | 516 | return (array != null) ? array.clone() : null; |
| 517 | } |
Jeff Sharkey | da96e13 | 2014-07-15 14:54:09 -0700 | [diff] [blame] | 518 | |
Jeff Sharkey | 2bd31db | 2016-01-09 16:58:14 -0700 | [diff] [blame] | 519 | public static @Nullable <T> ArraySet<T> cloneOrNull(@Nullable ArraySet<T> array) { |
| 520 | return (array != null) ? new ArraySet<T>(array) : null; |
| 521 | } |
| 522 | |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 523 | public static @NonNull <T> ArraySet<T> add(@Nullable ArraySet<T> cur, T val) { |
Jeff Sharkey | da96e13 | 2014-07-15 14:54:09 -0700 | [diff] [blame] | 524 | if (cur == null) { |
| 525 | cur = new ArraySet<>(); |
| 526 | } |
| 527 | cur.add(val); |
| 528 | return cur; |
| 529 | } |
| 530 | |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 531 | public static @Nullable <T> ArraySet<T> remove(@Nullable ArraySet<T> cur, T val) { |
Jeff Sharkey | da96e13 | 2014-07-15 14:54:09 -0700 | [diff] [blame] | 532 | if (cur == null) { |
| 533 | return null; |
| 534 | } |
| 535 | cur.remove(val); |
| 536 | if (cur.isEmpty()) { |
| 537 | return null; |
| 538 | } else { |
| 539 | return cur; |
| 540 | } |
| 541 | } |
| 542 | |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 543 | public static @NonNull <T> ArrayList<T> add(@Nullable ArrayList<T> cur, T val) { |
Jeff Sharkey | da96e13 | 2014-07-15 14:54:09 -0700 | [diff] [blame] | 544 | if (cur == null) { |
| 545 | cur = new ArrayList<>(); |
| 546 | } |
| 547 | cur.add(val); |
| 548 | return cur; |
| 549 | } |
| 550 | |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 551 | public static @Nullable <T> ArrayList<T> remove(@Nullable ArrayList<T> cur, T val) { |
Jeff Sharkey | da96e13 | 2014-07-15 14:54:09 -0700 | [diff] [blame] | 552 | if (cur == null) { |
| 553 | return null; |
| 554 | } |
| 555 | cur.remove(val); |
| 556 | if (cur.isEmpty()) { |
| 557 | return null; |
| 558 | } else { |
| 559 | return cur; |
| 560 | } |
| 561 | } |
| 562 | |
Jeff Sharkey | 4288419 | 2016-04-09 16:12:01 -0600 | [diff] [blame] | 563 | public static <T> boolean contains(@Nullable Collection<T> cur, T val) { |
Jeff Sharkey | da96e13 | 2014-07-15 14:54:09 -0700 | [diff] [blame] | 564 | return (cur != null) ? cur.contains(val) : false; |
| 565 | } |
Adam Lesinski | 72478f0 | 2015-06-17 15:39:43 -0700 | [diff] [blame] | 566 | |
Jeff Sharkey | 2bd31db | 2016-01-09 16:58:14 -0700 | [diff] [blame] | 567 | public static @Nullable <T> T[] trimToSize(@Nullable T[] array, int size) { |
| 568 | if (array == null || size == 0) { |
| 569 | return null; |
| 570 | } else if (array.length == size) { |
| 571 | return array; |
| 572 | } else { |
| 573 | return Arrays.copyOf(array, size); |
| 574 | } |
| 575 | } |
| 576 | |
Adam Lesinski | 72478f0 | 2015-06-17 15:39:43 -0700 | [diff] [blame] | 577 | /** |
| 578 | * Returns true if the two ArrayLists are equal with respect to the objects they contain. |
| 579 | * The objects must be in the same order and be reference equal (== not .equals()). |
| 580 | */ |
| 581 | public static <T> boolean referenceEquals(ArrayList<T> a, ArrayList<T> b) { |
| 582 | if (a == b) { |
| 583 | return true; |
| 584 | } |
| 585 | |
| 586 | final int sizeA = a.size(); |
| 587 | final int sizeB = b.size(); |
| 588 | if (a == null || b == null || sizeA != sizeB) { |
| 589 | return false; |
| 590 | } |
| 591 | |
| 592 | boolean diff = false; |
| 593 | for (int i = 0; i < sizeA && !diff; i++) { |
| 594 | diff |= a.get(i) != b.get(i); |
| 595 | } |
| 596 | return !diff; |
| 597 | } |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 598 | |
| 599 | /** |
| 600 | * Removes elements that match the predicate in an efficient way that alters the order of |
| 601 | * elements in the collection. This should only be used if order is not important. |
| 602 | * @param collection The ArrayList from which to remove elements. |
| 603 | * @param predicate The predicate that each element is tested against. |
| 604 | * @return the number of elements removed. |
| 605 | */ |
| 606 | public static <T> int unstableRemoveIf(@Nullable ArrayList<T> collection, |
| 607 | @NonNull java.util.function.Predicate<T> predicate) { |
| 608 | if (collection == null) { |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | final int size = collection.size(); |
| 613 | int leftIdx = 0; |
| 614 | int rightIdx = size - 1; |
| 615 | while (leftIdx <= rightIdx) { |
| 616 | // Find the next element to remove moving left to right. |
| 617 | while (leftIdx < size && !predicate.test(collection.get(leftIdx))) { |
| 618 | leftIdx++; |
| 619 | } |
| 620 | |
| 621 | // Find the next element to keep moving right to left. |
| 622 | while (rightIdx > leftIdx && predicate.test(collection.get(rightIdx))) { |
| 623 | rightIdx--; |
| 624 | } |
| 625 | |
| 626 | if (leftIdx >= rightIdx) { |
| 627 | // Done. |
| 628 | break; |
| 629 | } |
| 630 | |
| 631 | Collections.swap(collection, leftIdx, rightIdx); |
| 632 | leftIdx++; |
| 633 | rightIdx--; |
| 634 | } |
| 635 | |
| 636 | // leftIdx is now at the end. |
| 637 | for (int i = size - 1; i >= leftIdx; i--) { |
| 638 | collection.remove(i); |
| 639 | } |
| 640 | return size - leftIdx; |
| 641 | } |
Jeff Sharkey | 2abd66c | 2017-05-05 15:26:13 -0600 | [diff] [blame] | 642 | |
Jeff Sharkey | 2e47145 | 2018-01-19 18:02:47 +0900 | [diff] [blame] | 643 | public static @NonNull int[] defeatNullable(@Nullable int[] val) { |
| 644 | return (val != null) ? val : EmptyArray.INT; |
| 645 | } |
| 646 | |
Jeff Sharkey | 2abd66c | 2017-05-05 15:26:13 -0600 | [diff] [blame] | 647 | public static @NonNull String[] defeatNullable(@Nullable String[] val) { |
| 648 | return (val != null) ? val : EmptyArray.STRING; |
| 649 | } |
Neil Fuller | 8a1683f | 2018-07-03 18:46:10 +0100 | [diff] [blame] | 650 | |
Jeff Sharkey | b0c363b2 | 2018-12-15 11:53:03 -0700 | [diff] [blame] | 651 | public static @NonNull File[] defeatNullable(@Nullable File[] val) { |
| 652 | return (val != null) ? val : EMPTY_FILE; |
| 653 | } |
| 654 | |
Neil Fuller | 8a1683f | 2018-07-03 18:46:10 +0100 | [diff] [blame] | 655 | /** |
| 656 | * Throws {@link ArrayIndexOutOfBoundsException} if the index is out of bounds. |
| 657 | * |
| 658 | * @param len length of the array. Must be non-negative |
| 659 | * @param index the index to check |
| 660 | * @throws ArrayIndexOutOfBoundsException if the {@code index} is out of bounds of the array |
| 661 | */ |
| 662 | public static void checkBounds(int len, int index) { |
| 663 | if (index < 0 || len <= index) { |
| 664 | throw new ArrayIndexOutOfBoundsException("length=" + len + "; index=" + index); |
| 665 | } |
| 666 | } |
Eugene Susla | 31c9cd1 | 2018-12-03 15:41:31 -0800 | [diff] [blame] | 667 | |
| 668 | /** |
| 669 | * Returns an array with values from {@code val} minus {@code null} values |
| 670 | * |
| 671 | * @param arrayConstructor typically {@code T[]::new} e.g. {@code String[]::new} |
| 672 | */ |
| 673 | public static <T> T[] filterNotNull(T[] val, IntFunction<T[]> arrayConstructor) { |
| 674 | int nullCount = 0; |
| 675 | int size = size(val); |
| 676 | for (int i = 0; i < size; i++) { |
| 677 | if (val[i] == null) { |
| 678 | nullCount++; |
| 679 | } |
| 680 | } |
| 681 | if (nullCount == 0) { |
| 682 | return val; |
| 683 | } |
| 684 | T[] result = arrayConstructor.apply(size - nullCount); |
| 685 | int outIdx = 0; |
| 686 | for (int i = 0; i < size; i++) { |
| 687 | if (val[i] != null) { |
| 688 | result[outIdx++] = val[i]; |
| 689 | } |
| 690 | } |
| 691 | return result; |
| 692 | } |
Eugene Susla | 8758d32 | 2019-01-24 16:31:15 -0800 | [diff] [blame] | 693 | |
Jeff Sharkey | d95d0d2 | 2019-01-31 17:51:32 -0700 | [diff] [blame] | 694 | public static boolean startsWith(byte[] cur, byte[] val) { |
| 695 | if (cur == null || val == null) return false; |
| 696 | if (cur.length < val.length) return false; |
| 697 | for (int i = 0; i < val.length; i++) { |
| 698 | if (cur[i] != val[i]) return false; |
| 699 | } |
| 700 | return true; |
| 701 | } |
| 702 | |
Eugene Susla | 8758d32 | 2019-01-24 16:31:15 -0800 | [diff] [blame] | 703 | /** |
| 704 | * Returns the first element from the array for which |
| 705 | * condition {@code predicate} is true, or null if there is no such element |
| 706 | */ |
| 707 | public static @Nullable <T> T find(@Nullable T[] items, |
| 708 | @NonNull java.util.function.Predicate<T> predicate) { |
| 709 | if (isEmpty(items)) return null; |
| 710 | for (final T item : items) { |
| 711 | if (predicate.test(item)) return item; |
| 712 | } |
| 713 | return null; |
| 714 | } |
Jeff Sharkey | bffd250 | 2019-02-28 16:39:12 -0700 | [diff] [blame] | 715 | |
| 716 | public static String deepToString(Object value) { |
| 717 | if (value != null && value.getClass().isArray()) { |
| 718 | if (value.getClass() == boolean[].class) { |
| 719 | return Arrays.toString((boolean[]) value); |
| 720 | } else if (value.getClass() == byte[].class) { |
| 721 | return Arrays.toString((byte[]) value); |
| 722 | } else if (value.getClass() == char[].class) { |
| 723 | return Arrays.toString((char[]) value); |
| 724 | } else if (value.getClass() == double[].class) { |
| 725 | return Arrays.toString((double[]) value); |
| 726 | } else if (value.getClass() == float[].class) { |
| 727 | return Arrays.toString((float[]) value); |
| 728 | } else if (value.getClass() == int[].class) { |
| 729 | return Arrays.toString((int[]) value); |
| 730 | } else if (value.getClass() == long[].class) { |
| 731 | return Arrays.toString((long[]) value); |
| 732 | } else if (value.getClass() == short[].class) { |
| 733 | return Arrays.toString((short[]) value); |
| 734 | } else { |
| 735 | return Arrays.deepToString((Object[]) value); |
| 736 | } |
| 737 | } else { |
| 738 | return String.valueOf(value); |
| 739 | } |
| 740 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 741 | } |