Skip to content

Instantly share code, notes, and snippets.

@dmcg
Last active August 29, 2015 14:10
Show Gist options
  • Save dmcg/8ddf275688fd450e977e to your computer and use it in GitHub Desktop.
Save dmcg/8ddf275688fd450e977e to your computer and use it in GitHub Desktop.
Hamcrest Matcher transformed by function
public class TransformingMatcher<U, T> extends TypeSafeMatcher<U> {
private final Matcher<T> base;
private final Function<? super U, ? extends T> function;
public TransformingMatcher(Matcher<T> base, Function<? super U, ? extends T> function) {
this.base = base;
this.function = function;
}
@Override
public void describeTo(Description description) {
description.appendText("transformed version of ");
base.describeTo(description);
}
@Override
protected boolean matchesSafely(U item) {
return base.matches(function.apply(item));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment