summaryrefslogblamecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/util/Util.java
blob: ffd31066b195a1c228bde251eaf9139b02ab0e9f (plain) (tree)
1
2
3
4
5
6
7
8
9
10

                                     
                    



                               


                                           

                 
 
                                                                   
 






                                                                          
                                                                            




                                                                        



                                                                           

                                                                                      


                                         
 








                                                                                   
                                                                                  







                                                                                      
 



                                                          
 











                                                                              
 

                                           
           

                        












                                                          
         


                                                     
           


                                               

                                                   











                                                                        

                                                     
                                                     
                                            

                 








                                                                           







                                                                           
 
package org.openslx.imagemaster.util;

import java.io.File;
import java.util.Random;

import org.apache.log4j.Logger;

/**
 * Some utilities to make our lives easier.
 */
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 );
		}
	}

	/**
	 * Check if String is null or empty, abort program if so.
	 * An optional message to be printed can be passed. A stack trace
	 * will be printed, too. Finally the application terminates with
	 * exit code 2.
	 * 
	 * @param something The string you want to check
	 * @param message The message to be printed if "something" is null or empty
	 */
	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 );
		}
	}

	/**
	 * 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 );
	}

	/**
	 * Remove a folder and all contents
	 * 
	 * @param folder
	 */
	public static void deleteFolder( File folder )
	{
		File[] files = folder.listFiles();
		if ( files != null ) {
			for ( File f : files ) {
				if ( f.isDirectory() ) {
					deleteFolder( f );
				} else {
					f.delete();
				}
			}
		}
		folder.delete();
	}

	/**
	 * Tries to parse an int. Returns 0 on error.
	 * 
	 * @param s The strig to parse
	 * @return The parsed int or 0 on error
	 */
	public static int tryToParseInt( String s )
	{
		return tryToParseInt( s, 0 );
	}

	/**
	 * Tries to parse an int. Returns defaultValue on error.
	 * 
	 * @param s The strig to parse
	 * @param defaultValue value to be returned if s is not parsable
	 * @return The parsed int
	 */
	public static int tryToParseInt( String s, int defaultValue )
	{
		try {
			return Integer.parseInt( s );
		} catch ( NumberFormatException e ) {
			return defaultValue;
		}
	}

	public static int getNumberOfBlocks( long fileSize, int blockSize )
	{
		int blocks = (int) ( fileSize / blockSize );
		if ( fileSize % blockSize != 0 )
			blocks++;
		return blocks;
	}

	public static String sanitizeFileName( String fileName )
	{
		fileName = fileName.replaceAll( "[^a-zA-Z0-9_\\-]+", "_" );
		if ( fileName.length() > 40 )
			fileName = fileName.substring( 0, 40 );
		return fileName;
	}

}