package org.hamcrest.collection; import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.TypeSafeMatcher; /** * Tests if collection is empty. */ public class IsEmptyIterable extends TypeSafeMatcher> { @Override public boolean matchesSafely(Iterable iterable) { return !iterable.iterator().hasNext(); } @Override public void describeMismatchSafely(Iterable iter, Description mismatchDescription) { mismatchDescription.appendValueList("[", ",", "]", iter); } @Override public void describeTo(Description description) { description.appendText("an empty iterable"); } /** * Creates a matcher for {@link Iterable}s matching examined iterables that yield no items. * For example: *
assertThat(new ArrayList<String>(), is(emptyIterable()))
* */ public static Matcher> emptyIterable() { return new IsEmptyIterable(); } /** * Creates a matcher for {@link Iterable}s matching examined iterables that yield no items. * For example: *
assertThat(new ArrayList<String>(), is(emptyIterableOf(String.class)))
* * @param unusedToForceReturnType * the type of the iterable's content */ @SuppressWarnings({"unchecked", "UnusedParameters"}) public static Matcher> emptyIterableOf(Class unusedToForceReturnType) { return (Matcher)emptyIterable(); } }