From f3814a84f8471684cd483347db4fb7b107c2e635 Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Thu, 3 Dec 2020 19:28:15 -0500 Subject: Rename interfaces to match Java style Rename several interfaces that were in the style IWhatever, which Java doesn't use --- .../java/bjc/utils/patterns/PatternMatcher.java | 96 ++++++++++++++++------ 1 file changed, 70 insertions(+), 26 deletions(-) (limited to 'base/src/main/java/bjc/utils/patterns/PatternMatcher.java') diff --git a/base/src/main/java/bjc/utils/patterns/PatternMatcher.java b/base/src/main/java/bjc/utils/patterns/PatternMatcher.java index f144d36..40bf42b 100644 --- a/base/src/main/java/bjc/utils/patterns/PatternMatcher.java +++ b/base/src/main/java/bjc/utils/patterns/PatternMatcher.java @@ -1,41 +1,85 @@ package bjc.utils.patterns; -import bjc.data.*; +import java.util.function.*; + +import bjc.functypes.*; /** - * Implements pattern-matching (of a sort) against a collection of patterns. + * Represents a pattern matcher against a series of patterns. * * @author Ben Culkin - * - * @param The type returned by the pattern. + * + * @param The type returned from matching the patterns. + * @param The type to match against. */ -public class PatternMatcher - implements IPatternMatcher { - private final ComplexPattern[] patterns; +@FunctionalInterface +public interface PatternMatcher { + /** + * 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 new pattern matcher. + * Create a pattern matcher against a static set of patterns. * - * @param patterns The set of patterns to match against. + * @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. */ - @SuppressWarnings("unchecked") @SafeVarargs - public PatternMatcher(ComplexPattern...patterns) { - // Note: this may seem a somewhat questionable cast, but because we never - // actually do anything with the value who has a type matching the second - // parameter, this should be safe - this.patterns = (ComplexPattern[]) patterns; + static PatternMatcher matchingOn( + ComplexPattern... patterns) { + return new SimplePatternMatcher<>(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 PatternMatcher 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 PatternMatcher transformInput( + Function transformer) { + return from(inp -> matchFor(transformer.apply(inp))); } - @Override - public ReturnType matchFor(InputType input) throws NonExhaustiveMatch { - for (ComplexPattern pattern : patterns) { - Pair matches = pattern.matches(input); - if (matches.getLeft()) { - pattern.apply(input, matches.getRight()); - } - } - - throw new NonExhaustiveMatch("Non-exhaustive match against " + input); + /** + * 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 PatternMatcher transformOutput( + Function transformer) { + return from(inp -> transformer.apply(matchFor(inp))); } -} +} \ No newline at end of file -- cgit v1.2.3