From fefd6eb2917b9a0856c247353545cc13876b6eda Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Tue, 1 Dec 2020 20:19:34 -0500 Subject: An assortment of changes/new things --- .../java/bjc/utils/patterns/ComplexPattern.java | 90 ++++++++++++++++------ 1 file changed, 65 insertions(+), 25 deletions(-) (limited to 'base/src/main/java/bjc/utils/patterns/ComplexPattern.java') diff --git a/base/src/main/java/bjc/utils/patterns/ComplexPattern.java b/base/src/main/java/bjc/utils/patterns/ComplexPattern.java index 3926f2c..e9035df 100644 --- a/base/src/main/java/bjc/utils/patterns/ComplexPattern.java +++ b/base/src/main/java/bjc/utils/patterns/ComplexPattern.java @@ -35,6 +35,8 @@ public interface ComplexPattern { */ ReturnType apply(InputType input, PredType state); + /* Pattern producing functions */ + /** * Create a pattern composed from a predicate & a function. * @@ -49,7 +51,8 @@ public interface ComplexPattern { */ static ComplexPattern from( Function> matcher, - BiFunction accepter) { + BiFunction accepter) + { return new FunctionalPattern<>(matcher, accepter); } @@ -68,7 +71,8 @@ public interface ComplexPattern { @SuppressWarnings("unchecked") static ComplexPattern ofClass( Class clasz, - Function action) { + Function action) + { return from( (input) -> IPair.pair(clasz.isInstance(input), null), (input, ignored) -> action.apply((ClassType)input) @@ -89,11 +93,12 @@ public interface ComplexPattern { static ComplexPattern matchesObject( InpType obj, Function action - ) { + ) + { return from( - (input) -> IPair.pair(obj.equals(input), null), - (input, ignored) -> action.apply(input) - ); + (input) -> IPair.pair(obj.equals(input), null), + (input, ignored) -> action.apply(input) + ); } /** @@ -113,13 +118,16 @@ public interface ComplexPattern { static ComplexPattern equalsString( String pattern, BiFunction action - ) { + ) + { + Function> matcher = (input) -> { + String objString = input.toString(); + + return IPair.pair(pattern.equals(objString), objString); + }; + return from( - (input) -> { - String objString = input.toString(); - - return IPair.pair(pattern.equals(objString), objString); - }, + matcher, (input, objString) -> action.apply(input, objString) ); } @@ -140,21 +148,21 @@ public interface ComplexPattern { String regex, Predicate cond, BiFunction action - ) { + ) + { java.util.regex.Pattern regexPat = java.util.regex.Pattern.compile(regex); + Function> matcher = (input) -> { + String inpString = input.toString(); + + Matcher mat = regexPat.matcher(inpString); + + if (cond.test(mat)) return IPair.pair(true, mat); + else return IPair.pair(false, null); + }; + return from( - (input) -> { - String inpString = input.toString(); - - Matcher mat = regexPat.matcher(inpString); - - if (cond.test(mat)) { - return IPair.pair(true, mat); - } else { - return IPair.pair(false, null); - } - }, + matcher, (input, res) -> action.apply(input, res) ); } @@ -162,6 +170,7 @@ public interface ComplexPattern { // @TODO Nov 21, 2020 Ben Culkin :MorePatterns // Try and write something to iterate over Iterator in a type-safe manner // Also, something for doing a sub-pattern match + /** * Create a pattern which will always execute. * @@ -174,10 +183,41 @@ public interface ComplexPattern { */ static ComplexPattern otherwise( Function action - ) { + ) + { return from( (input) -> IPair.pair(true, null), (input, ignored) -> action.apply(input) ); } + + /** + * Create a pattern which checks if the string form of a given object starts + * with a specific string. + * + * @param The type returned by the matcher. + * @param The type being matched against. + * + * @param pattern The string to check against. + * @param action The action to execute. + * + * @return A pattern which functions as described. + */ + static ComplexPattern startsWith( + String pattern, + Function action) + { + return from((input) -> { + String objString = input.toString(); + + if (objString.startsWith(pattern)) { + return IPair.pair( + true, + objString.substring( + pattern.length())); + } else { + return IPair.pair(false, null); + } + }, (ignored, input) -> action.apply(input)); + } } \ No newline at end of file -- cgit v1.2.3 From a2c7425458f645802a352abc4783e0afc73dba13 Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Thu, 3 Dec 2020 19:22:35 -0500 Subject: Adapt to esodata changes --- .../java/bjc/utils/patterns/ComplexPattern.java | 24 +++++++++++----------- 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'base/src/main/java/bjc/utils/patterns/ComplexPattern.java') diff --git a/base/src/main/java/bjc/utils/patterns/ComplexPattern.java b/base/src/main/java/bjc/utils/patterns/ComplexPattern.java index e9035df..c6d72ec 100644 --- a/base/src/main/java/bjc/utils/patterns/ComplexPattern.java +++ b/base/src/main/java/bjc/utils/patterns/ComplexPattern.java @@ -23,7 +23,7 @@ public interface ComplexPattern { * @return Whether or not this pattern is matched, as well as a state value * that will get passed to the pattern if it did match. */ - IPair matches(InputType input); + Pair matches(InputType input); /** * Apply this pattern, once it has matched. @@ -50,7 +50,7 @@ public interface ComplexPattern { * @return A pattern composed from the passed in functions. */ static ComplexPattern from( - Function> matcher, + Function> matcher, BiFunction accepter) { return new FunctionalPattern<>(matcher, accepter); @@ -74,7 +74,7 @@ public interface ComplexPattern { Function action) { return from( - (input) -> IPair.pair(clasz.isInstance(input), null), + (input) -> Pair.pair(clasz.isInstance(input), null), (input, ignored) -> action.apply((ClassType)input) ); } @@ -96,7 +96,7 @@ public interface ComplexPattern { ) { return from( - (input) -> IPair.pair(obj.equals(input), null), + (input) -> Pair.pair(obj.equals(input), null), (input, ignored) -> action.apply(input) ); } @@ -120,10 +120,10 @@ public interface ComplexPattern { BiFunction action ) { - Function> matcher = (input) -> { + Function> matcher = (input) -> { String objString = input.toString(); - return IPair.pair(pattern.equals(objString), objString); + return Pair.pair(pattern.equals(objString), objString); }; return from( @@ -152,13 +152,13 @@ public interface ComplexPattern { { java.util.regex.Pattern regexPat = java.util.regex.Pattern.compile(regex); - Function> matcher = (input) -> { + Function> matcher = (input) -> { String inpString = input.toString(); Matcher mat = regexPat.matcher(inpString); - if (cond.test(mat)) return IPair.pair(true, mat); - else return IPair.pair(false, null); + if (cond.test(mat)) return Pair.pair(true, mat); + else return Pair.pair(false, null); }; return from( @@ -186,7 +186,7 @@ public interface ComplexPattern { ) { return from( - (input) -> IPair.pair(true, null), + (input) -> Pair.pair(true, null), (input, ignored) -> action.apply(input) ); } @@ -211,12 +211,12 @@ public interface ComplexPattern { String objString = input.toString(); if (objString.startsWith(pattern)) { - return IPair.pair( + return Pair.pair( true, objString.substring( pattern.length())); } else { - return IPair.pair(false, null); + return Pair.pair(false, null); } }, (ignored, input) -> action.apply(input)); } -- cgit v1.2.3