package org.hamcrest.object; import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.TypeSafeMatcher; public class IsCompatibleType extends TypeSafeMatcher> { private final Class type; public IsCompatibleType(Class type) { this.type = type; } @Override public boolean matchesSafely(Class cls) { return type.isAssignableFrom(cls); } @Override public void describeMismatchSafely(Class cls, Description mismatchDescription) { mismatchDescription.appendValue(cls.getName()); } @Override public void describeTo(Description description) { description.appendText("type < ").appendText(type.getName()); } /** * Creates a matcher of {@link Class} that matches when the specified baseType is * assignable from the examined class. * For example: *
assertThat(Integer.class, typeCompatibleWith(Number.class))
* * @param baseType * the base class to examine classes against */ public static Matcher> typeCompatibleWith(Class baseType) { return new IsCompatibleType(baseType); } }