com.ikokoon.toolkit
Class Toolkit

java.lang.Object
  extended by com.ikokoon.toolkit.Toolkit

public class Toolkit
extends java.lang.Object

This class contains methods for changing a string to the byte code representation and visa versa. Also some other nifty functions like stripping a string of white space etc.

Since:
12.07.09
Version:
01.00
Author:
Michael Couck

Nested Class Summary
static interface Toolkit.IFileFilter
          This is the interface to select files with below.
 
Constructor Summary
Toolkit()
           
 
Method Summary
static java.lang.String classNameToPackageName(java.lang.String className)
          Takes the name of a class and returns the package name for the class.
static void copyFile(java.io.File in, java.io.File out)
           
static void copyFiles(java.io.File src, java.io.File dest)
          This function will copy files or directories from one location to another. note that the source and the destination must be mutually exclusive.
static boolean createFile(java.io.File file)
           
static void deleteFile(java.io.File file, int maxRetryCount)
          Deletes all the files recursively and then the directories recursively.
static void deleteFiles(java.io.File file, java.lang.String... extensions)
          Deletes all the files in a directory with one of the specified extensions.
static java.lang.Object deserialize(java.lang.String xml)
           
static java.lang.Object deserializeFromBase64(java.lang.String base64)
          De-serializes an object from a base 64 string to an object.
static java.lang.String dotToSlash(java.lang.String name)
          This method replaces the . in the byte code name with / which is what we expect from byte code.
static void findFiles(java.io.File file, Toolkit.IFileFilter filter, java.util.List<java.io.File> list)
          Finds files on the file system below the directory specified recursively using the selection criteria supplied in the IFileFilter parameter.
static double format(double d, int precision)
          Formats a double to the required precision.
static java.lang.String format(java.lang.String string, int precision)
          Formats a string to the desired precision.
static java.io.ByteArrayOutputStream getContents(java.io.File file)
          Reads the contents of the file and returns the contents in a byte array form.
static java.io.ByteArrayOutputStream getContents(java.io.InputStream inputStream)
          Reads the contents of the file and returns the contents in a byte array form.
static java.lang.reflect.Field getField(java.lang.Class<?> klass, java.lang.String name)
          Gets a field in the class or in the heirachy of the class.
static
<T> T[]
getUniqueValues(T t)
          Returns an array of values that are defined as being a unique combination for the entity by using the Unique annotation for the class.
static
<E> E
getValue(java.lang.Class<E> klass, java.lang.Object object, java.lang.String name)
          Returns the value of the field specified from the object specified.
static java.lang.Long hash(java.lang.Object... objects)
          Builds a hash from an array of objects.
static java.lang.Long hash(java.lang.String string)
          Simple, fast hash function to generate quite unique hashes from strings(i.e. toCharArray()).
static java.lang.String replaceAll(java.lang.String aInput, java.lang.String aOldPattern, java.lang.String aNewPattern)
          If Java 1.4 is unavailable, the following technique may be used.
static java.lang.String serialize(java.lang.Object object)
           
static java.lang.String serializeToBase64(java.lang.Object object)
          Serializes an object to a byte array then to a base 64 string of the byte array.
static void setContents(java.io.File file, byte[] bytes)
          Writes the contents of a byte array to a file.
static java.lang.String slashToDot(java.lang.String name)
          This method replaces the / in the byte code name with . which is XML friendly.
static java.lang.String stripWhitespace(java.lang.String string)
          Removes any whitespace from the string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Toolkit

public Toolkit()
Method Detail

hash

public static final java.lang.Long hash(java.lang.String string)
Simple, fast hash function to generate quite unique hashes from strings(i.e. toCharArray()).

Parameters:
string - the string to generate the hash from
Returns:
the integer representation of the hash of the string characters, typically quite unique for strings less than 10 characters

hash

public static final java.lang.Long hash(java.lang.Object... objects)
Builds a hash from an array of objects.

Parameters:
objects - the objects to build the hash from
Returns:
the hash of the objects

slashToDot

public static java.lang.String slashToDot(java.lang.String name)
This method replaces the / in the byte code name with . which is XML friendly.

Parameters:
name - the byte code name of a class
Returns:
the Java name of the class

dotToSlash

public static java.lang.String dotToSlash(java.lang.String name)
This method replaces the . in the byte code name with / which is what we expect from byte code.

Parameters:
name - the name of the class or package
Returns:
the byte code name of the class or package

classNameToPackageName

public static java.lang.String classNameToPackageName(java.lang.String className)
Takes the name of a class and returns the package name for the class.

Parameters:
className - the name of the class fully qualified
Returns:
the package name of the class

stripWhitespace

public static java.lang.String stripWhitespace(java.lang.String string)
Removes any whitespace from the string.

Parameters:
string - the string to remove whitespace from
Returns:
the string without any whitespace that includes carriage returns etc.

getField

public static java.lang.reflect.Field getField(java.lang.Class<?> klass,
                                               java.lang.String name)
Gets a field in the class or in the heirachy of the class.

Parameters:
klass - the original class
name - the name of the field
Returns:
the field in the object or super classes of the object

getValue

public static <E> E getValue(java.lang.Class<E> klass,
                             java.lang.Object object,
                             java.lang.String name)
Returns the value of the field specified from the object specified.

Parameters:
object - the object to get the field value from
name - the name of the field in the object
Returns:
the value of the field if there is such a field or null if there isn't ir if anything goes wrong

deleteFile

public static void deleteFile(java.io.File file,
                              int maxRetryCount)
Deletes all the files recursively and then the directories recursively.

Parameters:
file - the directory or file to delete
the - number of times to retry to delete the file

deleteFiles

public static void deleteFiles(java.io.File file,
                               java.lang.String... extensions)
Deletes all the files in a directory with one of the specified extensions.

Parameters:
file - the file to delete or the directory to delete files in
extensions - the extensions of files to delete

findFiles

public static void findFiles(java.io.File file,
                             Toolkit.IFileFilter filter,
                             java.util.List<java.io.File> list)
Finds files on the file system below the directory specified recursively using the selection criteria supplied in the IFileFilter parameter.

Parameters:
file - the file to start looking from
filter - the filter to select files with
list - the list of files to add the selected files to

getContents

public static java.io.ByteArrayOutputStream getContents(java.io.File file)
Reads the contents of the file and returns the contents in a byte array form.

Parameters:
file - the file to read the contents from
Returns:
the file contents in a byte array output stream
Throws:
java.lang.Exception

getContents

public static java.io.ByteArrayOutputStream getContents(java.io.InputStream inputStream)
Reads the contents of the file and returns the contents in a byte array form.

Parameters:
inputStream - the file to read the contents from
Returns:
the file contents in a byte array output stream
Throws:
java.lang.Exception

setContents

public static void setContents(java.io.File file,
                               byte[] bytes)
Writes the contents of a byte array to a file.

Parameters:
file - the file to write to
bytes - the byte data to write

format

public static double format(double d,
                            int precision)
Formats a double to the required precision.

Parameters:
d - the double to format
precision - the precision for the result
Returns:
the double formatted to the required precision

format

public static java.lang.String format(java.lang.String string,
                                      int precision)
Formats a string to the desired precision.

Parameters:
string - the string to format to a precision
precision - the precision of the result
Returns:
the string formatted to the required precision

serializeToBase64

public static java.lang.String serializeToBase64(java.lang.Object object)
Serializes an object to a byte array then to a base 64 string of the byte array.

Parameters:
object - the object to serialise to base 64
Returns:
the string representation of the object in serialised base 64

deserializeFromBase64

public static java.lang.Object deserializeFromBase64(java.lang.String base64)
De-serializes an object from a base 64 string to an object.

Parameters:
base64 - the base 64 string representation of the object
Returns:
the object de-serialised from the string or null if an exception is thrown

getUniqueValues

public static <T> T[] getUniqueValues(T t)
Returns an array of values that are defined as being a unique combination for the entity by using the Unique annotation for the class.

Type Parameters:
T - the type of object to be inspected for unique fields
Parameters:
t - the object t inspect for unique field combinations
Returns:
the array of unique field values for the entity

copyFile

public static void copyFile(java.io.File in,
                            java.io.File out)

copyFiles

public static void copyFiles(java.io.File src,
                             java.io.File dest)
This function will copy files or directories from one location to another. note that the source and the destination must be mutually exclusive. This function can not be used to copy a directory to a sub directory of itself. The function will also have problems if the destination files already exist.

Parameters:
src - A File object that represents the source for the copy
dest - A File object that represents the destination for the copy.

replaceAll

public static java.lang.String replaceAll(java.lang.String aInput,
                                          java.lang.String aOldPattern,
                                          java.lang.String aNewPattern)
If Java 1.4 is unavailable, the following technique may be used.

Parameters:
aInput - is the original String which may contain substring aOldPattern
aOldPattern - is the non-empty substring which is to be replaced
aNewPattern - is the replacement for aOldPattern

createFile

public static boolean createFile(java.io.File file)

serialize

public static java.lang.String serialize(java.lang.Object object)

deserialize

public static java.lang.Object deserialize(java.lang.String xml)


Copyright © 2010. All Rights Reserved.