// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 package software.amazon.lambda.snapstart.matcher; import java.util.Arrays; import java.util.Iterator; import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.StringDescription; import org.hamcrest.TypeSafeMatcher; public final class ContainsMatcher extends TypeSafeMatcher> { private final Iterable> matchers; private final Description description; public ContainsMatcher(Iterable> matchers) { this.matchers = matchers; this.description = new StringDescription(); } public static Matcher> containsAll(final Iterable> matchers) { return new ContainsMatcher<>(matchers); } public static Matcher> containsAll(final Matcher ... matchers) { return containsAll(Arrays.asList(matchers)); } @Override protected boolean matchesSafely(Iterable items) { Iterator itemsIterator = items.iterator(); Iterator> matcherIterator = matchers.iterator(); boolean allItemsMatched = true; while (matcherIterator.hasNext()) { Matcher matcher = matcherIterator.next(); T item = null; if (itemsIterator.hasNext()) { item = itemsIterator.next(); } if (!matcher.matches(item)) { matcher.describeTo(description); description.appendText("\n"); allItemsMatched = false; } } return allItemsMatched; } @Override public void describeTo(final Description desc) { desc.appendText(description.toString()); } }