11ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot/*  Copyright (c) 2000-2006 hamcrest.org
21ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot */
31ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabotpackage org.hamcrest.core;
41ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot
51ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabotimport static org.hamcrest.core.IsEqual.equalTo;
61ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabotimport org.hamcrest.Description;
71ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabotimport org.hamcrest.Matcher;
81ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabotimport org.hamcrest.Factory;
91ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabotimport org.hamcrest.BaseMatcher;
101ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot
111ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot
121ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot/**
131ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot * Calculates the logical negation of a matcher.
141ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot */
151ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabotpublic class IsNot<T> extends BaseMatcher<T> {
161ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    private final Matcher<T> matcher;
171ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot
181ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    public IsNot(Matcher<T> matcher) {
191ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot        this.matcher = matcher;
201ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    }
211ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot
221ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    public boolean matches(Object arg) {
231ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot        return !matcher.matches(arg);
241ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    }
251ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot
261ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    public void describeTo(Description description) {
271ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot        description.appendText("not ").appendDescriptionOf(matcher);
281ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    }
291ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot
301ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    /**
311ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot     * Inverts the rule.
321ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot     */
331ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    @Factory
341ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    public static <T> Matcher<T> not(Matcher<T> matcher) {
351ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot        return new IsNot<T>(matcher);
361ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    }
371ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot
381ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    /**
391ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot     * This is a shortcut to the frequently used not(equalTo(x)).
401ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot     *
411ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot     * eg. assertThat(cheese, is(not(equalTo(smelly))))
421ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot     * vs  assertThat(cheese, is(not(smelly)))
431ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot     */
441ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    @Factory
451ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    public static <T> Matcher<T> not(T value) {
461ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot        return not(equalTo(value));
471ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    }
481ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot
491ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot}
50