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 | |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 27 | import java.lang.reflect.Array; |
Jeff Sharkey | da96e13 | 2014-07-15 14:54:09 -0700 | [diff] [blame] | 28 | import java.util.ArrayList; |
Jeff Sharkey | 2bd31db | 2016-01-09 16:58:14 -0700 | [diff] [blame] | 29 | import java.util.Arrays; |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 30 | import java.util.Collections; |
Jeff Sharkey | 3e19589 | 2016-03-05 19:48:59 -0700 | [diff] [blame] | 31 | import java.util.List; |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 32 | import java.util.Objects; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * ArrayUtils contains some methods that you can call to find out |
| 36 | * the most efficient increments by which to grow arrays. |
| 37 | */ |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 38 | public class ArrayUtils { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | private static final int CACHE_SIZE = 73; |
| 40 | private static Object[] sCache = new Object[CACHE_SIZE]; |
| 41 | |
| 42 | private ArrayUtils() { /* cannot be instantiated */ } |
| 43 | |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 44 | public static byte[] newUnpaddedByteArray(int minLen) { |
| 45 | return (byte[])VMRuntime.getRuntime().newUnpaddedArray(byte.class, minLen); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 48 | public static char[] newUnpaddedCharArray(int minLen) { |
| 49 | return (char[])VMRuntime.getRuntime().newUnpaddedArray(char.class, minLen); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 52 | public static int[] newUnpaddedIntArray(int minLen) { |
| 53 | return (int[])VMRuntime.getRuntime().newUnpaddedArray(int.class, minLen); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 56 | public static boolean[] newUnpaddedBooleanArray(int minLen) { |
| 57 | return (boolean[])VMRuntime.getRuntime().newUnpaddedArray(boolean.class, minLen); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 60 | public static long[] newUnpaddedLongArray(int minLen) { |
| 61 | return (long[])VMRuntime.getRuntime().newUnpaddedArray(long.class, minLen); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 64 | public static float[] newUnpaddedFloatArray(int minLen) { |
| 65 | return (float[])VMRuntime.getRuntime().newUnpaddedArray(float.class, minLen); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 68 | public static Object[] newUnpaddedObjectArray(int minLen) { |
| 69 | return (Object[])VMRuntime.getRuntime().newUnpaddedArray(Object.class, minLen); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 72 | @SuppressWarnings("unchecked") |
| 73 | public static <T> T[] newUnpaddedArray(Class<T> clazz, int minLen) { |
| 74 | return (T[])VMRuntime.getRuntime().newUnpaddedArray(clazz, minLen); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Checks if the beginnings of two byte arrays are equal. |
| 79 | * |
| 80 | * @param array1 the first byte array |
| 81 | * @param array2 the second byte array |
| 82 | * @param length the number of bytes to check |
| 83 | * @return true if they're equal, false otherwise |
| 84 | */ |
| 85 | public static boolean equals(byte[] array1, byte[] array2, int length) { |
Christopher Tate | fc05434 | 2013-03-22 15:01:13 -0700 | [diff] [blame] | 86 | if (length < 0) { |
| 87 | throw new IllegalArgumentException(); |
| 88 | } |
| 89 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | if (array1 == array2) { |
| 91 | return true; |
| 92 | } |
| 93 | if (array1 == null || array2 == null || array1.length < length || array2.length < length) { |
| 94 | return false; |
| 95 | } |
| 96 | for (int i = 0; i < length; i++) { |
| 97 | if (array1[i] != array2[i]) { |
| 98 | return false; |
| 99 | } |
| 100 | } |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Returns an empty array of the specified type. The intent is that |
| 106 | * it will return the same empty array every time to avoid reallocation, |
| 107 | * although this is not guaranteed. |
| 108 | */ |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 109 | @SuppressWarnings("unchecked") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | public static <T> T[] emptyArray(Class<T> kind) { |
| 111 | if (kind == Object.class) { |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 112 | return (T[]) EmptyArray.OBJECT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Mathieu Chartier | b4e5061 | 2014-09-10 12:56:21 -0700 | [diff] [blame] | 115 | int bucket = (kind.hashCode() & 0x7FFFFFFF) % CACHE_SIZE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | Object cache = sCache[bucket]; |
| 117 | |
| 118 | if (cache == null || cache.getClass().getComponentType() != kind) { |
| 119 | cache = Array.newInstance(kind, 0); |
| 120 | sCache[bucket] = cache; |
| 121 | |
| 122 | // Log.e("cache", "new empty " + kind.getName() + " at " + bucket); |
| 123 | } |
| 124 | |
| 125 | return (T[]) cache; |
| 126 | } |
| 127 | |
| 128 | /** |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 129 | * Checks if given array is null or has zero elements. |
| 130 | */ |
Jeff Sharkey | 3e19589 | 2016-03-05 19:48:59 -0700 | [diff] [blame] | 131 | public static boolean isEmpty(@Nullable List<?> array) { |
| 132 | return array == null || array.isEmpty(); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Checks if given array is null or has zero elements. |
| 137 | */ |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 138 | public static <T> boolean isEmpty(@Nullable T[] array) { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 139 | return array == null || array.length == 0; |
| 140 | } |
| 141 | |
| 142 | /** |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 143 | * Checks if given array is null or has zero elements. |
| 144 | */ |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 145 | public static boolean isEmpty(@Nullable int[] array) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 146 | return array == null || array.length == 0; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Checks if given array is null or has zero elements. |
| 151 | */ |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 152 | public static boolean isEmpty(@Nullable long[] array) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 153 | return array == null || array.length == 0; |
| 154 | } |
| 155 | |
| 156 | /** |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 157 | * Checks if given array is null or has zero elements. |
| 158 | */ |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 159 | public static boolean isEmpty(@Nullable byte[] array) { |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 160 | return array == null || array.length == 0; |
| 161 | } |
| 162 | |
| 163 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | * Checks that value is present as at least one of the elements of the array. |
| 165 | * @param array the array to check in |
| 166 | * @param value the value to check for |
| 167 | * @return true if the value is present in the array |
| 168 | */ |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 169 | public static <T> boolean contains(@Nullable T[] array, T value) { |
Jeff Sharkey | 94c91dc | 2013-03-06 16:25:50 -0800 | [diff] [blame] | 170 | return indexOf(array, value) != -1; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Return first index of {@code value} in {@code array}, or {@code -1} if |
| 175 | * not found. |
| 176 | */ |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 177 | public static <T> int indexOf(@Nullable T[] array, T value) { |
Jeff Sharkey | 57dcf5b | 2014-06-18 17:46:05 -0700 | [diff] [blame] | 178 | if (array == null) return -1; |
Jeff Sharkey | 94c91dc | 2013-03-06 16:25:50 -0800 | [diff] [blame] | 179 | for (int i = 0; i < array.length; i++) { |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 180 | if (Objects.equals(array[i], value)) return i; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | } |
Jeff Sharkey | 94c91dc | 2013-03-06 16:25:50 -0800 | [diff] [blame] | 182 | return -1; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Test if all {@code check} items are contained in {@code array}. |
| 187 | */ |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 188 | public static <T> boolean containsAll(@Nullable T[] array, T[] check) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 189 | if (check == null) return true; |
Jeff Sharkey | 94c91dc | 2013-03-06 16:25:50 -0800 | [diff] [blame] | 190 | for (T checkItem : check) { |
| 191 | if (!contains(array, checkItem)) { |
| 192 | return false; |
| 193 | } |
| 194 | } |
| 195 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | } |
Jeff Sharkey | 630a171 | 2011-09-26 10:47:10 -0700 | [diff] [blame] | 197 | |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 198 | /** |
| 199 | * Test if any {@code check} items are contained in {@code array}. |
| 200 | */ |
| 201 | public static <T> boolean containsAny(@Nullable T[] array, T[] check) { |
| 202 | if (check == null) return false; |
| 203 | for (T checkItem : check) { |
| 204 | if (contains(array, checkItem)) { |
| 205 | return true; |
| 206 | } |
| 207 | } |
| 208 | return false; |
| 209 | } |
| 210 | |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 211 | public static boolean contains(@Nullable int[] array, int value) { |
Jeff Sharkey | 57dcf5b | 2014-06-18 17:46:05 -0700 | [diff] [blame] | 212 | if (array == null) return false; |
Jeff Sharkey | 630a171 | 2011-09-26 10:47:10 -0700 | [diff] [blame] | 213 | for (int element : array) { |
| 214 | if (element == value) { |
| 215 | return true; |
| 216 | } |
| 217 | } |
| 218 | return false; |
| 219 | } |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 220 | |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 221 | public static boolean contains(@Nullable long[] array, long value) { |
Jeff Sharkey | 57dcf5b | 2014-06-18 17:46:05 -0700 | [diff] [blame] | 222 | if (array == null) return false; |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 223 | for (long element : array) { |
| 224 | if (element == value) { |
| 225 | return true; |
| 226 | } |
| 227 | } |
| 228 | return false; |
| 229 | } |
| 230 | |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 231 | public static long total(@Nullable long[] array) { |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 232 | long total = 0; |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 233 | if (array != null) { |
| 234 | for (long value : array) { |
| 235 | total += value; |
| 236 | } |
Jeff Sharkey | 63abc37 | 2012-01-11 18:38:16 -0800 | [diff] [blame] | 237 | } |
| 238 | return total; |
| 239 | } |
| 240 | |
Chong Zhang | 167bbfac | 2016-03-31 09:44:34 -0700 | [diff] [blame] | 241 | public static int[] convertToIntArray(List<Integer> list) { |
| 242 | int[] array = new int[list.size()]; |
| 243 | for (int i = 0; i < list.size(); i++) { |
| 244 | array[i] = list.get(i); |
| 245 | } |
| 246 | return array; |
| 247 | } |
| 248 | |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 249 | /** |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 250 | * Adds value to given array if not already present, providing set-like |
| 251 | * behavior. |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 252 | */ |
| 253 | @SuppressWarnings("unchecked") |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 254 | public static @NonNull <T> T[] appendElement(Class<T> kind, @Nullable T[] array, T element) { |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 255 | final T[] result; |
| 256 | final int end; |
| 257 | if (array != null) { |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 258 | if (contains(array, element)) return array; |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 259 | end = array.length; |
| 260 | result = (T[])Array.newInstance(kind, end + 1); |
| 261 | System.arraycopy(array, 0, result, 0, end); |
| 262 | } else { |
| 263 | end = 0; |
| 264 | result = (T[])Array.newInstance(kind, 1); |
| 265 | } |
| 266 | result[end] = element; |
| 267 | return result; |
| 268 | } |
| 269 | |
| 270 | /** |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 271 | * Removes value from given array if present, providing set-like behavior. |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 272 | */ |
| 273 | @SuppressWarnings("unchecked") |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 274 | 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] | 275 | if (array != null) { |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 276 | if (!contains(array, element)) return array; |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 277 | final int length = array.length; |
| 278 | for (int i = 0; i < length; i++) { |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 279 | if (Objects.equals(array[i], element)) { |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 280 | if (length == 1) { |
| 281 | return null; |
| 282 | } |
| 283 | T[] result = (T[])Array.newInstance(kind, length - 1); |
| 284 | System.arraycopy(array, 0, result, 0, i); |
| 285 | System.arraycopy(array, i + 1, result, i, length - i - 1); |
| 286 | return result; |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | return array; |
| 291 | } |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 292 | |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 293 | /** |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 294 | * Adds value to given array if not already present, providing set-like |
| 295 | * behavior. |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 296 | */ |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 297 | public static @NonNull int[] appendInt(@Nullable int[] cur, int val) { |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 298 | if (cur == null) { |
| 299 | return new int[] { val }; |
| 300 | } |
| 301 | final int N = cur.length; |
| 302 | for (int i = 0; i < N; i++) { |
| 303 | if (cur[i] == val) { |
| 304 | return cur; |
| 305 | } |
| 306 | } |
| 307 | int[] ret = new int[N + 1]; |
| 308 | System.arraycopy(cur, 0, ret, 0, N); |
| 309 | ret[N] = val; |
| 310 | return ret; |
| 311 | } |
| 312 | |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 313 | /** |
| 314 | * Removes value from given array if present, providing set-like behavior. |
| 315 | */ |
| 316 | public static @Nullable int[] removeInt(@Nullable int[] cur, int val) { |
Jeff Sharkey | 854b2b1 | 2012-04-13 16:03:40 -0700 | [diff] [blame] | 317 | if (cur == null) { |
| 318 | return null; |
| 319 | } |
| 320 | final int N = cur.length; |
| 321 | for (int i = 0; i < N; i++) { |
| 322 | if (cur[i] == val) { |
| 323 | int[] ret = new int[N - 1]; |
| 324 | if (i > 0) { |
| 325 | System.arraycopy(cur, 0, ret, 0, i); |
| 326 | } |
| 327 | if (i < (N - 1)) { |
| 328 | System.arraycopy(cur, i + 1, ret, i, N - i - 1); |
| 329 | } |
| 330 | return ret; |
| 331 | } |
| 332 | } |
| 333 | return cur; |
| 334 | } |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 335 | |
| 336 | /** |
Svet Ganov | 52153f4 | 2015-08-11 08:59:12 -0700 | [diff] [blame] | 337 | * Removes value from given array if present, providing set-like behavior. |
| 338 | */ |
| 339 | public static @Nullable String[] removeString(@Nullable String[] cur, String val) { |
| 340 | if (cur == null) { |
| 341 | return null; |
| 342 | } |
| 343 | final int N = cur.length; |
| 344 | for (int i = 0; i < N; i++) { |
| 345 | if (Objects.equals(cur[i], val)) { |
| 346 | String[] ret = new String[N - 1]; |
| 347 | if (i > 0) { |
| 348 | System.arraycopy(cur, 0, ret, 0, i); |
| 349 | } |
| 350 | if (i < (N - 1)) { |
| 351 | System.arraycopy(cur, i + 1, ret, i, N - i - 1); |
| 352 | } |
| 353 | return ret; |
| 354 | } |
| 355 | } |
| 356 | return cur; |
| 357 | } |
| 358 | |
| 359 | /** |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 360 | * Adds value to given array if not already present, providing set-like |
| 361 | * behavior. |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 362 | */ |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 363 | public static @NonNull long[] appendLong(@Nullable long[] cur, long val) { |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 364 | if (cur == null) { |
| 365 | return new long[] { val }; |
| 366 | } |
| 367 | final int N = cur.length; |
| 368 | for (int i = 0; i < N; i++) { |
| 369 | if (cur[i] == val) { |
| 370 | return cur; |
| 371 | } |
| 372 | } |
| 373 | long[] ret = new long[N + 1]; |
| 374 | System.arraycopy(cur, 0, ret, 0, N); |
| 375 | ret[N] = val; |
| 376 | return ret; |
| 377 | } |
| 378 | |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 379 | /** |
| 380 | * Removes value from given array if present, providing set-like behavior. |
| 381 | */ |
| 382 | public static @Nullable long[] removeLong(@Nullable long[] cur, long val) { |
dcashman | 874d0d4 | 2014-05-30 09:34:04 -0700 | [diff] [blame] | 383 | if (cur == null) { |
| 384 | return null; |
| 385 | } |
| 386 | final int N = cur.length; |
| 387 | for (int i = 0; i < N; i++) { |
| 388 | if (cur[i] == val) { |
| 389 | long[] ret = new long[N - 1]; |
| 390 | if (i > 0) { |
| 391 | System.arraycopy(cur, 0, ret, 0, i); |
| 392 | } |
| 393 | if (i < (N - 1)) { |
| 394 | System.arraycopy(cur, i + 1, ret, i, N - i - 1); |
| 395 | } |
| 396 | return ret; |
| 397 | } |
| 398 | } |
| 399 | return cur; |
| 400 | } |
Jeff Sharkey | 57dcf5b | 2014-06-18 17:46:05 -0700 | [diff] [blame] | 401 | |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 402 | public static @Nullable long[] cloneOrNull(@Nullable long[] array) { |
Jeff Sharkey | 57dcf5b | 2014-06-18 17:46:05 -0700 | [diff] [blame] | 403 | return (array != null) ? array.clone() : null; |
| 404 | } |
Jeff Sharkey | da96e13 | 2014-07-15 14:54:09 -0700 | [diff] [blame] | 405 | |
Jeff Sharkey | 2bd31db | 2016-01-09 16:58:14 -0700 | [diff] [blame] | 406 | public static @Nullable <T> ArraySet<T> cloneOrNull(@Nullable ArraySet<T> array) { |
| 407 | return (array != null) ? new ArraySet<T>(array) : null; |
| 408 | } |
| 409 | |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 410 | 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] | 411 | if (cur == null) { |
| 412 | cur = new ArraySet<>(); |
| 413 | } |
| 414 | cur.add(val); |
| 415 | return cur; |
| 416 | } |
| 417 | |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 418 | 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] | 419 | if (cur == null) { |
| 420 | return null; |
| 421 | } |
| 422 | cur.remove(val); |
| 423 | if (cur.isEmpty()) { |
| 424 | return null; |
| 425 | } else { |
| 426 | return cur; |
| 427 | } |
| 428 | } |
| 429 | |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 430 | public static <T> boolean contains(@Nullable ArraySet<T> cur, T val) { |
Jeff Sharkey | da96e13 | 2014-07-15 14:54:09 -0700 | [diff] [blame] | 431 | return (cur != null) ? cur.contains(val) : false; |
| 432 | } |
| 433 | |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 434 | 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] | 435 | if (cur == null) { |
| 436 | cur = new ArrayList<>(); |
| 437 | } |
| 438 | cur.add(val); |
| 439 | return cur; |
| 440 | } |
| 441 | |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 442 | 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] | 443 | if (cur == null) { |
| 444 | return null; |
| 445 | } |
| 446 | cur.remove(val); |
| 447 | if (cur.isEmpty()) { |
| 448 | return null; |
| 449 | } else { |
| 450 | return cur; |
| 451 | } |
| 452 | } |
| 453 | |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 454 | public static <T> boolean contains(@Nullable ArrayList<T> cur, T val) { |
Jeff Sharkey | da96e13 | 2014-07-15 14:54:09 -0700 | [diff] [blame] | 455 | return (cur != null) ? cur.contains(val) : false; |
| 456 | } |
Adam Lesinski | 72478f0 | 2015-06-17 15:39:43 -0700 | [diff] [blame] | 457 | |
Jeff Sharkey | 2bd31db | 2016-01-09 16:58:14 -0700 | [diff] [blame] | 458 | public static @Nullable <T> T[] trimToSize(@Nullable T[] array, int size) { |
| 459 | if (array == null || size == 0) { |
| 460 | return null; |
| 461 | } else if (array.length == size) { |
| 462 | return array; |
| 463 | } else { |
| 464 | return Arrays.copyOf(array, size); |
| 465 | } |
| 466 | } |
| 467 | |
Adam Lesinski | 72478f0 | 2015-06-17 15:39:43 -0700 | [diff] [blame] | 468 | /** |
| 469 | * Returns true if the two ArrayLists are equal with respect to the objects they contain. |
| 470 | * The objects must be in the same order and be reference equal (== not .equals()). |
| 471 | */ |
| 472 | public static <T> boolean referenceEquals(ArrayList<T> a, ArrayList<T> b) { |
| 473 | if (a == b) { |
| 474 | return true; |
| 475 | } |
| 476 | |
| 477 | final int sizeA = a.size(); |
| 478 | final int sizeB = b.size(); |
| 479 | if (a == null || b == null || sizeA != sizeB) { |
| 480 | return false; |
| 481 | } |
| 482 | |
| 483 | boolean diff = false; |
| 484 | for (int i = 0; i < sizeA && !diff; i++) { |
| 485 | diff |= a.get(i) != b.get(i); |
| 486 | } |
| 487 | return !diff; |
| 488 | } |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 489 | |
| 490 | /** |
| 491 | * Removes elements that match the predicate in an efficient way that alters the order of |
| 492 | * elements in the collection. This should only be used if order is not important. |
| 493 | * @param collection The ArrayList from which to remove elements. |
| 494 | * @param predicate The predicate that each element is tested against. |
| 495 | * @return the number of elements removed. |
| 496 | */ |
| 497 | public static <T> int unstableRemoveIf(@Nullable ArrayList<T> collection, |
| 498 | @NonNull java.util.function.Predicate<T> predicate) { |
| 499 | if (collection == null) { |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | final int size = collection.size(); |
| 504 | int leftIdx = 0; |
| 505 | int rightIdx = size - 1; |
| 506 | while (leftIdx <= rightIdx) { |
| 507 | // Find the next element to remove moving left to right. |
| 508 | while (leftIdx < size && !predicate.test(collection.get(leftIdx))) { |
| 509 | leftIdx++; |
| 510 | } |
| 511 | |
| 512 | // Find the next element to keep moving right to left. |
| 513 | while (rightIdx > leftIdx && predicate.test(collection.get(rightIdx))) { |
| 514 | rightIdx--; |
| 515 | } |
| 516 | |
| 517 | if (leftIdx >= rightIdx) { |
| 518 | // Done. |
| 519 | break; |
| 520 | } |
| 521 | |
| 522 | Collections.swap(collection, leftIdx, rightIdx); |
| 523 | leftIdx++; |
| 524 | rightIdx--; |
| 525 | } |
| 526 | |
| 527 | // leftIdx is now at the end. |
| 528 | for (int i = size - 1; i >= leftIdx; i--) { |
| 529 | collection.remove(i); |
| 530 | } |
| 531 | return size - leftIdx; |
| 532 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 533 | } |