diff options
| author | Ben Culkin <scorpress@gmail.com> | 2020-11-21 16:51:04 -0500 |
|---|---|---|
| committer | Ben Culkin <scorpress@gmail.com> | 2020-11-21 16:51:04 -0500 |
| commit | 4c0f972c4616eb549a098c3ef40d527eb542d7a6 (patch) | |
| tree | f791838e7477bb15fe2181bf673231e2e4ccda0a /base/src/main/java/bjc/utils/patterns/FunctionalPatternMatcher.java | |
| parent | aaf35ffcea677d315aa24180f2742a45e2146ece (diff) | |
Add basic pattern matching
Adds a basic pattern matching implementation. Not perfect, but pretty
good, considering what we have to work with
Diffstat (limited to 'base/src/main/java/bjc/utils/patterns/FunctionalPatternMatcher.java')
| -rw-r--r-- | base/src/main/java/bjc/utils/patterns/FunctionalPatternMatcher.java | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/base/src/main/java/bjc/utils/patterns/FunctionalPatternMatcher.java b/base/src/main/java/bjc/utils/patterns/FunctionalPatternMatcher.java new file mode 100644 index 0000000..5a214d3 --- /dev/null +++ b/base/src/main/java/bjc/utils/patterns/FunctionalPatternMatcher.java @@ -0,0 +1,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); + } +} |
