From 889fac2bdf993dc86f64a8893c0260fdcf848acb Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Mon, 10 Apr 2017 16:40:33 -0400 Subject: Cleanup --- .../java/bjc/utils/ioutils/RegexStringEditor.java | 123 ++++++++++----------- 1 file changed, 60 insertions(+), 63 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/ioutils/RegexStringEditor.java') diff --git a/BJC-Utils2/src/main/java/bjc/utils/ioutils/RegexStringEditor.java b/BJC-Utils2/src/main/java/bjc/utils/ioutils/RegexStringEditor.java index 4f66a99..ee1e2ea 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/ioutils/RegexStringEditor.java +++ b/BJC-Utils2/src/main/java/bjc/utils/ioutils/RegexStringEditor.java @@ -1,19 +1,19 @@ package bjc.utils.ioutils; +import java.util.function.BiFunction; +import java.util.function.UnaryOperator; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + import bjc.utils.data.Toggle; import bjc.utils.data.ValueToggle; import bjc.utils.funcdata.FunctionalList; import bjc.utils.funcdata.IList; import bjc.utils.functypes.ID; -import java.util.function.BiFunction; -import java.util.function.UnaryOperator; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - /** * Editor methods for strings based off the command language for the Sam editor. - * + * * @author EVE * */ @@ -23,65 +23,66 @@ public class RegexStringEditor { /** * Replace every occurrence of the pattern with the result of applying * the action to the string matched by the pattern. - * + * * @param input * The input string to process. - * + * * @param patt * The pattern to match the string against. - * + * * @param action * The action to transform matches with. - * + * * @return The string, with matches replaced with the action. */ - public static String onOccurances(String input, Pattern patt, UnaryOperator action) { + public static String onOccurances(final String input, final Pattern patt, final UnaryOperator action) { return reduceOccurances(input, patt, SID, action); } /** * Replace every occurrence between the patterns with the result of * applying the action to the strings between the patterns. - * + * * @param input * The input string to process. - * + * * @param patt * The pattern to match the string against. - * + * * @param action * The action to transform matches with. - * + * * @return The string, with strings between the matches replaced with * the action. */ - public static String betweenOccurances(String input, Pattern patt, UnaryOperator action) { + public static String betweenOccurances(final String input, final Pattern patt, + final UnaryOperator action) { return reduceOccurances(input, patt, action, SID); } /** * Execute actions between and on matches of a regular expression. - * + * * @param input * The input string. - * + * * @param rPatt * The pattern to match against the string. - * + * * @param betweenAction * The function to execute between matches of the string. - * + * * @param onAction * The function to execute on matches of the string. - * + * * @return The string, with both actions applied. */ - public static String reduceOccurances(String input, Pattern rPatt, UnaryOperator betweenAction, - UnaryOperator onAction) { - IList occurances = listOccurances(input, rPatt); + public static String reduceOccurances(final String input, final Pattern rPatt, + final UnaryOperator betweenAction, final UnaryOperator onAction) { + final IList occurances = listOccurances(input, rPatt); - Toggle> actions = new ValueToggle<>(onAction, betweenAction); - BiFunction reducer = (strang, state) -> { + final Toggle> actions = new ValueToggle<>(onAction, betweenAction); + final BiFunction reducer = (strang, state) -> { return state.append(actions.get().apply(strang)); }; @@ -90,50 +91,50 @@ public class RegexStringEditor { /** * Execute actions between and on matches of a regular expression. - * + * * @param input * The input string. - * + * * @param rPatt * The pattern to match against the string. - * + * * @param betweenAction * The function to execute between matches of the string. - * + * * @param onAction * The function to execute on matches of the string. - * + * * @return The string, with both actions applied. */ - public static IList mapOccurances(String input, Pattern rPatt, UnaryOperator betweenAction, - UnaryOperator onAction) { - IList occurances = listOccurances(input, rPatt); - Toggle> actions = new ValueToggle<>(onAction, betweenAction); + public static IList mapOccurances(final String input, final Pattern rPatt, + final UnaryOperator betweenAction, final UnaryOperator onAction) { + final IList occurances = listOccurances(input, rPatt); + final Toggle> actions = new ValueToggle<>(onAction, betweenAction); return occurances.map(strang -> actions.get().apply(strang)); } /** * Separate a string into match/non-match segments. - * + * * @param input * The string to separate. - * + * * @param rPatt * The pattern to use for separation. - * + * * @return The string, as a list of match/non-match segments, * starting/ending with a non-match segment. */ - public static IList listOccurances(String input, Pattern rPatt) { - IList res = new FunctionalList<>(); + public static IList listOccurances(final String input, final Pattern rPatt) { + final IList res = new FunctionalList<>(); - Matcher matcher = rPatt.matcher(input); + final Matcher matcher = rPatt.matcher(input); StringBuffer work = new StringBuffer(); - while(matcher.find()) { - String match = matcher.group(); + while (matcher.find()) { + final String match = matcher.group(); matcher.appendReplacement(work, ""); @@ -152,50 +153,46 @@ public class RegexStringEditor { /** * Apply an operation to a string if it matches a regular expression. - * + * * @param input * The input string. - * + * * @param patt * The pattern to match against it. - * + * * @param action * The action to execute if it matches. - * + * * @return The string, modified by the action if the pattern matched. */ - public static String ifMatches(String input, Pattern patt, UnaryOperator action) { - Matcher matcher = patt.matcher(input); + public static String ifMatches(final String input, final Pattern patt, final UnaryOperator action) { + final Matcher matcher = patt.matcher(input); - if(matcher.matches()) { + if (matcher.matches()) return action.apply(input); - } else { - return input; - } + else return input; } /** * Apply an operation to a string if it matches a regular expression. - * + * * @param input * The input string. - * + * * @param patt * The pattern to match against it. - * + * * @param action * The action to execute if it doesn't match. - * + * * @return The string, modified by the action if the pattern didn't * match. */ - public static String ifNotMatches(String input, Pattern patt, UnaryOperator action) { - Matcher matcher = patt.matcher(input); + public static String ifNotMatches(final String input, final Pattern patt, final UnaryOperator action) { + final Matcher matcher = patt.matcher(input); - if(matcher.matches()) { + if (matcher.matches()) return input; - } else { - return action.apply(input); - } + else return action.apply(input); } } \ No newline at end of file -- cgit v1.2.3