001 /* Copyright (c) 2000-2009 hamcrest.org 002 */ 003 package org.hamcrest.core; 004 005 import org.hamcrest.BaseMatcher; 006 import org.hamcrest.Description; 007 import org.hamcrest.Factory; 008 import org.hamcrest.Matcher; 009 010 import static org.hamcrest.core.IsEqual.equalTo; 011 012 013 /** 014 * Calculates the logical negation of a matcher. 015 */ 016 public 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 }