001/*  Copyright (c) 2000-2009 hamcrest.org
002 */
003package org.hamcrest.core;
004
005import org.hamcrest.BaseMatcher;
006import org.hamcrest.Description;
007import org.hamcrest.Factory;
008import org.hamcrest.Matcher;
009
010import static org.hamcrest.core.IsEqual.equalTo;
011
012
013/**
014 * Calculates the logical negation of a matcher.
015 */
016public class IsNot<T> extends BaseMatcher<T>  {
017    private final Matcher<T> matcher;
018
019    public IsNot(Matcher<T> matcher) {
020        this.matcher = matcher;
021    }
022
023    @Override
024    public boolean matches(Object arg) {
025        return !matcher.matches(arg);
026    }
027
028    @Override
029    public void describeTo(Description description) {
030        description.appendText("not ").appendDescriptionOf(matcher);
031    }
032
033    
034    /**
035     * Creates a matcher that wraps an existing matcher, but inverts the logic by which
036     * it will match.
037     * <p/>
038     * For example:
039     * <pre>assertThat(cheese, is(not(equalTo(smelly))))</pre>
040     * 
041     * @param matcher
042     *     the matcher whose sense should be inverted
043     */
044    @Factory
045    public static <T> Matcher<T> not(Matcher<T> matcher) {
046        return new IsNot<T>(matcher);
047    }
048
049    /**
050     * A shortcut to the frequently used <code>not(equalTo(x))</code>.
051     * <p/>
052     * For example:
053     * <pre>assertThat(cheese, is(not(smelly)))</pre>
054     * instead of:
055     * <pre>assertThat(cheese, is(not(equalTo(smelly))))</pre>
056     * 
057     * @param value
058     *     the value that any examined object should <b>not</b> equal
059     */
060    @Factory
061    public static <T> Matcher<T> not(T value) {
062        return not(equalTo(value));
063    }
064}