Added StringUtil#wrapParsingInt() and StringUtil#wrapParsingLong() methods

This commit is contained in:
Marc Baloup 2022-08-14 00:29:23 +02:00
parent c548856b76
commit 6afa1086c2
Signed by: marcbal
GPG Key ID: BBC0FE3ABC30B893

View File

@ -3,6 +3,7 @@ package fr.pandacube.lib.util;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -116,4 +117,26 @@ public class StringUtil {
return newStr; 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<String> wrapParsingInt(UnaryOperator<Integer> 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<String> wrapParsingLong(UnaryOperator<Long> operator) {
return s -> Long.toString(operator.apply(Long.parseLong(s)));
}
} }