1package org.hamcrest.core;
2
3import org.hamcrest.BaseMatcher;
4import org.hamcrest.Description;
5import org.hamcrest.Matcher;
6
7import static org.hamcrest.core.IsEqual.equalTo;
8
9
10/**
11 * Calculates the logical negation of a matcher.
12 */
13public class IsNot<T> extends BaseMatcher<T>  {
14    private final Matcher<T> matcher;
15
16    public IsNot(Matcher<T> matcher) {
17        this.matcher = matcher;
18    }
19
20    @Override
21    public boolean matches(Object arg) {
22        return !matcher.matches(arg);
23    }
24
25    @Override
26    public void describeTo(Description description) {
27        description.appendText("not ").appendDescriptionOf(matcher);
28    }
29
30
31    /**
32     * Creates a matcher that wraps an existing matcher, but inverts the logic by which
33     * it will match.
34     * For example:
35     * <pre>assertThat(cheese, is(not(equalTo(smelly))))</pre>
36     *
37     * @param matcher
38     *     the matcher whose sense should be inverted
39     */
40    public static <T> Matcher<T> not(Matcher<T> matcher) {
41        return new IsNot<T>(matcher);
42    }
43
44    /**
45     * A shortcut to the frequently used <code>not(equalTo(x))</code>.
46     * For example:
47     * <pre>assertThat(cheese, is(not(smelly)))</pre>
48     * instead of:
49     * <pre>assertThat(cheese, is(not(equalTo(smelly))))</pre>
50     *
51     * @param value
52     *     the value that any examined object should <b>not</b> equal
53     */
54    public static <T> Matcher<T> not(T value) {
55        return not(equalTo(value));
56    }
57}
58