Skip to content

Instantly share code, notes, and snippets.

@MaZderMind
Forked from GuiSim/LambdaMatcher.java
Created July 3, 2017 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MaZderMind/186ea38b0b56379b423b54034070f18f to your computer and use it in GitHub Desktop.
Save MaZderMind/186ea38b0b56379b423b54034070f18f to your computer and use it in GitHub Desktop.
Hamcrest matcher that matches a Lambda.
package util.matcher;
import java.util.function.Function;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
public class LambdaMatcher<T> extends BaseMatcher<T> {
private final Function<T, Boolean> matcher;
private final String description;
public LambdaMatcher(Function<T, Boolean> matcher,
String description) {
this.matcher = matcher;
this.description = description;
}
public LambdaMatcher(Function<T, Boolean> matcher) {
this(matcher, "matches lambda");
}
public static <T> LambdaMatcher<T> matchesLambda(Function<T, Boolean> matcher) {
return new LambdaMatcher<T>(matcher);
}
public static <T> LambdaMatcher<T> matchesLambda(Function<T, Boolean> matcher, String description) {
return new LambdaMatcher<T>(matcher, description);
}
@Override
public boolean matches(Object argument) {
return matcher.apply((T) argument);
}
@Override
public void describeTo(Description description) {
description.appendText(this.description);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment