001    package org.hamcrest;
002    
003    
004    /**
005     * Utility class for writing one off matchers.
006     * For example:
007     * <pre>
008     * Matcher&lt;String&gt; aNonEmptyString = new CustomTypeSafeMatcher&lt;String&gt;("a non empty string") {
009     *   public boolean matchesSafely(String string) {
010     *     return !string.isEmpty();
011     *   }
012     *   public void describeMismatchSafely(String string, Description mismatchDescription) {
013     *     mismatchDescription.appendText("was empty");
014     *   }
015     * };
016     * </pre>
017     * This is a variant of {@link CustomMatcher} that first type checks
018     * the argument being matched. By the time {@link TypeSafeMatcher#matchesSafely} is
019     * is called the argument is guaranteed to be non-null and of the correct
020     * type.
021     *
022     * @author Neil Dunn
023     * @param <T> The type of object being matched
024     */
025    public abstract class CustomTypeSafeMatcher<T> extends TypeSafeMatcher<T> {
026        private final String fixedDescription;
027    
028        public CustomTypeSafeMatcher(String description) {
029            if (description == null) {
030                throw new IllegalArgumentException("Description must be non null!");
031            }
032            this.fixedDescription = description;
033        }
034    
035        @Override
036        public final void describeTo(Description description) {
037            description.appendText(fixedDescription);
038        }
039    }