summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/patterns/FunctionalPatternMatcher.java
blob: 5a214d30d580ed88f7ff32ba7217851d7f3c7cb3 (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 IPatternMatcher<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);
	}
}