|
28 | 28 | import io.grpc.StatusRuntimeException;
|
29 | 29 | import java.security.SecureRandom;
|
30 | 30 | import java.util.ArrayList;
|
| 31 | +import java.util.Collection; |
31 | 32 | import java.util.Collections;
|
32 | 33 | import java.util.Comparator;
|
33 | 34 | import java.util.Iterator;
|
@@ -98,18 +99,8 @@ public static int compareMixed(double doubleValue, long longValue) {
|
98 | 99 | return NumberComparisonHelper.firestoreCompareDoubleWithLong(doubleValue, longValue);
|
99 | 100 | }
|
100 | 101 |
|
101 |
| - @SuppressWarnings("unchecked") |
102 |
| - private static final Comparator COMPARABLE_COMPARATOR = |
103 |
| - new Comparator<Comparable<?>>() { |
104 |
| - @Override |
105 |
| - public int compare(Comparable left, Comparable right) { |
106 |
| - return left.compareTo(right); |
107 |
| - } |
108 |
| - }; |
109 |
| - |
110 |
| - @SuppressWarnings("unchecked") |
111 | 102 | public static <T extends Comparable<T>> Comparator<T> comparator() {
|
112 |
| - return COMPARABLE_COMPARATOR; |
| 103 | + return Comparable::compareTo; |
113 | 104 | }
|
114 | 105 |
|
115 | 106 | public static FirebaseFirestoreException exceptionFromStatus(Status error) {
|
@@ -249,9 +240,94 @@ public static StringBuilder repeatSequence(
|
249 | 240 | return sb;
|
250 | 241 | }
|
251 | 242 |
|
| 243 | + /** |
| 244 | + * Compares two collections for equality using the provided comparator. The method computes the |
| 245 | + * intersection and invokes `onAdd` for every element that is in `after` but not `before`. |
| 246 | + * `onRemove` is invoked for every element in `before` but missing from `after`. |
| 247 | + * |
| 248 | + * <p>The method creates a copy of both `before` and `after` and runs in O(n log n), where n is |
| 249 | + * the size of the two lists. |
| 250 | + * |
| 251 | + * @param before The elements that exist in the original set. |
| 252 | + * @param after The elements to diff against the original set. |
| 253 | + * @param comparator The comparator to use to define equality between elements. |
| 254 | + * @param onAdd A function to invoke for every element that is part of `after` but not `before`. |
| 255 | + * @param onRemove A function to invoke for every element that is part of `before` but not |
| 256 | + * `after`. |
| 257 | + */ |
| 258 | + public static <T> void diffCollections( |
| 259 | + Collection<T> before, |
| 260 | + Collection<T> after, |
| 261 | + Comparator<T> comparator, |
| 262 | + Consumer<T> onAdd, |
| 263 | + Consumer<T> onRemove) { |
| 264 | + List<T> beforeEntries = new ArrayList<>(before); |
| 265 | + Collections.sort(beforeEntries, comparator); |
| 266 | + Iterator<T> beforeIt = beforeEntries.iterator(); |
| 267 | + @Nullable T beforeValue = advanceIterator(beforeIt); |
| 268 | + |
| 269 | + List<T> afterEntries = new ArrayList<>(after); |
| 270 | + Collections.sort(afterEntries, comparator); |
| 271 | + Iterator<T> afterIt = afterEntries.iterator(); |
| 272 | + @Nullable T afterValue = advanceIterator(afterIt); |
| 273 | + |
| 274 | + // Walk through the two sets at the same time, using the ordering defined by `comparator`. |
| 275 | + while (beforeValue != null || afterValue != null) { |
| 276 | + boolean added = false; |
| 277 | + boolean removed = false; |
| 278 | + |
| 279 | + if (beforeValue != null && afterValue != null) { |
| 280 | + int cmp = comparator.compare(beforeValue, afterValue); |
| 281 | + if (cmp < 0) { |
| 282 | + // The element was removed if the next element in our ordered walkthrough is only in |
| 283 | + // `before`. |
| 284 | + removed = true; |
| 285 | + } else if (cmp > 0) { |
| 286 | + // The element was added if the next element in our ordered walkthrough is only in |
| 287 | + // `after`. |
| 288 | + added = true; |
| 289 | + } |
| 290 | + } else if (beforeValue != null) { |
| 291 | + removed = true; |
| 292 | + } else { |
| 293 | + added = true; |
| 294 | + } |
| 295 | + |
| 296 | + if (added) { |
| 297 | + onAdd.accept(afterValue); |
| 298 | + afterValue = advanceIterator(afterIt); |
| 299 | + } else if (removed) { |
| 300 | + onRemove.accept(beforeValue); |
| 301 | + beforeValue = advanceIterator(beforeIt); |
| 302 | + } else { |
| 303 | + beforeValue = advanceIterator(beforeIt); |
| 304 | + afterValue = advanceIterator(afterIt); |
| 305 | + } |
| 306 | + } |
| 307 | + } |
| 308 | + |
| 309 | + /** |
| 310 | + * Compares two collections for equality using their natural ordering. The method computes the |
| 311 | + * intersection and invokes `onAdd` for every element that is in `after` but not `before`. |
| 312 | + * `onRemove` is invoked for every element in `before` but missing from `after`. |
| 313 | + * |
| 314 | + * <p>The method creates a copy of both `before` and `after` and runs in O(n log n), where n is |
| 315 | + * the size of the two lists. |
| 316 | + * |
| 317 | + * @param before The elements that exist in the original set. |
| 318 | + * @param after The elements to diff against the original set. |
| 319 | + * @param onAdd A function to invoke for every element that is part of `after` but not `before`. |
| 320 | + * @param onRemove A function to invoke for every element that is part of `before` but not |
| 321 | + * `after`. |
| 322 | + */ |
| 323 | + public static <T extends Comparable<T>> void diffCollections( |
| 324 | + Collection<T> before, Collection<T> after, Consumer<T> onAdd, Consumer<T> onRemove) { |
| 325 | + diffCollections(before, after, Comparable::compareTo, onAdd, onRemove); |
| 326 | + } |
| 327 | + |
252 | 328 | /** Returns the next element from the iterator or `null` if none available. */
|
253 | 329 | @Nullable
|
254 |
| - public static <T> T advanceIterator(Iterator<T> it) { |
| 330 | + private static <T> T advanceIterator(Iterator<T> it) { |
255 | 331 | return it.hasNext() ? it.next() : null;
|
256 | 332 | }
|
257 | 333 | }
|
0 commit comments