11ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabotpackage org.hamcrest.core;
21ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot
31ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabotimport static org.hamcrest.core.IsInstanceOf.instanceOf;
41ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabotimport static org.hamcrest.core.IsEqual.equalTo;
51ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabotimport org.hamcrest.Factory;
61ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabotimport org.hamcrest.Matcher;
71ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabotimport org.hamcrest.BaseMatcher;
81ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabotimport org.hamcrest.Description;
91ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot
101ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot/**
111ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot * Decorates another Matcher, retaining the behavior but allowing tests
121ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot * to be slightly more expressive.
131ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot *
141ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot * eg. assertThat(cheese, equalTo(smelly))
151ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot * vs  assertThat(cheese, is(equalTo(smelly)))
161ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot */
171ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabotpublic class Is<T> extends BaseMatcher<T> {
181ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot
191ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    private final Matcher<T> matcher;
201ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot
211ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    public Is(Matcher<T> matcher) {
221ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot        this.matcher = matcher;
231ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    }
241ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot
251ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    public boolean matches(Object arg) {
261ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot        return matcher.matches(arg);
271ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    }
281ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot
291ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    public void describeTo(Description description) {
301ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot        description.appendText("is ").appendDescriptionOf(matcher);
311ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    }
321ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot
331ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    /**
341ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot     * Decorates another Matcher, retaining the behavior but allowing tests
351ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot     * to be slightly more expressive.
361ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot     *
371ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot     * eg. assertThat(cheese, equalTo(smelly))
381ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot     * vs  assertThat(cheese, is(equalTo(smelly)))
391ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot     */
401ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    @Factory
411ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    public static <T> Matcher<T> is(Matcher<T> matcher) {
421ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot        return new Is<T>(matcher);
431ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    }
441ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot
451ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    /**
461ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot     * This is a shortcut to the frequently used is(equalTo(x)).
471ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot     *
481ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot     * eg. assertThat(cheese, is(equalTo(smelly)))
491ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot     * vs  assertThat(cheese, is(smelly))
501ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot     */
511ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    @Factory
521ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    public static <T> Matcher<T> is(T value) {
531ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot        return is(equalTo(value));
541ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    }
551ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot
561ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    /**
571ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot     * This is a shortcut to the frequently used is(instanceOf(SomeClass.class)).
581ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot     *
591ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot     * eg. assertThat(cheese, is(instanceOf(Cheddar.class)))
601ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot     * vs  assertThat(cheese, is(Cheddar.class))
611ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot     */
621ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    @Factory
631ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    public static Matcher<Object> is(Class<?> type) {
641ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot        return is(instanceOf(type));
651ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot    }
661ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot
671ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot}
681ecfda91236a8970119144e59e0ba6113dc22c0fBrett Chabot
69