Skip to content

Commit 88be713

Browse files
author
joeretro
committed
Added abstract BaseMatcher class that all Matchers should extend. This allows for future API compatability as the Matcher interface evolves.
1 parent 69160fd commit 88be713

16 files changed

Lines changed: 68 additions & 22 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* Copyright (c) 2000-2006 hamcrest.org
2+
*/
3+
package org.hamcrest;
4+
5+
/**
6+
* BaseClass for all Matcher implementations.
7+
*
8+
* @see Matcher
9+
*/
10+
public abstract class BaseMatcher<T> implements Matcher<T> {
11+
12+
/**
13+
* @see Matcher#_dont_implement_Matcher___instead_extend_BaseMatcher_()
14+
*/
15+
public final void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
16+
// See Matcher interface for an explanation of this method.
17+
}
18+
19+
}

‎src/api/org/hamcrest/Matcher.java‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@
33
package org.hamcrest;
44

55
/**
6-
* A matcher over acceptable values.
6+
* A matcher over acceptable values.
77
* A matcher is able to describe itself to give feedback when it fails.
8+
* <p/>
9+
* <p/>
10+
* Matcher implementations should <b>NOT directly implement this interface</b>.
11+
* Instead, <b>extend</b> the <b>BaseMatcher</b> abstract class, which will ensure that
12+
* the Matcher API can grow to support new features and remain compatible with all
13+
* Matcher implementations.
14+
*
15+
* @see BaseMatcher
816
*/
9-
@SuppressWarnings("unused")
17+
@SuppressWarnings({"unused", "UnusedDeclaration"})
1018
public interface Matcher<T> extends SelfDescribing {
1119

1220
/**
@@ -16,5 +24,13 @@ public interface Matcher<T> extends SelfDescribing {
1624
* @return <code>true</code> if <var>item</var> matches, otherwise <code>false</code>.
1725
*/
1826
boolean matches(Object item);
19-
27+
28+
/**
29+
* This method simply acts a friendly reminder not to implement Matcher directly and
30+
* instead extend BaseMatcher. It's easy to ignore JavaDoc, but a bit harder to ignore
31+
* compile errors :).
32+
*
33+
* @see Matcher for reasons why.
34+
*/
35+
void _dont_implement_Matcher___instead_extend_BaseMatcher_();
2036
}

‎src/library/org/hamcrest/TypeSafeMatcher.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
* @author Joe Walnes
1010
*/
11-
public abstract class TypeSafeMatcher<T> implements Matcher<T> {
11+
public abstract class TypeSafeMatcher<T> extends BaseMatcher<T> {
1212

1313
private Class expectedType;
1414

‎src/library/org/hamcrest/core/Always.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
import org.hamcrest.Description;
66
import org.hamcrest.Matcher;
77
import org.hamcrest.Factory;
8+
import org.hamcrest.BaseMatcher;
89

910
/**
1011
* Always passes or failes, ignoring what value is passed to it.
1112
*/
12-
public class Always<T> implements Matcher<T> {
13+
public class Always<T> extends BaseMatcher<T> {
1314

1415
private final boolean pass;
1516
private final String description;

‎src/library/org/hamcrest/core/And.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.hamcrest.Description;
66
import org.hamcrest.Matcher;
77
import org.hamcrest.Factory;
8+
import org.hamcrest.BaseMatcher;
89

910
import java.util.Arrays;
1011

@@ -14,7 +15,7 @@
1415
* shortcut, so that the second matcher is not called if the first
1516
* matcher returns <code>false</code>.
1617
*/
17-
public class And<T> implements Matcher<T> {
18+
public class And<T> extends BaseMatcher<T> {
1819
private final Iterable<Matcher<T>> matchers;
1920

2021
public And(Iterable<Matcher<T>> matchers) {

‎src/library/org/hamcrest/core/DescribedAs.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
import org.hamcrest.Description;
88
import org.hamcrest.Matcher;
99
import org.hamcrest.Factory;
10+
import org.hamcrest.BaseMatcher;
1011

1112
/**
1213
* Provides a custom description to another matcher.
1314
*/
14-
public class DescribedAs<T> implements Matcher<T> {
15+
public class DescribedAs<T> extends BaseMatcher<T> {
1516
private final String descriptionTemplate;
1617
private final Matcher<T> matcher;
1718
private final Object[] values;

‎src/library/org/hamcrest/core/IsAnything.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
import org.hamcrest.Description;
66
import org.hamcrest.Matcher;
77
import org.hamcrest.Factory;
8+
import org.hamcrest.BaseMatcher;
89

910

1011
/**
1112
* A matcher that always returns <code>true</code>.
1213
*/
13-
public class IsAnything<T> implements Matcher<T> {
14+
public class IsAnything<T> extends BaseMatcher<T> {
1415

1516
private final String description;
1617

‎src/library/org/hamcrest/core/IsEqual.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.hamcrest.Description;
66
import org.hamcrest.Matcher;
77
import org.hamcrest.Factory;
8+
import org.hamcrest.BaseMatcher;
89

910
import java.lang.reflect.Array;
1011

@@ -13,7 +14,7 @@
1314
* Is the value equal to another value, as tested by the
1415
* {@link java.lang.Object#equals} invokedMethod?
1516
*/
16-
public class IsEqual<T> implements Matcher<T> {
17+
public class IsEqual<T> extends BaseMatcher<T> {
1718
private final Object object;
1819

1920
public IsEqual(T equalArg) {

‎src/library/org/hamcrest/core/IsInstanceOf.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
import org.hamcrest.Description;
66
import org.hamcrest.Matcher;
77
import org.hamcrest.Factory;
8+
import org.hamcrest.BaseMatcher;
89

910

1011
/**
1112
* Tests whether the value is an instance of a class.
1213
*/
13-
public class IsInstanceOf<T> implements Matcher<T> {
14+
public class IsInstanceOf<T> extends BaseMatcher<T> {
1415
private final Class<T> theClass;
1516

1617
/**

‎src/library/org/hamcrest/core/IsNot.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
import org.hamcrest.Description;
66
import org.hamcrest.Matcher;
77
import org.hamcrest.Factory;
8+
import org.hamcrest.BaseMatcher;
89

910

1011
/**
1112
* Calculates the logical negation of a matcher.
1213
*/
13-
public class IsNot<T> implements Matcher<T> {
14+
public class IsNot<T> extends BaseMatcher<T> {
1415
private final Matcher<T> matcher;
1516

1617
public IsNot(Matcher<T> matcher) {

0 commit comments

Comments
 (0)