blob: e370fa01b9310071e24aa8fa96e88f6790dd0365 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
package bjc.utils.patterns;
import bjc.functypes.*;
/**
* A simple pattern matcher backed by a function.
*
* @author Ben Culkin
*
* @param <ReturnType> The type returned by the matcher.
* @param <InputType> The type to match against.
*/
public class FunctionalPatternMatcher<ReturnType, InputType>
implements PatternMatcher<ReturnType, InputType> {
private final ThrowFunction<InputType, ReturnType, NonExhaustiveMatch> matcher;
/**
* Create a new function-backed pattern matcher.
*
* @param matcher The function backing this matcher.
*/
public FunctionalPatternMatcher(ThrowFunction<InputType, ReturnType, NonExhaustiveMatch> matcher) {
this.matcher = matcher;
}
@Override
public ReturnType matchFor(InputType input) throws NonExhaustiveMatch {
return matcher.apply(input);
}
}
|