react-native-navigation的迁移库

CollectionUtils.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package com.reactnativenavigation.utils;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.Collections;
  5. import java.util.HashMap;
  6. import java.util.Iterator;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.Objects;
  10. import androidx.annotation.NonNull;
  11. import androidx.annotation.Nullable;
  12. import androidx.core.util.Pair;
  13. public class CollectionUtils {
  14. public interface Apply<T> {
  15. void on(T t);
  16. }
  17. public static boolean isNullOrEmpty(Collection collection) {
  18. return collection == null || collection.isEmpty();
  19. }
  20. public interface KeyBy<K, V> {
  21. K by(V value);
  22. }
  23. public static <K, V> Map<K, V> keyBy(Collection<V> elements, KeyBy<K, V> key) {
  24. Map<K, V> map = new HashMap<>();
  25. for (V value : elements) {
  26. map.put(key.by(value), value);
  27. }
  28. return map;
  29. }
  30. public interface Mapper<T, S> {
  31. S map(T value);
  32. }
  33. public static @Nullable <T, S> List<S> map(@Nullable Collection<T> items, Mapper<T, S> map) {
  34. if (items == null) return null;
  35. List<S> result = new ArrayList<>();
  36. for (T item : items) {
  37. result.add(map.map(item));
  38. }
  39. return result;
  40. }
  41. public interface Filter<T> {
  42. boolean filter(T value);
  43. }
  44. public static <T> List<T> filter(Collection<T> list, Filter<T> filter) {
  45. List<T> result = new ArrayList<>();
  46. for (T t : list) {
  47. if (filter.filter(t)) result.add(t);
  48. }
  49. return result;
  50. }
  51. public static <T> List<T> merge(@Nullable Collection<T> a, @Nullable Collection<T> b, @NonNull List<T> defaultValue) {
  52. List<T> result = merge(a, b);
  53. return result == null ? defaultValue : result;
  54. }
  55. public static <T> List<T> merge(@Nullable Collection<T> a, @Nullable Collection<T> b) {
  56. if (a == null && b == null) return null;
  57. List<T> result = new ArrayList<>(get(a));
  58. result.addAll(get(b));
  59. return result;
  60. }
  61. public static <T> void forEach(@Nullable Collection<T> items, Apply<T> apply) {
  62. if (items != null) forEach(new ArrayList<>(items), 0, apply);
  63. }
  64. public static <T> void forEach(@Nullable T[] items, Apply<T> apply) {
  65. if (items == null) return;
  66. for (T item : items) {
  67. apply.on(item);
  68. }
  69. }
  70. public static <T> void forEach(@Nullable List<T> items, Apply<T> apply) {
  71. forEach(items, 0, apply);
  72. }
  73. public static <T> void forEach(@Nullable List<T> items, int startIndex, Apply<T> apply) {
  74. if (items == null) return;
  75. for (int i = startIndex; i < items.size(); i++) {
  76. apply.on(items.get(i));
  77. }
  78. }
  79. public static @Nullable <T> T first(@Nullable Collection<T> items, Filter<T> by) {
  80. if (isNullOrEmpty(items)) return null;
  81. for (T item : items) {
  82. if (by.filter(item)) return item;
  83. }
  84. return null;
  85. }
  86. public static @Nullable <T> T first(@Nullable Collection<T> items, Filter<T> by, Functions.Func1<T> apply) {
  87. if (isNullOrEmpty(items)) return null;
  88. for (T item : items) {
  89. if (by.filter(item)) {
  90. apply.run(item);
  91. return item;
  92. }
  93. }
  94. return null;
  95. }
  96. public static <T> T last(@Nullable List<T> items) {
  97. return CollectionUtils.isNullOrEmpty(items) ? null : items.get(items.size() - 1);
  98. }
  99. public static <T> T removeLast(@NonNull List<T> items) {
  100. return items.remove(items.size() - 1);
  101. }
  102. public interface Reducer<S, T> {
  103. S reduce(T item, S currentValue);
  104. }
  105. public static <S, T> S reduce(Collection<T> items, S initialValue, Reducer<S, T> reducer) {
  106. S currentValue = initialValue;
  107. for (T item : items) {
  108. currentValue = reducer.reduce(item, currentValue);
  109. }
  110. return currentValue;
  111. }
  112. public static <T> Boolean reduce(@Nullable Collection<T> items, boolean initialValue, Functions.FuncR1<T, Boolean> reducer) {
  113. boolean currentValue = initialValue;
  114. if (CollectionUtils.isNullOrEmpty(items)) return currentValue;
  115. for (T item : items) {
  116. currentValue &= reducer.run(item);
  117. if (!currentValue) return false;
  118. }
  119. return currentValue;
  120. }
  121. public static @NonNull <T> Collection<T> get(@Nullable Collection<T> t) {
  122. return t == null ? Collections.emptyList() : t;
  123. }
  124. public static @NonNull <T> Collection<T> get(@Nullable Map<?, T> t) {
  125. return t == null ? Collections.emptyList() : t.values();
  126. }
  127. public static <T> boolean equals(@Nullable Collection<T> a, @Nullable Collection<T> b) {
  128. if (size(a) != size(b)) return false;
  129. return reduce(zip(a, b), true, (p, currentValue) -> currentValue && Objects.equals(p.first, p.second));
  130. }
  131. public static int size(@Nullable Collection items) {
  132. return items == null ? 0 : items.size();
  133. }
  134. public static <T> Collection<Pair<T, T>> zip(@Nullable Collection<T> a, @Nullable Collection<T> b) {
  135. if (a == null || b == null) return new ArrayList<>();
  136. Iterator iter1 = a.iterator();
  137. Iterator iter2 = b.iterator();
  138. ArrayList<Pair<T,T>> result = new ArrayList<>();
  139. while (iter1.hasNext() && iter2.hasNext()) {
  140. result.add(new Pair(iter1.next(), iter2.next()));
  141. }
  142. return result;
  143. }
  144. }