From 4c0f972c4616eb549a098c3ef40d527eb542d7a6 Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Sat, 21 Nov 2020 16:51:04 -0500 Subject: Add basic pattern matching Adds a basic pattern matching implementation. Not perfect, but pretty good, considering what we have to work with --- .../java/bjc/utils/patterns/IPatternMatcher.java | 85 ++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 base/src/main/java/bjc/utils/patterns/IPatternMatcher.java (limited to 'base/src/main/java/bjc/utils/patterns/IPatternMatcher.java') 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 The type returned from matching the patterns. + * @param The type to match against. + */ +@FunctionalInterface +public interface IPatternMatcher { + /** + * 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 The type returned from matching the patterns. + * @param 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 IPatternMatcher matchingOn( + ComplexPattern... patterns) { + return new PatternMatcher<>(patterns); + } + + /** + * Create a pattern matcher from a handler function. + * + * @param The type returned by the matcher. + * @param The type to match against. + * + * @param handler The handler function. + * + * @return A pattern matcher defined by the given handler. + */ + static IPatternMatcher from( + ThrowFunction handler) { + return new FunctionalPatternMatcher<>(handler); + } + + /** + * Create a pattern matcher which applies a transform to its input. + * + * @param 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 IPatternMatcher transformInput( + Function transformer) { + return from(inp -> matchFor(transformer.apply(inp))); + } + + /** + * Create a pattern matcher which applies a transform to its output. + * + * @param 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 IPatternMatcher transformOutput( + Function transformer) { + return from(inp -> transformer.apply(matchFor(inp))); + } +} \ No newline at end of file -- cgit v1.2.3