diff options
| author | Simon Rettberg | 2014-11-13 13:47:24 +0100 |
|---|---|---|
| committer | Simon Rettberg | 2014-11-13 13:47:24 +0100 |
| commit | 7a5ce393bd4db00ad930e0559e05f504661c0c05 (patch) | |
| tree | 5597924190d6da7da5464e3f539d72a77b0ce51f /src/main/java/org/openslx/util | |
| parent | Add DataReceived callback to Downloader (diff) | |
| parent | Example file for proxy config file. Proxy config file MUST be stored as "/opt... (diff) | |
| download | master-sync-shared-7a5ce393bd4db00ad930e0559e05f504661c0c05.tar.gz master-sync-shared-7a5ce393bd4db00ad930e0559e05f504661c0c05.tar.xz master-sync-shared-7a5ce393bd4db00ad930e0559e05f504661c0c05.zip | |
Merge branch 'master' of git.openslx.org:bwlp/master-sync-shared
Diffstat (limited to 'src/main/java/org/openslx/util')
| -rw-r--r-- | src/main/java/org/openslx/util/Util.java | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/main/java/org/openslx/util/Util.java b/src/main/java/org/openslx/util/Util.java new file mode 100644 index 0000000..9f98007 --- /dev/null +++ b/src/main/java/org/openslx/util/Util.java @@ -0,0 +1,74 @@ +package org.openslx.util; + +import java.io.Closeable; + +import org.apache.log4j.Logger; +import org.openslx.util.Util; + +public class Util +{ + private static Logger log = Logger.getLogger( Util.class ); + + /** + * Check if the given object is null, abort program if true. An optional + * message to be printed can be passed. A stack trace will be printed, too. + * Finally the application terminates with exit code 2. + * + * This comes in handy if something must not be null, and you want user + * friendly output. A perfect example would be reading settings from a + * config file. You can use this on mandatory fields. + * + * @param something + * the object to compare to null + * @param message + * the message to be printed if something is null + */ + public static void notNullFatal( Object something, String message ) + { + if ( something == null ) { + if ( message != null ) + log.fatal( "[NOTNULL] " + message ); + log.warn( Thread.currentThread().getStackTrace().toString() ); + System.exit( 2 ); + } + } + + public static void notNullOrEmptyFatal( String something, String message ) + { + if ( something == null || something.isEmpty() ) { + if ( message != null ) + log.fatal( "[NOTNULL] " + message ); + log.warn( Thread.currentThread().getStackTrace().toString() ); + System.exit( 2 ); + } + } + + + /** + * Tries to parse an int. Returns 0 on error. + * + * @param s + * The string to parse + * @return The parsed int or 0 on error + */ + public static int tryToParseInt( String s ) + { + try { + return Integer.parseInt( s ); + } catch ( NumberFormatException e ) { + return 0; + } + } + + public static void streamClose( Closeable... closeable ) + { + for ( Closeable c : closeable ) { + if ( c == null ) + continue; + try { + c.close(); + } catch ( Throwable t ) { + } + } + } +} |
