diff options
author | Brett Chabot <brettchabot@google.com> | 2014-06-11 16:08:22 -0700 |
---|---|---|
committer | Brett Chabot <brettchabot@google.com> | 2014-06-13 14:18:35 -0700 |
commit | f5e9a2415ec42c425c2bb17db46f2a9649992d80 (patch) | |
tree | 51560c56cffa6e2e751396bf3ccea0124b90efc5 /library/src/org/hamcrest/collection/IsCollectionContaining.java | |
parent | f79c5d901a0e83f829c2325f5304f2b2c87fac70 (diff) | |
download | hamcrest-f5e9a2415ec42c425c2bb17db46f2a9649992d80.tar.gz |
Add hamcrest 1.1 library and integration source
Change-Id: I98691c987d5845c1d6e05325971517eec7e6f8b5
Diffstat (limited to 'library/src/org/hamcrest/collection/IsCollectionContaining.java')
-rw-r--r-- | library/src/org/hamcrest/collection/IsCollectionContaining.java | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/library/src/org/hamcrest/collection/IsCollectionContaining.java b/library/src/org/hamcrest/collection/IsCollectionContaining.java new file mode 100644 index 0000000..ba98630 --- /dev/null +++ b/library/src/org/hamcrest/collection/IsCollectionContaining.java @@ -0,0 +1,65 @@ +package org.hamcrest.collection; + +import static org.hamcrest.core.AllOf.allOf; +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.Factory; +import org.hamcrest.TypeSafeMatcher; +import static org.hamcrest.core.IsEqual.equalTo; + +import java.util.Collection; +import java.util.ArrayList; + +public class IsCollectionContaining<T> extends TypeSafeMatcher<Iterable<T>> { + private final Matcher<? extends T> elementMatcher; + + public IsCollectionContaining(Matcher<? extends T> elementMatcher) { + this.elementMatcher = elementMatcher; + } + + public boolean matchesSafely(Iterable<T> collection) { + for (T item : collection) { + if (elementMatcher.matches(item)){ + return true; + } + } + return false; + } + + public void describeTo(Description description) { + description + .appendText("a collection containing ") + .appendDescriptionOf(elementMatcher); + } + + @Factory + public static <T> Matcher<Iterable<T>> hasItem(Matcher<? extends T> elementMatcher) { + return new IsCollectionContaining<T>(elementMatcher); + } + + @Factory + public static <T> Matcher<Iterable<T>> hasItem(T element) { + return hasItem(equalTo(element)); + } + + @Factory + public static <T> Matcher<Iterable<T>> hasItems(Matcher<? extends T>... elementMatchers) { + Collection<Matcher<? extends Iterable<T>>> all + = new ArrayList<Matcher<? extends Iterable<T>>>(elementMatchers.length); + for (Matcher<? extends T> elementMatcher : elementMatchers) { + all.add(hasItem(elementMatcher)); + } + return allOf(all); + } + + @Factory + public static <T> Matcher<Iterable<T>> hasItems(T... elements) { + Collection<Matcher<? extends Iterable<T>>> all + = new ArrayList<Matcher<? extends Iterable<T>>>(elements.length); + for (T element : elements) { + all.add(hasItem(element)); + } + return allOf(all); + } + +} |