• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.hamcrest.collection;
2 
3 import org.hamcrest.Description;
4 import org.hamcrest.Matcher;
5 import org.hamcrest.TypeSafeMatcher;
6 
7 /**
8  * Tests if collection is empty.
9  */
10 public class IsEmptyIterable<E> extends TypeSafeMatcher<Iterable<? extends E>> {
11 
12     @Override
matchesSafely(Iterable<? extends E> iterable)13     public boolean matchesSafely(Iterable<? extends E> iterable) {
14         return !iterable.iterator().hasNext();
15     }
16     @Override
describeMismatchSafely(Iterable<? extends E> iter, Description mismatchDescription)17     public void describeMismatchSafely(Iterable<? extends E> iter, Description mismatchDescription) {
18         mismatchDescription.appendValueList("[", ",", "]", iter);
19     }
20 
21     @Override
describeTo(Description description)22     public void describeTo(Description description) {
23         description.appendText("an empty iterable");
24     }
25 
26     /**
27      * Creates a matcher for {@link Iterable}s matching examined iterables that yield no items.
28      * For example:
29      * <pre>assertThat(new ArrayList&lt;String&gt;(), is(emptyIterable()))</pre>
30      *
31      */
emptyIterable()32     public static <E> Matcher<Iterable<? extends E>> emptyIterable() {
33         return new IsEmptyIterable<E>();
34     }
35 
36     /**
37      * Creates a matcher for {@link Iterable}s matching examined iterables that yield no items.
38      * For example:
39      * <pre>assertThat(new ArrayList&lt;String&gt;(), is(emptyIterableOf(String.class)))</pre>
40      *
41      * @param unusedToForceReturnType
42      *     the type of the iterable's content
43      */
44     @SuppressWarnings({"unchecked", "UnusedParameters"})
emptyIterableOf(Class<E> unusedToForceReturnType)45     public static <E> Matcher<Iterable<E>> emptyIterableOf(Class<E> unusedToForceReturnType) {
46       return (Matcher)emptyIterable();
47     }
48 }
49