summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/util/Util.java
diff options
context:
space:
mode:
authorSimon Rettberg2015-07-21 12:40:53 +0200
committerSimon Rettberg2015-07-21 12:40:53 +0200
commitd13036a0227139287c682ebf63c73a4d459ccbdf (patch)
treee2baf3b12ed3fd950097e6dd865322d848e34d75 /src/main/java/org/openslx/util/Util.java
parentThrift API changes, add configurable timeout to file transfer classes (diff)
downloadmaster-sync-shared-d13036a0227139287c682ebf63c73a4d459ccbdf.tar.gz
master-sync-shared-d13036a0227139287c682ebf63c73a4d459ccbdf.tar.xz
master-sync-shared-d13036a0227139287c682ebf63c73a4d459ccbdf.zip
Extended Util class
Diffstat (limited to 'src/main/java/org/openslx/util/Util.java')
-rw-r--r--src/main/java/org/openslx/util/Util.java54
1 files changed, 42 insertions, 12 deletions
diff --git a/src/main/java/org/openslx/util/Util.java b/src/main/java/org/openslx/util/Util.java
index 9f98007..24daf4c 100644
--- a/src/main/java/org/openslx/util/Util.java
+++ b/src/main/java/org/openslx/util/Util.java
@@ -3,7 +3,6 @@ package org.openslx.util;
import java.io.Closeable;
import org.apache.log4j.Logger;
-import org.openslx.util.Util;
public class Util
{
@@ -43,24 +42,24 @@ public class Util
}
}
-
/**
- * Tries to parse an int. Returns 0 on error.
+ * Parse the given String as a base10 integer.
+ * If the string does not represent a valid integer, return the given
+ * default value.
*
- * @param s
- * The string to parse
- * @return The parsed int or 0 on error
+ * @param value string representation to parse to an int
+ * @param defaultValue fallback value if given string can't be parsed
+ * @return
*/
- public static int tryToParseInt( String s )
- {
+ public static int parseInt(String value, int defaultValue) {
try {
- return Integer.parseInt( s );
- } catch ( NumberFormatException e ) {
- return 0;
+ return Integer.parseInt(value);
+ } catch (Exception e) {
+ return defaultValue;
}
}
- public static void streamClose( Closeable... closeable )
+ public static void safeClose( Closeable... closeable )
{
for ( Closeable c : closeable ) {
if ( c == null )
@@ -71,4 +70,35 @@ public class Util
}
}
}
+
+ public static void safeClose( AutoCloseable... closeable )
+ {
+ for ( AutoCloseable c : closeable ) {
+ if ( c == null )
+ continue;
+ try {
+ c.close();
+ } catch ( Throwable t ) {
+ }
+ }
+ }
+
+ public static boolean sleep(int millis) {
+ try {
+ Thread.sleep( millis );
+ return true;
+ } catch ( InterruptedException e ) {
+ Thread.currentThread().interrupt();
+ return false;
+ }
+ }
+
+ public static boolean joinThread(Thread t) {
+ try {
+ t.join();
+ return true;
+ } catch ( InterruptedException e ) {
+ return false;
+ }
+ }
}