001 package org.hamcrest; 002 003 /** 004 * Utility class for writing one off matchers. 005 * For example: 006 * <pre> 007 * Matcher<String> aNonEmptyString = new CustomMatcher<String>("a non empty string") { 008 * public boolean matches(Object object) { 009 * return ((object instanceof String) && !((String) object).isEmpty(); 010 * } 011 * }; 012 * </pre> 013 * <p> 014 * This class is designed for scenarios where an anonymous inner class 015 * matcher makes sense. It should not be used by API designers implementing 016 * matchers. 017 * 018 * @author Neil Dunn 019 * @see CustomTypeSafeMatcher for a type safe variant of this class that you probably 020 * want to use. 021 * @param <T> The type of object being matched. 022 */ 023 public abstract class CustomMatcher<T> extends BaseMatcher<T> { 024 private final String fixedDescription; 025 026 public CustomMatcher(String description) { 027 if (description == null) { 028 throw new IllegalArgumentException("Description should be non null!"); 029 } 030 this.fixedDescription = description; 031 } 032 033 @Override 034 public final void describeTo(Description description) { 035 description.appendText(fixedDescription); 036 } 037 }