aboutsummaryrefslogtreecommitdiffstats
path: root/abi-check-plugin/src/main/java/com/yahoo/abicheck/setmatcher/SetMatcher.java
blob: 9a18a76d6a2aee32b1f2f868544b446d53d6f111 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.abicheck.setmatcher;

import java.util.HashSet;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Predicate;

import static java.util.function.Predicate.not;

public class SetMatcher {

  public static <T> boolean compare(Set<T> expected, Set<T> actual, Predicate<T> itemsMatch,
      Consumer<T> onMissing, Consumer<T> onExtra) {
    boolean mismatch = false;
    Set<T> missing = new HashSet<>(expected);
    missing.removeIf(actual::contains);
    for (T item : missing) {
      mismatch = true;
      onMissing.accept(item);
    }
    Set<T> extra = new HashSet<>(actual);
    extra.removeIf(expected::contains);
    for (T item : extra) {
      mismatch = true;
      onExtra.accept(item);
    }
    Set<T> both = new HashSet<>(actual);
    both.removeIf(not(expected::contains));
    for (T item : both) {
      if (!itemsMatch.test(item)) {
        mismatch = true;
      }
    }
    return !mismatch;
  }
}