diff options
author | Paul Duffin <paulduffin@google.com> | 2017-01-24 18:36:59 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2017-01-24 18:36:59 +0000 |
commit | 05e68aaf5378b155cc99e7c2b8b87de92011c5ce (patch) | |
tree | aa4a6a53609f47d386dfa114be0abfc01e0e5dff /hamcrest-library/src/main/java/org/hamcrest/collection/IsArrayContaining.java | |
parent | 3752c058e57f3b560396cc9484bd353cba75cfc3 (diff) | |
parent | 1dfb5bbe24ccbd127405dc8bb8e178536d1a70b4 (diff) | |
download | hamcrest-05e68aaf5378b155cc99e7c2b8b87de92011c5ce.tar.gz |
Merge "Match upstream file structure" am: 5d9cc8c989 am: b5937e4be3 am: 11a97a2ff7
am: 1dfb5bbe24
Change-Id: Ib4f853674ae97e7d7b4e3cef05e2668ef514e045
Diffstat (limited to 'hamcrest-library/src/main/java/org/hamcrest/collection/IsArrayContaining.java')
-rw-r--r-- | hamcrest-library/src/main/java/org/hamcrest/collection/IsArrayContaining.java | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/hamcrest-library/src/main/java/org/hamcrest/collection/IsArrayContaining.java b/hamcrest-library/src/main/java/org/hamcrest/collection/IsArrayContaining.java new file mode 100644 index 0000000..76ddf9d --- /dev/null +++ b/hamcrest-library/src/main/java/org/hamcrest/collection/IsArrayContaining.java @@ -0,0 +1,42 @@ +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<T> extends TypeSafeMatcher<T[]> { + + private final Matcher<T> elementMatcher; + + public IsArrayContaining(Matcher<T> 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 <T> Matcher<T[]> hasItemInArray(Matcher<T> elementMatcher) { + return new IsArrayContaining<T>(elementMatcher); + } + + @Factory + public static <T> Matcher<T[]> hasItemInArray(T element) { + return hasItemInArray(equalTo(element)); + } + +} |