diff options
Diffstat (limited to 'src/main/java/org/openslx/imagemaster/util/Util.java')
| -rw-r--r-- | src/main/java/org/openslx/imagemaster/util/Util.java | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/src/main/java/org/openslx/imagemaster/util/Util.java b/src/main/java/org/openslx/imagemaster/util/Util.java index ff0b8c1..4459a3a 100644 --- a/src/main/java/org/openslx/imagemaster/util/Util.java +++ b/src/main/java/org/openslx/imagemaster/util/Util.java @@ -1,16 +1,52 @@ package org.openslx.imagemaster.util; +import java.util.Random; + +import org.apache.log4j.Logger; + 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 friendsly 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 ) - System.out.println( "[NOTNULL] " + message ); - System.out.println( Thread.currentThread().getStackTrace().toString() ); + log.fatal( "[NOTNULL] " + message ); + log.warn( Thread.currentThread().getStackTrace().toString() ); System.exit( 2 ); } } + + /** + * Static {@link Random} instance. + */ + private static final Random random = new Random(); + + /** + * Return a random integer in the range of 0 (inclusive) and + * n (exclusive). Uses the internal static instance of {@link Random}, + * so you don't have to deal with instances everywhere. + * + * @param n the upper bound (exclusive) + * @return number between 0 (inclusive) and n (exclusive) + */ + public static int randomInt( int n ) + { + return random.nextInt( n ); + } } |
