Skip to content

Commit 3180783

Browse files
committed
Using FunctionMatcher for files
1 parent 2830455 commit 3180783

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

hamcrest-core/src/main/java/org/hamcrest/FunctionMatcher.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
* @param <U> The type of the feature to be matched
1212
*/
1313
public class FunctionMatcher<T, U> extends TypeSafeDiagnosingMatcher<T> {
14-
public static interface Feature<T, U> {
15-
U from(T actual);
14+
public static interface Feature<ActualType, FeatureType> {
15+
FeatureType from(ActualType actual);
1616
}
1717

1818

@@ -30,8 +30,8 @@ public static interface Feature<T, U> {
3030
* @param feature A function to extra the value to be matched against.
3131
*/
3232
public FunctionMatcher(String featureDescription, String featureName,
33-
Matcher<? super U> subMatcher,
34-
Feature<T, U> feature) {
33+
Matcher<? super U> subMatcher, Feature<T, U> feature)
34+
{
3535
super(TYPE_FINDER.findExpectedType(feature.getClass()));
3636
this.subMatcher = subMatcher;
3737
this.featureDescription = featureDescription;

hamcrest-library/src/main/java/org/hamcrest/io/FileMatchers.java

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

33
import org.hamcrest.Description;
4-
import org.hamcrest.FeatureMatcher;
4+
import org.hamcrest.FunctionMatcher;
5+
import org.hamcrest.FunctionMatcher.Feature;
56
import org.hamcrest.Matcher;
67
import org.hamcrest.TypeSafeDiagnosingMatcher;
78

@@ -37,33 +38,20 @@ public static Matcher<File> aFileWithSize(long size) {
3738
}
3839

3940
public static Matcher<File> aFileWithSize(final Matcher<Long> expected) {
40-
return new FeatureMatcher<File, Long>(expected, "A file with size", "size") {
41-
@Override protected Long featureValueOf(File actual) { return actual.length(); }
42-
};
41+
return new FunctionMatcher<>("A file with size", "size", expected, LENGTH);
4342
}
4443

4544
public static Matcher<File> aFileNamed(final Matcher<String> expected) {
46-
return new FeatureMatcher<File, String>(expected, "A file with name", "name") {
47-
@Override protected String featureValueOf(File actual) { return actual.getName(); }
48-
};
45+
return new FunctionMatcher<>("A file with name", "name", expected, NAME);
4946
}
5047

5148
public static Matcher<File> aFileWithCanonicalPath(final Matcher<String> expected) {
52-
return new FeatureMatcher<File, String>(expected, "A file with canonical path", "path") {
53-
@Override protected String featureValueOf(File actual) {
54-
try {
55-
return actual.getCanonicalPath();
56-
} catch (IOException e) {
57-
return "Exception: " + e.getMessage();
58-
}
59-
}
60-
};
49+
return new FunctionMatcher<>("A file with canonical path", "path", expected, CANONICAL_PATH);
6150
}
6251

52+
6353
public static Matcher<File> aFileWithAbsolutePath(final Matcher<String> expected) {
64-
return new FeatureMatcher<File, String>(expected, "A file with absolute path", "path") {
65-
@Override protected String featureValueOf(File actual) { return actual.getAbsolutePath(); }
66-
};
54+
return new FunctionMatcher<>("A file with absolute path", "path", expected, ABSOLUTE_PATH);
6755
}
6856

6957
public static interface FileStatus {
@@ -104,4 +92,25 @@ public void describeTo(Description description) {
10492
}
10593
};
10694
}
95+
96+
private static final Feature<File, String> ABSOLUTE_PATH = new Feature<File, String>() {
97+
@Override public String from(File actual) { return actual.getAbsolutePath(); }
98+
};
99+
public static final Feature<File, Long> LENGTH = new Feature<File, Long>() {
100+
@Override public Long from(File actual) { return actual.length(); }
101+
};
102+
103+
public static final Feature<File, String> NAME = new Feature<File, String>() {
104+
@Override public String from(File actual) { return actual.getName(); }
105+
};
106+
107+
public static final Feature<File, String> CANONICAL_PATH = new Feature<File, String>() {
108+
@Override public String from(File actual) {
109+
try {
110+
return actual.getCanonicalPath();
111+
} catch (IOException e) {
112+
return "Exception: " + e.getMessage();
113+
}
114+
}
115+
};
107116
}

0 commit comments

Comments
 (0)