diff options
-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; + } } } |