package org.hamcrest.collection; import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.Factory; import org.hamcrest.TypeSafeMatcher; import static org.hamcrest.core.IsEqual.equalTo; public class IsArrayContaining extends TypeSafeMatcher { private final Matcher elementMatcher; public IsArrayContaining(Matcher elementMatcher) { this.elementMatcher = elementMatcher; } public boolean matchesSafely(T[] array) { for (T item : array) { if (elementMatcher.matches(item)) { return true; } } return false; } public void describeTo(Description description) { description .appendText("an array containing ") .appendDescriptionOf(elementMatcher); } @Factory public static Matcher hasItemInArray(Matcher elementMatcher) { return new IsArrayContaining(elementMatcher); } @Factory public static Matcher hasItemInArray(T element) { return hasItemInArray(equalTo(element)); } }