3 import java.util.ArrayList;
6 import org.hamcrest.Description;
7 import org.hamcrest.Factory;
8 import org.hamcrest.TypeSafeDiagnosingMatcher;
11 * Matcher that finds items in string collections in a case-insensitive way.
13 public class CaseInsensitiveStringCollectionMatcher extends TypeSafeDiagnosingMatcher<List<String>> {
15 private final String[] items;
17 public CaseInsensitiveStringCollectionMatcher(String... items) {
22 protected boolean matchesSafely(List<String> collection, Description description) {
23 List<String> missing = new ArrayList<>();
25 for (String item : items) {
26 if (collection.stream().noneMatch(i -> i.equalsIgnoreCase(item))) {
31 if (!missing.isEmpty()) {
32 if (missing.size() == items.length) {
33 description.appendValueList("was [", ", ", "]", collection);
35 description.appendValueList("missing [", ", ", "]", missing)
36 .appendValueList(" in [", ", ", "]", collection);
40 return missing.isEmpty();
44 public void describeTo(Description description) {
45 description.appendValueList("collection containing ", " and ", "", items);
49 public static CaseInsensitiveStringCollectionMatcher hasItems(String... items) {
50 return new CaseInsensitiveStringCollectionMatcher(items);