diff options
Diffstat (limited to 'src/main/java/org/openslx/util/Util.java')
-rw-r--r-- | src/main/java/org/openslx/util/Util.java | 43 |
1 files changed, 42 insertions, 1 deletions
diff --git a/src/main/java/org/openslx/util/Util.java b/src/main/java/org/openslx/util/Util.java index f475d01..2ebd4e1 100644 --- a/src/main/java/org/openslx/util/Util.java +++ b/src/main/java/org/openslx/util/Util.java @@ -74,7 +74,6 @@ public class Util * * @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 ) { @@ -85,6 +84,23 @@ public class Util } } + /** + * Parse the given String as a base10 long. + * If the string does not represent a valid long, return the given + * default value. + * + * @param value string representation to parse to a long + * @param defaultValue fallback value if given string can't be parsed + */ + public static long parseLong( String value, long defaultValue ) + { + try { + return Long.parseLong( value ); + } catch ( Exception e ) { + return defaultValue; + } + } + public static void safeClose( AutoCloseable... closeable ) { for ( AutoCloseable c : closeable ) { @@ -118,9 +134,34 @@ public class Util } } + /** + * Number of seconds elapsed since 1970-01-01 UTC. + */ public static long unixTime() { return System.currentTimeMillis() / 1000; } + /** + * Monotonic tick count in milliseconds, not bound to RTC. + */ + public static long tickCount() + { + return System.nanoTime() / 1000_000; + } + + private static final String[] UNITS = new String[] { "B", "KB", "MB", "GB", "TB", "PB", "???" }; + + public static String formatBytes( double val ) + { + int unit = 0; + while ( val > 1024 ) { + val /= 1024; + unit++; + if (unit >= UNITS.length) + break; + } + return String.format( "%.1f %s", val, UNITS[unit] ); + } + } |