| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.util; |
| 18 |
|
| 19 |
import static java.util.Collections.emptySet; |
| 20 |
|
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.annotation.Nullable; |
| 23 |
import android.util.ArrayMap; |
| 24 |
import android.util.ArraySet; |
| 25 |
import android.util.ExceptionUtils; |
| 26 |
|
| 27 |
import com.android.internal.util.FunctionalUtils.ThrowingConsumer; |
| 28 |
|
| 29 |
import java.util.ArrayList; |
| 30 |
import java.util.Collection; |
| 31 |
import java.util.Collections; |
| 32 |
import java.util.List; |
| 33 |
import java.util.Map; |
| 34 |
import java.util.Set; |
| 35 |
import java.util.function.BiConsumer; |
| 36 |
import java.util.function.Function; |
| 37 |
import java.util.function.Predicate; |
| 38 |
import java.util.stream.Stream; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public class CollectionUtils { |
| 47 |
private CollectionUtils() { } |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public static <T> boolean contains(@Nullable Collection<T> collection, T element) { |
| 53 |
return collection != null && collection.contains(element); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public static @NonNull <T> List<T> filter(@Nullable List<T> list, |
| 63 |
java.util.function.Predicate<? super T> predicate) { |
| 64 |
ArrayList<T> result = null; |
| 65 |
for (int i = 0; i < size(list); i++) { |
| 66 |
final T item = list.get(i); |
| 67 |
if (predicate.test(item)) { |
| 68 |
result = ArrayUtils.add(result, item); |
| 69 |
} |
| 70 |
} |
| 71 |
return emptyIfNull(result); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public static @NonNull <T> Set<T> filter(@Nullable Set<T> set, |
| 78 |
java.util.function.Predicate<? super T> predicate) { |
| 79 |
if (set == null || set.size() == 0) return emptySet(); |
| 80 |
ArraySet<T> result = null; |
| 81 |
if (set instanceof ArraySet) { |
| 82 |
ArraySet<T> arraySet = (ArraySet<T>) set; |
| 83 |
int size = arraySet.size(); |
| 84 |
for (int i = 0; i < size; i++) { |
| 85 |
final T item = arraySet.valueAt(i); |
| 86 |
if (predicate.test(item)) { |
| 87 |
result = ArrayUtils.add(result, item); |
| 88 |
} |
| 89 |
} |
| 90 |
} else { |
| 91 |
for (T item : set) { |
| 92 |
if (predicate.test(item)) { |
| 93 |
result = ArrayUtils.add(result, item); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
return emptyIfNull(result); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
public static <T> void addIf(@Nullable List<T> source, @NonNull Collection<? super T> dest, |
| 102 |
@Nullable Predicate<? super T> predicate) { |
| 103 |
for (int i = 0; i < size(source); i++) { |
| 104 |
final T item = source.get(i); |
| 105 |
if (predicate.test(item)) { |
| 106 |
dest.add(item); |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public static @NonNull <I, O> List<O> map(@Nullable List<I> cur, |
| 121 |
Function<? super I, ? extends O> f) { |
| 122 |
if (isEmpty(cur)) return Collections.emptyList(); |
| 123 |
final ArrayList<O> result = new ArrayList<>(); |
| 124 |
for (int i = 0; i < cur.size(); i++) { |
| 125 |
result.add(f.apply(cur.get(i))); |
| 126 |
} |
| 127 |
return result; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public static @NonNull <I, O> Set<O> map(@Nullable Set<I> cur, |
| 134 |
Function<? super I, ? extends O> f) { |
| 135 |
if (isEmpty(cur)) return emptySet(); |
| 136 |
ArraySet<O> result = new ArraySet<>(); |
| 137 |
if (cur instanceof ArraySet) { |
| 138 |
ArraySet<I> arraySet = (ArraySet<I>) cur; |
| 139 |
int size = arraySet.size(); |
| 140 |
for (int i = 0; i < size; i++) { |
| 141 |
result.add(f.apply(arraySet.valueAt(i))); |
| 142 |
} |
| 143 |
} else { |
| 144 |
for (I item : cur) { |
| 145 |
result.add(f.apply(item)); |
| 146 |
} |
| 147 |
} |
| 148 |
return result; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public static @NonNull <I, O> List<O> mapNotNull(@Nullable List<I> cur, |
| 163 |
Function<? super I, ? extends O> f) { |
| 164 |
if (isEmpty(cur)) return Collections.emptyList(); |
| 165 |
List<O> result = null; |
| 166 |
for (int i = 0; i < cur.size(); i++) { |
| 167 |
O transformed = f.apply(cur.get(i)); |
| 168 |
if (transformed != null) { |
| 169 |
result = add(result, transformed); |
| 170 |
} |
| 171 |
} |
| 172 |
return emptyIfNull(result); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public static @NonNull <T> List<T> emptyIfNull(@Nullable List<T> cur) { |
| 183 |
return cur == null ? Collections.emptyList() : cur; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public static @NonNull <T> Set<T> emptyIfNull(@Nullable Set<T> cur) { |
| 194 |
return cur == null ? emptySet() : cur; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public static @NonNull <K, V> Map<K, V> emptyIfNull(@Nullable Map<K, V> cur) { |
| 205 |
return cur == null ? Collections.emptyMap() : cur; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
public static int size(@Nullable Collection<?> cur) { |
| 212 |
return cur != null ? cur.size() : 0; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public static int size(@Nullable Map<?, ?> cur) { |
| 219 |
return cur != null ? cur.size() : 0; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
public static boolean isEmpty(@Nullable Collection<?> cur) { |
| 226 |
return size(cur) == 0; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
public static boolean isEmpty(@Nullable Map<?, ?> cur) { |
| 233 |
return size(cur) == 0; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
public static @NonNull <T> List<T> filter(@Nullable List<?> list, Class<T> c) { |
| 240 |
if (isEmpty(list)) return Collections.emptyList(); |
| 241 |
ArrayList<T> result = null; |
| 242 |
for (int i = 0; i < list.size(); i++) { |
| 243 |
final Object item = list.get(i); |
| 244 |
if (c.isInstance(item)) { |
| 245 |
result = ArrayUtils.add(result, (T) item); |
| 246 |
} |
| 247 |
} |
| 248 |
return emptyIfNull(result); |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
public static <T> boolean any(@Nullable List<T> items, |
| 256 |
java.util.function.Predicate<T> predicate) { |
| 257 |
return find(items, predicate) != null; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
public static <T> boolean any(@Nullable Set<T> items, |
| 265 |
java.util.function.Predicate<T> predicate) { |
| 266 |
return find(items, predicate) != null; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
public static @Nullable <T> T find(@Nullable List<T> items, |
| 274 |
java.util.function.Predicate<T> predicate) { |
| 275 |
if (isEmpty(items)) return null; |
| 276 |
for (int i = 0; i < items.size(); i++) { |
| 277 |
final T item = items.get(i); |
| 278 |
if (predicate.test(item)) return item; |
| 279 |
} |
| 280 |
return null; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
public static @Nullable <T> T find(@Nullable Set<T> cur, |
| 288 |
java.util.function.Predicate<T> predicate) { |
| 289 |
if (cur == null || predicate == null) return null; |
| 290 |
int size = cur.size(); |
| 291 |
if (size == 0) return null; |
| 292 |
try { |
| 293 |
if (cur instanceof ArraySet) { |
| 294 |
ArraySet<T> arraySet = (ArraySet<T>) cur; |
| 295 |
for (int i = 0; i < size; i++) { |
| 296 |
T item = arraySet.valueAt(i); |
| 297 |
if (predicate.test(item)) { |
| 298 |
return item; |
| 299 |
} |
| 300 |
} |
| 301 |
} else { |
| 302 |
for (T t : cur) { |
| 303 |
if (predicate.test(t)) { |
| 304 |
return t; |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
} catch (Exception e) { |
| 309 |
throw ExceptionUtils.propagate(e); |
| 310 |
} |
| 311 |
return null; |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
public static @NonNull <T> List<T> add(@Nullable List<T> cur, T val) { |
| 319 |
if (cur == null || cur == Collections.emptyList()) { |
| 320 |
cur = new ArrayList<>(); |
| 321 |
} |
| 322 |
cur.add(val); |
| 323 |
return cur; |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
public static @NonNull <T> List<T> add(@Nullable List<T> cur, int index, T val) { |
| 331 |
if (cur == null || cur == Collections.emptyList()) { |
| 332 |
cur = new ArrayList<>(); |
| 333 |
} |
| 334 |
cur.add(index, val); |
| 335 |
return cur; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
public static @NonNull <T> Set<T> addAll(@Nullable Set<T> cur, @Nullable Collection<T> val) { |
| 343 |
if (isEmpty(val)) { |
| 344 |
return cur != null ? cur : emptySet(); |
| 345 |
} |
| 346 |
if (cur == null || cur == emptySet()) { |
| 347 |
cur = new ArraySet<>(); |
| 348 |
} |
| 349 |
cur.addAll(val); |
| 350 |
return cur; |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
public static @NonNull <T> Set<T> add(@Nullable Set<T> cur, T val) { |
| 357 |
if (cur == null || cur == emptySet()) { |
| 358 |
cur = new ArraySet<>(); |
| 359 |
} |
| 360 |
cur.add(val); |
| 361 |
return cur; |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
public static @NonNull <K, V> Map<K, V> add(@Nullable Map<K, V> map, K key, V value) { |
| 368 |
if (map == null || map == Collections.emptyMap()) { |
| 369 |
map = new ArrayMap<>(); |
| 370 |
} |
| 371 |
map.put(key, value); |
| 372 |
return map; |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
public static @NonNull <T> List<T> remove(@Nullable List<T> cur, T val) { |
| 380 |
if (isEmpty(cur)) { |
| 381 |
return emptyIfNull(cur); |
| 382 |
} |
| 383 |
cur.remove(val); |
| 384 |
return cur; |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
public static @NonNull <T> Set<T> remove(@Nullable Set<T> cur, T val) { |
| 391 |
if (isEmpty(cur)) { |
| 392 |
return emptyIfNull(cur); |
| 393 |
} |
| 394 |
cur.remove(val); |
| 395 |
return cur; |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
public static @NonNull <T> List<T> copyOf(@Nullable List<T> cur) { |
| 402 |
return isEmpty(cur) ? Collections.emptyList() : new ArrayList<>(cur); |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
public static @NonNull <T> Set<T> copyOf(@Nullable Set<T> cur) { |
| 409 |
return isEmpty(cur) ? emptySet() : new ArraySet<>(cur); |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
public static @NonNull <T> Set<T> toSet(@Nullable Collection<T> cur) { |
| 416 |
return isEmpty(cur) ? emptySet() : new ArraySet<>(cur); |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
public static <T> void forEach(@Nullable Set<T> cur, @Nullable ThrowingConsumer<T> action) { |
| 425 |
if (cur == null || action == null) return; |
| 426 |
int size = cur.size(); |
| 427 |
if (size == 0) return; |
| 428 |
try { |
| 429 |
if (cur instanceof ArraySet) { |
| 430 |
ArraySet<T> arraySet = (ArraySet<T>) cur; |
| 431 |
for (int i = 0; i < size; i++) { |
| 432 |
action.acceptOrThrow(arraySet.valueAt(i)); |
| 433 |
} |
| 434 |
} else { |
| 435 |
for (T t : cur) { |
| 436 |
action.acceptOrThrow(t); |
| 437 |
} |
| 438 |
} |
| 439 |
} catch (Exception e) { |
| 440 |
throw ExceptionUtils.propagate(e); |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
public static <K, V> void forEach(@Nullable Map<K, V> cur, @Nullable BiConsumer<K, V> action) { |
| 451 |
if (cur == null || action == null) { |
| 452 |
return; |
| 453 |
} |
| 454 |
int size = cur.size(); |
| 455 |
if (size == 0) { |
| 456 |
return; |
| 457 |
} |
| 458 |
|
| 459 |
if (cur instanceof ArrayMap) { |
| 460 |
ArrayMap<K, V> arrayMap = (ArrayMap<K, V>) cur; |
| 461 |
for (int i = 0; i < size; i++) { |
| 462 |
action.accept(arrayMap.keyAt(i), arrayMap.valueAt(i)); |
| 463 |
} |
| 464 |
} else { |
| 465 |
for (K key : cur.keySet()) { |
| 466 |
action.accept(key, cur.get(key)); |
| 467 |
} |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
public static @Nullable <T> T firstOrNull(@Nullable List<T> cur) { |
| 475 |
return isEmpty(cur) ? null : cur.get(0); |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
public static @Nullable <T> T firstOrNull(@Nullable Collection<T> cur) { |
| 482 |
return isEmpty(cur) ? null : cur.iterator().next(); |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
public static @NonNull <T> List<T> singletonOrEmpty(@Nullable T item) { |
| 489 |
return item == null ? Collections.emptyList() : Collections.singletonList(item); |
| 490 |
} |
| 491 |
} |