Home | History | Annotate | Download | only in collection
      1 package org.hamcrest.collection;
      2 
      3 import org.hamcrest.Description;
      4 import org.hamcrest.Matcher;
      5 import org.hamcrest.TypeSafeMatcher;
      6 
      7 import java.util.Collection;
      8 
      9 /**
     10  * Tests if collection is empty.
     11  */
     12 public class IsEmptyCollection<E> extends TypeSafeMatcher<Collection<? extends E>> {
     13 
     14     @Override
     15     public boolean matchesSafely(Collection<? extends E> item) {
     16         return item.isEmpty();
     17     }
     18 
     19     @Override
     20     public void describeMismatchSafely(Collection<? extends E> item, Description mismatchDescription) {
     21       mismatchDescription.appendValue(item);
     22     }
     23 
     24     @Override
     25     public void describeTo(Description description) {
     26         description.appendText("an empty collection");
     27     }
     28 
     29     /**
     30      * Creates a matcher for {@link java.util.Collection}s matching examined collections whose <code>isEmpty</code>
     31      * method returns <code>true</code>.
     32      * For example:
     33      * <pre>assertThat(new ArrayList&lt;String&gt;(), is(empty()))</pre>
     34      *
     35      */
     36     public static <E> Matcher<Collection<? extends E>> empty() {
     37         return new IsEmptyCollection<E>();
     38     }
     39 
     40     /**
     41      * Creates a matcher for {@link java.util.Collection}s matching examined collections whose <code>isEmpty</code>
     42      * method returns <code>true</code>.
     43      * For example:
     44      * <pre>assertThat(new ArrayList&lt;String&gt;(), is(emptyCollectionOf(String.class)))</pre>
     45      *
     46      * @param unusedToForceReturnType
     47      *     the type of the collection's content
     48      */
     49     @SuppressWarnings({"unchecked", "UnusedParameters"})
     50     public static <E> Matcher<Collection<E>> emptyCollectionOf(Class<E> unusedToForceReturnType) {
     51       return (Matcher)empty();
     52     }
     53 }
     54