From 83ba8f02ea105fff93266cb9fd62cd7d7937e61c Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 9 Oct 2019 17:40:49 +0200 Subject: Util: isEmpty: Use regex; add parseInt --- src/main/java/org/openslx/satserver/util/Util.java | 29 ++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/openslx/satserver/util/Util.java b/src/main/java/org/openslx/satserver/util/Util.java index 5979603..55dc76b 100644 --- a/src/main/java/org/openslx/satserver/util/Util.java +++ b/src/main/java/org/openslx/satserver/util/Util.java @@ -6,6 +6,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.charset.StandardCharsets; +import java.util.regex.Pattern; import org.apache.commons.io.FileUtils; @@ -86,10 +87,34 @@ public class Util { return startsWith( dir, DEFAULT_ALLOWED_DIRS ); } + + private static Pattern nonSpaceExp = Pattern.compile( "[^\\p{C}\\p{Z}]" ); - public static boolean isEmpty( String s ) + /** + * Whether given string is null, empty, or only matches space-like + * characters. + */ + public static boolean isEmpty( String string ) + { + return string == null || !nonSpaceExp.matcher( string ).find(); + } + + /** + * Parse the given String as a base10 integer. + * If the string does not represent a valid integer, return the given + * default value. + * + * @param value string representation to parse to an int + * @param defaultValue fallback value if given string can't be parsed + * @return + */ + public static int parseInt( String value, int defaultValue ) { - return s == null || s.isEmpty(); + try { + return Integer.parseInt( value ); + } catch ( Exception e ) { + return defaultValue; + } } } -- cgit v1.2.3-55-g7522