1/* Copyright (c) 2000-2006 hamcrest.org 2 */ 3package org.hamcrest.core; 4 5import static org.hamcrest.core.IsEqual.equalTo; 6import org.hamcrest.Description; 7import org.hamcrest.Matcher; 8import org.hamcrest.Factory; 9import org.hamcrest.BaseMatcher; 10 11 12/** 13 * Calculates the logical negation of a matcher. 14 */ 15public class IsNot<T> extends BaseMatcher<T> { 16 private final Matcher<T> matcher; 17 18 public IsNot(Matcher<T> matcher) { 19 this.matcher = matcher; 20 } 21 22 public boolean matches(Object arg) { 23 return !matcher.matches(arg); 24 } 25 26 public void describeTo(Description description) { 27 description.appendText("not ").appendDescriptionOf(matcher); 28 } 29 30 /** 31 * Inverts the rule. 32 */ 33 @Factory 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 45 public static <T> Matcher<T> not(T value) { 46 return not(equalTo(value)); 47 } 48 49} 50