From 6afa1086c2f90298877727bb94a73c09641b4e07 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Sun, 14 Aug 2022 00:29:23 +0200 Subject: [PATCH] Added StringUtil#wrapParsingInt() and StringUtil#wrapParsingLong() methods --- .../fr/pandacube/lib/util/StringUtil.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/pandalib-util/src/main/java/fr/pandacube/lib/util/StringUtil.java b/pandalib-util/src/main/java/fr/pandacube/lib/util/StringUtil.java index c6c91fd..1777e95 100644 --- a/pandalib-util/src/main/java/fr/pandacube/lib/util/StringUtil.java +++ b/pandalib-util/src/main/java/fr/pandacube/lib/util/StringUtil.java @@ -3,6 +3,7 @@ package fr.pandacube.lib.util; import java.util.Arrays; import java.util.List; import java.util.function.Predicate; +import java.util.function.UnaryOperator; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -116,4 +117,26 @@ public class StringUtil { return newStr; } } + + + /** + * Wraps the provided {@link UnaryOperator} of integer into an {@link UnaryOperator} of String parsing the input + * string to int and converting back the return value to String. + * @param operator the {@link UnaryOperator} to warp. + * @return an {@link UnaryOperator} of String. + */ + public static UnaryOperator wrapParsingInt(UnaryOperator operator) { + return s -> Integer.toString(operator.apply(Integer.parseInt(s))); + } + + /** + * Wraps the provided {@link UnaryOperator} of long into an {@link UnaryOperator} of String parsing the input + * string to long and converting back the return value to String. + * @param operator the {@link UnaryOperator} to warp. + * @return an {@link UnaryOperator} of String. + */ + public static UnaryOperator wrapParsingLong(UnaryOperator operator) { + return s -> Long.toString(operator.apply(Long.parseLong(s))); + } + }