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/IPatternMatcher.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/IPatternMatcher.java')
| -rw-r--r-- | base/src/main/java/bjc/utils/patterns/IPatternMatcher.java | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/base/src/main/java/bjc/utils/patterns/IPatternMatcher.java b/base/src/main/java/bjc/utils/patterns/IPatternMatcher.java new file mode 100644 index 0000000..b688a47 --- /dev/null +++ b/base/src/main/java/bjc/utils/patterns/IPatternMatcher.java @@ -0,0 +1,85 @@ +package bjc.utils.patterns; + +import java.util.function.*; + +import bjc.functypes.*; + +/** + * Represents a pattern matcher against a series of patterns. + * + * @author Ben Culkin + * + * @param <ReturnType> The type returned from matching the patterns. + * @param <InputType> The type to match against. + */ +@FunctionalInterface +public interface IPatternMatcher<ReturnType, InputType> { + /** + * Match an input object against a set of patterns. + * + * @param input The object to match against. + * + * @return The result of matching against the object. + * + * @throws NonExhaustiveMatch If none of the patterns in this set match + */ + ReturnType matchFor(InputType input) throws NonExhaustiveMatch; + + /** + * Create a pattern matcher against a static set of patterns. + * + * @param <RetType> The type returned from matching the patterns. + * @param <InpType> The type to match against. + * + * @param patterns The set of patterns to match on. + * + * @return A pattern matcher which matches on the given patterns. + */ + @SafeVarargs + static <RetType, InpType> IPatternMatcher<RetType, InpType> matchingOn( + ComplexPattern<RetType, ?, InpType>... patterns) { + return new PatternMatcher<>(patterns); + } + + /** + * Create a pattern matcher from a handler function. + * + * @param <RetType> The type returned by the matcher. + * @param <InpType> The type to match against. + * + * @param handler The handler function. + * + * @return A pattern matcher defined by the given handler. + */ + static <RetType, InpType> IPatternMatcher<RetType, InpType> from( + ThrowFunction<InpType, RetType, NonExhaustiveMatch> handler) { + return new FunctionalPatternMatcher<>(handler); + } + + /** + * Create a pattern matcher which applies a transform to its input. + * + * @param <NewInput> The new input type to use. + * @param transformer The function to convert from the new input to the old input. + * + * @return A pattern matcher which takes values of the new type instead. + */ + default <NewInput> IPatternMatcher<ReturnType, NewInput> transformInput( + Function<NewInput, InputType> transformer) { + return from(inp -> matchFor(transformer.apply(inp))); + } + + /** + * Create a pattern matcher which applies a transform to its output. + * + * @param <NewOutput> The new output type to use. + * + * @param transformer The function to convert from the new output to the old output. + * + * @return A pattern matcher which takes values of the new type instead. + */ + default <NewOutput> IPatternMatcher<NewOutput, InputType> transformOutput( + Function<ReturnType, NewOutput> transformer) { + return from(inp -> transformer.apply(matchFor(inp))); + } +}
\ No newline at end of file |
