diff options
Diffstat (limited to 'base/src/main/java/bjc/utils/patterns')
5 files changed, 113 insertions, 113 deletions
diff --git a/base/src/main/java/bjc/utils/patterns/FunctionalPatternMatcher.java b/base/src/main/java/bjc/utils/patterns/FunctionalPatternMatcher.java index 5a214d3..e370fa0 100644 --- a/base/src/main/java/bjc/utils/patterns/FunctionalPatternMatcher.java +++ b/base/src/main/java/bjc/utils/patterns/FunctionalPatternMatcher.java @@ -11,7 +11,7 @@ import bjc.functypes.*; * @param <InputType> The type to match against. */ public class FunctionalPatternMatcher<ReturnType, InputType> - implements IPatternMatcher<ReturnType, InputType> { + implements PatternMatcher<ReturnType, InputType> { private final ThrowFunction<InputType, ReturnType, NonExhaustiveMatch> matcher; diff --git a/base/src/main/java/bjc/utils/patterns/IPatternMatcher.java b/base/src/main/java/bjc/utils/patterns/IPatternMatcher.java deleted file mode 100644 index b688a47..0000000 --- a/base/src/main/java/bjc/utils/patterns/IPatternMatcher.java +++ /dev/null @@ -1,85 +0,0 @@ -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 diff --git a/base/src/main/java/bjc/utils/patterns/MutablePatternMatcher.java b/base/src/main/java/bjc/utils/patterns/MutablePatternMatcher.java index 17de37a..28e9cd7 100644 --- a/base/src/main/java/bjc/utils/patterns/MutablePatternMatcher.java +++ b/base/src/main/java/bjc/utils/patterns/MutablePatternMatcher.java @@ -16,7 +16,7 @@ import bjc.data.*; * @param <InputType> The type of the input to match against. */ public class MutablePatternMatcher<ReturnType, InputType> - implements IPatternMatcher<ReturnType, InputType> { + implements PatternMatcher<ReturnType, InputType> { private final List<ComplexPattern<ReturnType, Object, InputType>> patterns; /** 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 <ReturnType> The type returned by the pattern. + * + * @param <ReturnType> The type returned from matching the patterns. + * @param <InputType> The type to match against. */ -public class PatternMatcher<ReturnType, InputType> - implements IPatternMatcher<ReturnType, InputType> { - private final ComplexPattern<ReturnType, Object, InputType>[] patterns; +@FunctionalInterface +public interface PatternMatcher<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 new pattern matcher. + * Create a pattern matcher against a static set of patterns. * - * @param patterns The set of patterns to match against. + * @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. */ - @SuppressWarnings("unchecked") @SafeVarargs - public PatternMatcher(ComplexPattern<ReturnType, ?, InputType>...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<ReturnType, Object, InputType>[]) patterns; + static <RetType, InpType> PatternMatcher<RetType, InpType> matchingOn( + ComplexPattern<RetType, ?, InpType>... patterns) { + return new SimplePatternMatcher<>(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> PatternMatcher<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> PatternMatcher<ReturnType, NewInput> transformInput( + Function<NewInput, InputType> transformer) { + return from(inp -> matchFor(transformer.apply(inp))); } - @Override - public ReturnType matchFor(InputType input) throws NonExhaustiveMatch { - for (ComplexPattern<ReturnType, Object, InputType> pattern : patterns) { - Pair<Boolean, Object> 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 <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> PatternMatcher<NewOutput, InputType> transformOutput( + Function<ReturnType, NewOutput> transformer) { + return from(inp -> transformer.apply(matchFor(inp))); } -} +}
\ No newline at end of file diff --git a/base/src/main/java/bjc/utils/patterns/SimplePatternMatcher.java b/base/src/main/java/bjc/utils/patterns/SimplePatternMatcher.java new file mode 100644 index 0000000..fea947a --- /dev/null +++ b/base/src/main/java/bjc/utils/patterns/SimplePatternMatcher.java @@ -0,0 +1,41 @@ +package bjc.utils.patterns; + +import bjc.data.*; + +/** + * Implements pattern-matching (of a sort) against a collection of patterns. + * + * @author Ben Culkin + * + * @param <ReturnType> The type returned by the pattern. + */ +public class SimplePatternMatcher<ReturnType, InputType> + implements PatternMatcher<ReturnType, InputType> { + private final ComplexPattern<ReturnType, Object, InputType>[] patterns; + + /** + * Create a new pattern matcher. + * + * @param patterns The set of patterns to match against. + */ + @SuppressWarnings("unchecked") + @SafeVarargs + public SimplePatternMatcher(ComplexPattern<ReturnType, ?, InputType>...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<ReturnType, Object, InputType>[]) patterns; + } + + @Override + public ReturnType matchFor(InputType input) throws NonExhaustiveMatch { + for (ComplexPattern<ReturnType, Object, InputType> pattern : patterns) { + Pair<Boolean, Object> matches = pattern.matches(input); + if (matches.getLeft()) { + pattern.apply(input, matches.getRight()); + } + } + + throw new NonExhaustiveMatch("Non-exhaustive match against " + input); + } +} |
