1package org.hamcrest.core; 2 3import static org.hamcrest.core.IsInstanceOf.instanceOf; 4import static org.hamcrest.core.IsEqual.equalTo; 5import org.hamcrest.Factory; 6import org.hamcrest.Matcher; 7import org.hamcrest.BaseMatcher; 8import org.hamcrest.Description; 9 10/** 11 * Decorates another Matcher, retaining the behavior but allowing tests 12 * to be slightly more expressive. 13 * 14 * eg. assertThat(cheese, equalTo(smelly)) 15 * vs assertThat(cheese, is(equalTo(smelly))) 16 */ 17public class Is<T> extends BaseMatcher<T> { 18 19 private final Matcher<T> matcher; 20 21 public Is(Matcher<T> matcher) { 22 this.matcher = matcher; 23 } 24 25 public boolean matches(Object arg) { 26 return matcher.matches(arg); 27 } 28 29 public void describeTo(Description description) { 30 description.appendText("is ").appendDescriptionOf(matcher); 31 } 32 33 /** 34 * Decorates another Matcher, retaining the behavior but allowing tests 35 * to be slightly more expressive. 36 * 37 * eg. assertThat(cheese, equalTo(smelly)) 38 * vs assertThat(cheese, is(equalTo(smelly))) 39 */ 40 @Factory 41 public static <T> Matcher<T> is(Matcher<T> matcher) { 42 return new Is<T>(matcher); 43 } 44 45 /** 46 * This is a shortcut to the frequently used is(equalTo(x)). 47 * 48 * eg. assertThat(cheese, is(equalTo(smelly))) 49 * vs assertThat(cheese, is(smelly)) 50 */ 51 @Factory 52 public static <T> Matcher<T> is(T value) { 53 return is(equalTo(value)); 54 } 55 56 /** 57 * This is a shortcut to the frequently used is(instanceOf(SomeClass.class)). 58 * 59 * eg. assertThat(cheese, is(instanceOf(Cheddar.class))) 60 * vs assertThat(cheese, is(Cheddar.class)) 61 */ 62 @Factory 63 public static Matcher<Object> is(Class<?> type) { 64 return is(instanceOf(type)); 65 } 66 67} 68 69