Skip to content

Commit 05e35f2

Browse files
author
joeretro
committed
Changed Matcher interface from match(T) to match(Object). This moves the responsibility for determining if the value is the right type down into the Matcher implementations.
Introduced TypeSafeMatcher base class, which matchers that can only work with a specific type can extend.
1 parent 64218a6 commit 05e35f2

30 files changed

Lines changed: 94 additions & 77 deletions

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
* A matcher over acceptable values.
77
* A matcher is able to describe itself to give feedback when it fails.
88
*/
9+
@SuppressWarnings({"UnusedDeclaration"})
910
public interface Matcher<T> extends SelfDescribing {
1011
/**
1112
* Evaluates the matcher for argument <var>item</var>.
1213
*
1314
* @param item the object against which the matcher is evaluated.
1415
* @return <code>true</code> if <var>item</var> matches, otherwise <code>false</code>.
1516
*/
16-
boolean match(T item);
17+
boolean match(Object item);
1718
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ public class Matchers {
88
* Temporary Matcher... don't use this.. it will go away!
99
*/
1010
public static Matcher<String> isTwoXs() {
11-
return new Matcher<String>() {
12-
public boolean match(String string) {
11+
return new TypeSafeMatcher<String>() {
12+
public boolean matchSafely(String string) {
1313
return string.equals("xx");
1414
}
1515

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.hamcrest;
2+
3+
/**
4+
* Convenient base class for Matchers that require a non-null value of a specific type.
5+
* This simply implements the null check, checks the type and then casts.
6+
*
7+
* @author Joe Walnes
8+
*/
9+
public abstract class TypeSafeMatcher<T> implements Matcher<T> {
10+
11+
/**
12+
* Subclasses should implement this. The item will already have been checked for
13+
* the specific type and will never be null.
14+
*/
15+
protected abstract boolean matchSafely(T item);
16+
17+
/**
18+
* Method made final to prevent accidental override.
19+
* If you need to override this, there's no point on extending TypeSafeMatcher.
20+
* Instead, implement the Matcher interface directly.
21+
*/
22+
@SuppressWarnings({"unchecked"})
23+
public final boolean match(Object item) {
24+
return item != null && matchSafely((T) item);
25+
}
26+
27+
}

‎src/library/org/hamcrest/beans/HasProperty.java‎

Lines changed: 3 additions & 3 deletions
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.TypeSafeMatcher;
89

910
import java.beans.IntrospectionException;
1011

@@ -16,17 +17,16 @@
1617
* @author Iain McGinniss
1718
* @author Nat Pryce
1819
* @author Steve Freeman
19-
* @since 1.1.0
2020
*/
21-
public class HasProperty<T> implements Matcher<T> {
21+
public class HasProperty<T> extends TypeSafeMatcher<T> {
2222

2323
private final String propertyName;
2424

2525
public HasProperty(String propertyName) {
2626
this.propertyName = propertyName;
2727
}
2828

29-
public boolean match(T obj) {
29+
public boolean matchSafely(T obj) {
3030
try {
3131
return PropertyUtil.getPropertyDescriptor(propertyName, obj) != null;
3232
} catch (IntrospectionException e) {

‎src/library/org/hamcrest/beans/HasPropertyWithValue.java‎

Lines changed: 3 additions & 4 deletions
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.TypeSafeMatcher;
89

910
import java.beans.IntrospectionException;
1011
import java.beans.PropertyDescriptor;
@@ -63,9 +64,8 @@
6364
* @author Iain McGinniss
6465
* @author Nat Pryce
6566
* @author Steve Freeman
66-
* @since 1.1.0
6767
*/
68-
public class HasPropertyWithValue<T> implements Matcher<T> {
68+
public class HasPropertyWithValue<T> extends TypeSafeMatcher<T> {
6969

7070
private static final Object[] NO_ARGUMENTS = new Object[0];
7171

@@ -77,8 +77,7 @@ public HasPropertyWithValue(String propertyName, Matcher value) {
7777
this.value = value;
7878
}
7979

80-
@SuppressWarnings({"unchecked"})
81-
public boolean match(T argument) {
80+
public boolean matchSafely(T argument) {
8281
try {
8382
Method readMethod = getReadMethod(argument);
8483
return readMethod != null

‎src/library/org/hamcrest/beans/PropertyUtil.java‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@ public class PropertyUtil {
3030
*/
3131
public static PropertyDescriptor getPropertyDescriptor(String propertyName, Object fromObj) throws IntrospectionException {
3232
BeanInfo beanInfo = Introspector.getBeanInfo(fromObj.getClass());
33-
PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();
3433

35-
for (int i = 0; i < properties.length; i++) {
36-
if (properties[i].getName().equals(propertyName)) {
37-
return properties[i];
34+
for (PropertyDescriptor property : beanInfo.getPropertyDescriptors()) {
35+
if (property.getName().equals(propertyName)) {
36+
return property;
3837
}
3938
}
4039

‎src/library/org/hamcrest/collection/IsArray.java‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@
44

55
import org.hamcrest.Description;
66
import org.hamcrest.Matcher;
7+
import org.hamcrest.TypeSafeMatcher;
78

8-
public class IsArray<T> implements Matcher<T[]> {
9+
public class IsArray<T> extends TypeSafeMatcher<T[]> {
910
private final Matcher<T>[] elementMatchers;
1011

1112
public IsArray(Matcher<T>[] elementMatchers) {
1213
this.elementMatchers = elementMatchers.clone();
1314
}
1415

15-
public boolean match(T[] array) {
16-
if (array == null) return false;
17-
16+
public boolean matchSafely(T[] array) {
1817
if (array.length != elementMatchers.length) return false;
1918

2019
for (int i = 0; i < array.length; i++) {

‎src/library/org/hamcrest/collection/IsArrayContaining.java‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,23 @@
33
import org.hamcrest.Description;
44
import org.hamcrest.Matcher;
55
import org.hamcrest.Factory;
6+
import org.hamcrest.TypeSafeMatcher;
67
import static org.hamcrest.core.IsEqual.eq;
78

89
import java.lang.reflect.Array;
910

10-
public class IsArrayContaining<T> implements Matcher<T[]> {
11+
public class IsArrayContaining<T> extends TypeSafeMatcher<T[]> {
1112

1213
private final Matcher<T> elementMatcher;
1314

1415
public IsArrayContaining(Matcher<T> elementMatcher) {
1516
this.elementMatcher = elementMatcher;
1617
}
1718

18-
@SuppressWarnings({"unchecked"})
19-
public boolean match(T[] array) {
20-
if (array == null) {
21-
return false;
22-
}
19+
public boolean matchSafely(T[] array) {
2320
int size = Array.getLength(array);
2421
for (int i = 0; i < size; i++) {
25-
if (elementMatcher.match((T)Array.get(array, i))) {
22+
if (elementMatcher.match(Array.get(array, i))) {
2623
return true;
2724
}
2825
}

‎src/library/org/hamcrest/collection/IsCollectionContaining.java‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
import org.hamcrest.Description;
44
import org.hamcrest.Matcher;
55
import org.hamcrest.Factory;
6+
import org.hamcrest.TypeSafeMatcher;
67
import static org.hamcrest.core.IsEqual.eq;
78

89
import java.util.Collection;
910

10-
public class IsCollectionContaining<T> implements Matcher<Collection<T>> {
11+
public class IsCollectionContaining<T> extends TypeSafeMatcher<Collection<T>> {
1112
private final Matcher<T> elementMatcher;
1213
//Comparator<? super T> c
1314
//int binarySearch(List<? extends Comparable<? super T>> list, T key) {
@@ -16,10 +17,7 @@ public IsCollectionContaining(Matcher<T> elementMatcher) {
1617
this.elementMatcher = elementMatcher;
1718
}
1819

19-
public boolean match(Collection<T> collection) {
20-
if (collection == null) {
21-
return false;
22-
}
20+
public boolean matchSafely(Collection<T> collection) {
2321
for (T item : collection) {
2422
if (elementMatcher.match(item)){
2523
return true;

‎src/library/org/hamcrest/collection/IsMapContaining.java‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
import org.hamcrest.Description;
44
import org.hamcrest.Factory;
55
import org.hamcrest.Matcher;
6+
import org.hamcrest.TypeSafeMatcher;
67
import org.hamcrest.core.IsAnything;
78

89
import static org.hamcrest.core.IsEqual.eq;
910

1011
import java.util.Map;
1112
import java.util.Map.Entry;
1213

13-
public class IsMapContaining<K,V> implements Matcher<Map<K, V>> {
14+
public class IsMapContaining<K,V> extends TypeSafeMatcher<Map<K, V>> {
1415

1516
private final Matcher<K> keyMatcher;
1617
private final Matcher<V> valueMatcher;
@@ -20,10 +21,7 @@ public IsMapContaining(Matcher<K> keyMatcher, Matcher<V> valueMatcher) {
2021
this.valueMatcher = valueMatcher;
2122
}
2223

23-
public boolean match(Map<K, V> map) {
24-
if (map == null) {
25-
return false;
26-
}
24+
public boolean matchSafely(Map<K, V> map) {
2725
for (Entry<K, V> entry : map.entrySet()) {
2826
if (keyMatcher.match(entry.getKey()) && valueMatcher.match(entry.getValue())) {
2927
return true;

0 commit comments

Comments
 (0)