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/SimplePatttern.java | 40 ++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 base/src/main/java/bjc/utils/patterns/SimplePatttern.java (limited to 'base/src/main/java/bjc/utils/patterns/SimplePatttern.java') diff --git a/base/src/main/java/bjc/utils/patterns/SimplePatttern.java b/base/src/main/java/bjc/utils/patterns/SimplePatttern.java new file mode 100644 index 0000000..1601894 --- /dev/null +++ b/base/src/main/java/bjc/utils/patterns/SimplePatttern.java @@ -0,0 +1,40 @@ +package bjc.utils.patterns; + +import bjc.data.*; + +/** + * A simpler form of a pattern. + * + * @author Ben Culkin + * + * @param The type returned by matching the pattern. + */ +public interface SimplePatttern extends Pattern { + /** + * Test if this pattern does match a given object. + * + * @param input The object to test against. + * + * @return Whether the object matches this pattern. + */ + boolean doesMatch(Object input); + + /** + * Applies this pattern to the input object. + * + * @param input The object that passed the condition. + * + * @return The result of applying this action to the input. + */ + ReturnType doApply(Object input); + + @Override + default ReturnType apply(Object input, Void state) { + return doApply(input); + } + + @Override + default IPair matches(Object input) { + return new Pair<>(doesMatch(input), null); + } +} \ No newline at end of file -- cgit v1.2.3