aboutsummaryrefslogtreecommitdiff
path: root/hamcrest-core/src/main/java/org/hamcrest/CustomMatcher.java
diff options
context:
space:
mode:
Diffstat (limited to 'hamcrest-core/src/main/java/org/hamcrest/CustomMatcher.java')
-rw-r--r--hamcrest-core/src/main/java/org/hamcrest/CustomMatcher.java37
1 files changed, 37 insertions, 0 deletions
diff --git a/hamcrest-core/src/main/java/org/hamcrest/CustomMatcher.java b/hamcrest-core/src/main/java/org/hamcrest/CustomMatcher.java
new file mode 100644
index 0000000..036a764
--- /dev/null
+++ b/hamcrest-core/src/main/java/org/hamcrest/CustomMatcher.java
@@ -0,0 +1,37 @@
+package org.hamcrest;
+
+/**
+ * Utility class for writing one off matchers.
+ * For example:
+ * <pre>
+ * Matcher&lt;String&gt; aNonEmptyString = new CustomMatcher&lt;String&gt;("a non empty string") {
+ * public boolean matches(Object object) {
+ * return ((object instanceof String) &amp;&amp; !((String) object).isEmpty();
+ * }
+ * };
+ * </pre>
+ * <p>
+ * This class is designed for scenarios where an anonymous inner class
+ * matcher makes sense. It should not be used by API designers implementing
+ * matchers.
+ *
+ * @author Neil Dunn
+ * @see CustomTypeSafeMatcher for a type safe variant of this class that you probably
+ * want to use.
+ * @param <T> The type of object being matched.
+ */
+public abstract class CustomMatcher<T> extends BaseMatcher<T> {
+ private final String fixedDescription;
+
+ public CustomMatcher(String description) {
+ if (description == null) {
+ throw new IllegalArgumentException("Description should be non null!");
+ }
+ this.fixedDescription = description;
+ }
+
+ @Override
+ public final void describeTo(Description description) {
+ description.appendText(fixedDescription);
+ }
+}