1 /* Copyright (c) 2000-2006 hamcrest.org 2 */ 3 package org.hamcrest.core; 4 5 import static org.hamcrest.core.IsEqual.equalTo; 6 import org.hamcrest.Description; 7 import org.hamcrest.Matcher; 8 import org.hamcrest.Factory; 9 import org.hamcrest.BaseMatcher; 10 11 12 /** 13 * Calculates the logical negation of a matcher. 14 */ 15 public class IsNot<T> extends BaseMatcher<T> { 16 private final Matcher<T> matcher; 17 IsNot(Matcher<T> matcher)18 public IsNot(Matcher<T> matcher) { 19 this.matcher = matcher; 20 } 21 matches(Object arg)22 public boolean matches(Object arg) { 23 return !matcher.matches(arg); 24 } 25 describeTo(Description description)26 public void describeTo(Description description) { 27 description.appendText("not ").appendDescriptionOf(matcher); 28 } 29 30 /** 31 * Inverts the rule. 32 */ 33 @Factory not(Matcher<T> matcher)34 public static <T> Matcher<T> not(Matcher<T> matcher) { 35 return new IsNot<T>(matcher); 36 } 37 38 /** 39 * This is a shortcut to the frequently used not(equalTo(x)). 40 * 41 * eg. assertThat(cheese, is(not(equalTo(smelly)))) 42 * vs assertThat(cheese, is(not(smelly))) 43 */ 44 @Factory not(T value)45 public static <T> Matcher<T> not(T value) { 46 return not(equalTo(value)); 47 } 48 49 } 50