All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yahoo.abicheck.setmatcher.SetMatcher Maven / Gradle / Ivy

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy