org.fuin.utils4j
Class Utils4J

java.lang.Object
  extended by org.fuin.utils4j.Utils4J

public final class Utils4J
extends java.lang.Object

Common utility methods for use in Java applications and libraries.


Method Summary
static void addToClasspath(java.lang.String url)
          Adds an URL to the classpath.
static void addToClasspath(java.net.URL url)
          Adds an URL to the classpath.
static void checkValidDir(java.io.File dir)
          Check if the argument is an existing directory.
static void checkValidFile(java.io.File file)
          Check if the argument is an existing file.
static boolean containsURL(java.net.URL[] urls, java.net.URL url)
          Checks if the array or URLs contains the given URL.
static java.lang.String createHash(java.io.File file)
          Creates an MD5 hash from a file.
static java.lang.Object createInstance(java.lang.String className)
          Create an instance with Class.forName(..) and wrap all exceptions into RuntimeExceptions.
static java.net.URL createUrl(java.net.URL baseUrl, java.lang.String path, java.lang.String filename)
          Creates an URL based on a directory a relative path and a filename.
static java.lang.String getPackagePath(java.lang.Class clasz)
          Returns the package path of a class.
static java.lang.String getRelativePath(java.io.File baseDir, java.io.File dir)
          Returns a relative path based on a base directory.
static java.net.URL getResource(java.lang.Class clasz, java.lang.String name)
          Get the path to a resource located in the same package as a given class.
static java.util.Properties loadProperties(java.lang.Class clasz, java.lang.String filename)
          Load properties from classpath.
static java.util.Properties loadProperties(java.io.File file)
          Load properties from a file.
static java.util.Properties loadProperties(java.net.URL baseUrl, java.lang.String filename)
          Load a file from an directory.
static void saveProperties(java.io.File file, java.util.Properties props, java.lang.String comment)
          Save properties to a file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getPackagePath

public static java.lang.String getPackagePath(java.lang.Class clasz)
Returns the package path of a class.

Parameters:
clasz - Class to determine the path for.
Returns:
Package path for the class.

getResource

public static java.net.URL getResource(java.lang.Class clasz,
                                       java.lang.String name)
Get the path to a resource located in the same package as a given class.

Parameters:
clasz - Class with the same package where the resource is located.
name - Filename of the resource.
Returns:
Resource URL.

loadProperties

public static java.util.Properties loadProperties(java.lang.Class clasz,
                                                  java.lang.String filename)
Load properties from classpath.

Parameters:
clasz - Class in the same package as the properties file.
filename - Name of the properties file (without path).
Returns:
Properties.

loadProperties

public static java.util.Properties loadProperties(java.io.File file)
Load properties from a file.

Parameters:
file - Properties file.
Returns:
Properties.

checkValidFile

public static void checkValidFile(java.io.File file)
Check if the argument is an existing file. If the check fails an IllegalArgumentException is thrown.

Parameters:
file - File to check.

checkValidDir

public static void checkValidDir(java.io.File dir)
Check if the argument is an existing directory. If the check fails an IllegalArgumentException is thrown.

Parameters:
dir - Directory to check.

saveProperties

public static void saveProperties(java.io.File file,
                                  java.util.Properties props,
                                  java.lang.String comment)
Save properties to a file.

Parameters:
file - Destination file.
props - Properties to save.
comment - Comment for the file.

createInstance

public static java.lang.Object createInstance(java.lang.String className)
Create an instance with Class.forName(..) and wrap all exceptions into RuntimeExceptions.

Parameters:
className - Full qualified class name.
Returns:
New instance of the class.

addToClasspath

public static void addToClasspath(java.lang.String url)
Adds an URL to the classpath.

Parameters:
url - URL to add.

containsURL

public static boolean containsURL(java.net.URL[] urls,
                                  java.net.URL url)
Checks if the array or URLs contains the given URL.

Parameters:
urls - Array of URLs.
url - URL to find.
Returns:
If the URL is in the array TRUE else FALSE.

createHash

public static java.lang.String createHash(java.io.File file)
Creates an MD5 hash from a file.

Parameters:
file - File to create an hash for.
Returns:
Hash as text.

createUrl

public static java.net.URL createUrl(java.net.URL baseUrl,
                                     java.lang.String path,
                                     java.lang.String filename)
Creates an URL based on a directory a relative path and a filename.

Parameters:
baseUrl - Directory URL.
path - Relative path inside the base URL.
filename - Filename without path.
Returns:
URL.

getRelativePath

public static java.lang.String getRelativePath(java.io.File baseDir,
                                               java.io.File dir)
Returns a relative path based on a base directory. If the dir is not inside baseDir an IllegalArgumentException is thrown.

Parameters:
baseDir - Base directory the path is relative to.
dir - Directory inside the base directory.
Returns:
Path of dir relative to baseDir. If both are equal an empty string is returned.

loadProperties

public static java.util.Properties loadProperties(java.net.URL baseUrl,
                                                  java.lang.String filename)
Load a file from an directory.

Parameters:
baseUrl - Directory URL.
filename - Filename without path.
Returns:
Properties.

addToClasspath

public static void addToClasspath(java.net.URL url)
Adds an URL to the classpath.

Parameters:
url - URL to add.


Copyright © 2009 Future Invent Informationsmanagement GmbH. All Rights Reserved.