diff options
author | Simon Rettberg | 2019-10-09 17:40:49 +0200 |
---|---|---|
committer | Simon Rettberg | 2019-10-09 17:40:49 +0200 |
commit | 83ba8f02ea105fff93266cb9fd62cd7d7937e61c (patch) | |
tree | c8bbfa7809191e324c7d01fccecf956c4fa371a2 | |
parent | [DeleteDirectory] New task (diff) | |
download | tmlite-bwlp-83ba8f02ea105fff93266cb9fd62cd7d7937e61c.tar.gz tmlite-bwlp-83ba8f02ea105fff93266cb9fd62cd7d7937e61c.tar.xz tmlite-bwlp-83ba8f02ea105fff93266cb9fd62cd7d7937e61c.zip |
Util: isEmpty: Use regex; add parseInt
-rw-r--r-- | src/main/java/org/openslx/satserver/util/Util.java | 29 |
1 files 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; + } } } |