Skip to content

Commit 635a412

Browse files
committed
Started function matcher
1 parent 23d721d commit 635a412

File tree

3 files changed

+71
-7
lines changed

3 files changed

+71
-7
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.hamcrest;
2+
3+
import org.hamcrest.internal.ReflectiveTypeFinder;
4+
5+
/**
6+
* Supporting class for matching a feature of an object. Uses a given function to extract
7+
* the feature to be matched against.
8+
* In preparation for Java 8.
9+
*
10+
* @param <T> The type of the object to be matched
11+
* @param <U> The type of the feature to be matched
12+
*/
13+
public class FunctionMatcher<T, U> extends TypeSafeDiagnosingMatcher<T> {
14+
public static interface Feature<T, U> {
15+
U from(T actual);
16+
}
17+
18+
19+
private static final ReflectiveTypeFinder TYPE_FINDER = new ReflectiveTypeFinder("featureValueOf", 1, 0);
20+
private final Matcher<? super U> subMatcher;
21+
private final String featureDescription;
22+
private final String featureName;
23+
private final Feature<T, U> feature;
24+
25+
/**
26+
* Constructor
27+
* @param featureDescription Descriptive text to use in describeTo
28+
* @param featureName Identifying text for mismatch message
29+
* @param subMatcher The matcher to apply to the feature
30+
* @param feature A function to extra the value to be matched against.
31+
*/
32+
public FunctionMatcher(String featureDescription, String featureName,
33+
Matcher<? super U> subMatcher,
34+
Feature<T, U> feature) {
35+
super(TYPE_FINDER);
36+
this.subMatcher = subMatcher;
37+
this.featureDescription = featureDescription;
38+
this.featureName = featureName;
39+
this.feature = feature;
40+
}
41+
42+
43+
@Override
44+
protected boolean matchesSafely(T actual, Description mismatch) {
45+
final U featureValue = feature.from(actual);
46+
if (subMatcher.matches(featureValue)) {
47+
return true;
48+
}
49+
50+
mismatch.appendText(featureName).appendText(" ");
51+
subMatcher.describeMismatch(featureValue, mismatch);
52+
return false;
53+
}
54+
55+
@Override
56+
public final void describeTo(Description description) {
57+
description.appendText(featureDescription).appendText(" ")
58+
.appendDescriptionOf(subMatcher);
59+
}
60+
}

hamcrest-library/src/main/java/org/hamcrest/Matchers.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ public static <E> org.hamcrest.Matcher<E[]> arrayContaining(java.util.List<org.h
577577
*/
578578
@SafeVarargs
579579
public static <E> org.hamcrest.Matcher<E[]> arrayContainingInAnyOrder(org.hamcrest.Matcher<? super E>... itemMatchers) {
580-
return org.hamcrest.collection.IsArrayContainingInAnyOrder.arrayContainingInAnyOrder(itemMatchers);
580+
return MatchArrays.arrayContainingInAnyOrder(itemMatchers);
581581
}
582582

583583
/**
@@ -600,7 +600,7 @@ public static <E> org.hamcrest.Matcher<E[]> arrayContainingInAnyOrder(org.hamcre
600600
* @param itemMatchers a list of matchers, each of which must be satisfied by an item provided by an examined array
601601
*/
602602
public static <E> org.hamcrest.Matcher<E[]> arrayContainingInAnyOrder(java.util.Collection<org.hamcrest.Matcher<? super E>> itemMatchers) {
603-
return org.hamcrest.collection.IsArrayContainingInAnyOrder.arrayContainingInAnyOrder(itemMatchers);
603+
return MatchArrays.arrayContainingInAnyOrder(itemMatchers);
604604
}
605605

606606
/**
@@ -622,7 +622,7 @@ public static <E> org.hamcrest.Matcher<E[]> arrayContainingInAnyOrder(java.util.
622622
*/
623623
@SafeVarargs
624624
public static <E> org.hamcrest.Matcher<E[]> arrayContainingInAnyOrder(E... items) {
625-
return org.hamcrest.collection.IsArrayContainingInAnyOrder.arrayContainingInAnyOrder(items);
625+
return MatchArrays.arrayContainingInAnyOrder(items);
626626
}
627627

628628
/**

hamcrest-library/src/main/java/org/hamcrest/collection/MatchArrays.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.hamcrest.collection;
22

3-
import org.hamcrest.FeatureMatcher;
3+
import org.hamcrest.FunctionMatcher;
4+
import org.hamcrest.FunctionMatcher.Feature;
45
import org.hamcrest.Matcher;
56

67
import java.util.Collection;
@@ -168,9 +169,12 @@ public static <E> Matcher<E[]> arrayContainingInAnyOrder(E... items) {
168169
* @param sizeMatcher a matcher for the length of an examined array
169170
*/
170171
public static <E> Matcher<E[]> arrayWithSize(Matcher<? super Integer> sizeMatcher) {
171-
return new FeatureMatcher<E[], Integer> (sizeMatcher, "an array with size","array size") {
172-
@Override protected Integer featureValueOf(E[] actual) { return actual.length; }
173-
};
172+
return new FunctionMatcher<>(
173+
"an array with size", "array size",
174+
sizeMatcher,
175+
new Feature<E[], Integer>() {
176+
@Override public Integer from(E[] actual) { return actual.length; }
177+
});
174178
}
175179

176180
/**

0 commit comments

Comments
 (0)