• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.hamcrest.core;
2 
3 import org.hamcrest.BaseMatcher;
4 import org.hamcrest.Description;
5 import org.hamcrest.Matcher;
6 
7 import static org.hamcrest.core.IsNot.not;
8 
9 /**
10  * Is the value null?
11  */
12 public class IsNull<T> extends BaseMatcher<T> {
13     @Override
matches(Object o)14     public boolean matches(Object o) {
15         return o == null;
16     }
17 
18     @Override
describeTo(Description description)19     public void describeTo(Description description) {
20         description.appendText("null");
21     }
22 
23     /**
24      * Creates a matcher that matches if examined object is <code>null</code>.
25      * For example:
26      * <pre>assertThat(cheese, is(nullValue())</pre>
27      *
28      */
nullValue()29     public static Matcher<Object> nullValue() {
30         return new IsNull<Object>();
31     }
32 
33     /**
34      * A shortcut to the frequently used <code>not(nullValue())</code>.
35      * For example:
36      * <pre>assertThat(cheese, is(notNullValue()))</pre>
37      * instead of:
38      * <pre>assertThat(cheese, is(not(nullValue())))</pre>
39      *
40      */
notNullValue()41     public static Matcher<Object> notNullValue() {
42         return not(nullValue());
43     }
44 
45     /**
46      * Creates a matcher that matches if examined object is <code>null</code>. Accepts a
47      * single dummy argument to facilitate type inference.
48      * For example:
49      * <pre>assertThat(cheese, is(nullValue(Cheese.class))</pre>
50      *
51      * @param type
52      *     dummy parameter used to infer the generic type of the returned matcher
53      */
nullValue(Class<T> type)54     public static <T> Matcher<T> nullValue(Class<T> type) {
55         return new IsNull<T>();
56     }
57 
58     /**
59      * A shortcut to the frequently used <code>not(nullValue(X.class)). Accepts a
60      * single dummy argument to facilitate type inference.</code>.
61      * For example:
62      * <pre>assertThat(cheese, is(notNullValue(X.class)))</pre>
63      * instead of:
64      * <pre>assertThat(cheese, is(not(nullValue(X.class))))</pre>
65      *
66      * @param type
67      *     dummy parameter used to infer the generic type of the returned matcher
68      *
69      */
notNullValue(Class<T> type)70     public static <T> Matcher<T> notNullValue(Class<T> type) {
71         return not(nullValue(type));
72     }
73 }
74 
75