Top Description Fields Constructors Methods
org.hamcrest

public abstract Class TypeSafeMatcher<T>

extends BaseMatcher<T>
Class Inheritance
Known Direct Subclasses
org.hamcrest.beans.HasProperty, org.hamcrest.collection.ArrayAsIterableMatcher, org.hamcrest.collection.HasItemInArray, org.hamcrest.collection.IsArray, org.hamcrest.collection.IsArrayContainingInAnyOrder, org.hamcrest.collection.IsArrayContainingInOrder, org.hamcrest.collection.IsEmptyCollection, org.hamcrest.collection.IsEmptyIterable, org.hamcrest.collection.IsMapContaining, org.hamcrest.comparator.ComparatorMatcherBuilder.ComparatorMatcher, org.hamcrest.core.SubstringMatcher, org.hamcrest.number.BigDecimalCloseTo, org.hamcrest.number.IsCloseTo, org.hamcrest.number.IsNaN, org.hamcrest.object.IsCompatibleType, org.hamcrest.text.IsBlankString, org.hamcrest.text.IsEmptyString, org.hamcrest.text.IsEqualCompressingWhiteSpace, org.hamcrest.text.IsEqualIgnoringCase, org.hamcrest.text.MatchesPattern, org.hamcrest.text.StringContainsInOrder, org.hamcrest.CustomTypeSafeMatcher
Imports
org.hamcrest.internal.ReflectiveTypeFinder

Convenient base class for Matchers that require a non-null value of a specific type. This simply implements the null check, checks the type and then casts.
Authors
Joe Walnes, Steve Freeman, Nat Pryce

Field Summary

Modifier and TypeField and Description
private final Class<?>
private static final ReflectiveTypeFinder

Constructor Summary

AccessConstructor and Description
protected
TypeSafeMatcher()

The default constructor for simple sub types

protected
TypeSafeMatcher(Class<?>
The expectedType of the actual value.
expectedType
)

Use this constructor if the subclass that implements matchesSafely is not the class that binds <T> to a type.

protected
TypeSafeMatcher(ReflectiveTypeFinder
A type finder to extract the type
typeFinder
)

Use this constructor if the subclass that implements matchesSafely is not the class that binds <T> to a type.

Method Summary

Modifier and TypeMethod and Description
public final void
describeMismatch(Object
The item that the Matcher has rejected.
item
,
Description
The description to be built or appended to.
description
)

Overrides org.hamcrest.BaseMatcher.describeMismatch.

Implements org.hamcrest.Matcher.describeMismatch.

Generate a description of why the matcher has not accepted the item.
protected void
describeMismatchSafely(T item, Description mismatchDescription)

Subclasses should override this.

public final boolean
matches(Object
the object against which the matcher is evaluated.
item
)

Implements org.hamcrest.Matcher.matches.

Methods made final to prevent accidental override.
protected abstract boolean
matchesSafely(T item)

Subclasses should implement this.

Inherited from org.hamcrest.BaseMatcher:
_dont_implement_Matcher___instead_extend_BaseMatcher_isNotNulltoString

Field Detail

expectedTypeback to summary
private final Class<?> expectedType
TYPE_FINDERback to summary
private static final ReflectiveTypeFinder TYPE_FINDER

Constructor Detail

TypeSafeMatcherback to summary
protected TypeSafeMatcher()

The default constructor for simple sub types

TypeSafeMatcherback to summary
protected TypeSafeMatcher(Class<?> expectedType)

Use this constructor if the subclass that implements matchesSafely is not the class that binds <T> to a type.

Parameters
expectedType:Class<?>

The expectedType of the actual value.

TypeSafeMatcherback to summary
protected TypeSafeMatcher(ReflectiveTypeFinder typeFinder)

Use this constructor if the subclass that implements matchesSafely is not the class that binds <T> to a type.

Parameters
typeFinder:ReflectiveTypeFinder

A type finder to extract the type

Method Detail

describeMismatchback to summary
public final void describeMismatch(Object item, Description description)

Overrides org.hamcrest.BaseMatcher.describeMismatch.

Implements org.hamcrest.Matcher.describeMismatch.

Doc from org.hamcrest.Matcher.describeMismatch.

Generate a description of why the matcher has not accepted the item. The description will be part of a larger description of why a matching failed, so it should be concise. This method assumes that matches(item) is false, but will not check this.

Parameters
item:Object

The item that the Matcher has rejected.

description:Description

The description to be built or appended to.

Annotations
@SuppressWarnings:unchecked
@Override
describeMismatchSafelyback to summary
protected void describeMismatchSafely(T item, Description mismatchDescription)

Subclasses should override this. The item will already have been checked for the specific type and will never be null.

matchesback to summary
public final boolean matches(Object item)

Implements org.hamcrest.Matcher.matches.

Methods made final to prevent accidental override. If you need to override this, there's no point on extending TypeSafeMatcher. Instead, extend the BaseMatcher.

Parameters
item:Object

Doc from org.hamcrest.Matcher.matches.

the object against which the matcher is evaluated.

Returns:boolean

Doc from org.hamcrest.Matcher.matches.

true if item matches, otherwise false.

Annotations
@Override
@SuppressWarnings:unchecked
matchesSafelyback to summary
protected abstract boolean matchesSafely(T item)

Subclasses should implement this. The item will already have been checked for the specific type and will never be null.