Evaluating Software Design Patterns
— the "Gang of Four" patterns implemented in Java 6

dk.rode.thesis.meta.util
Class Util

java.lang.Object
  extended by dk.rode.thesis.meta.util.Util

public class Util
extends Object

Various utility functions.

Author:
Gunni Rode / rode.dk

Field Summary
private static AtomicInteger FILE_ID
          To make temporary file names unique during serialization copy.
 
Constructor Summary
private Util()
          Private off-limit constructor.
 
Method Summary
static
<E> List<E>
add(List<E> list, E... elements)
          Adds each element in elements, in order, to the list supplied as list.
static
<E> Set<E>
add(Set<E> set, E... elements)
          Adds each element in elements, in order, to the set supplied as set.
static
<E> E[]
copy(Object[] from, E[] to)
          Copies all elements from from into to, from index zero.
static
<E> E[]
copy(Object[] from, E[] to, int length)
          Copies the first length elements from from into to, from index zero.
static boolean equals(Object first, Object... rest)
          Returns true first equals all other objects in rest, false if not.
static boolean equals(Object a, Object b)
          Returns true if a equals b, including true if both a and b are null.
static File getDataDirectory()
          Returns the data directory used for test purposes.
static File getDirectory(String name, boolean create)
          Returns the directory named name from the root of the class path.
static File getLogDirectory()
          Returns the log directory used for test purposes.
static
<E> Set<E>
newIdentityHashSet()
          Returns a new identity hash set.
static byte[] readBytes(InputStream in)
          Reads the contents of the input stream supplied as in and returns the contents as a byte array.
static
<T extends Serializable>
T
serializableCopy(T object)
          Copies object by serializing it and returning the deserialized copy.
static Object[] shift(Object value, Object... values)
          Creates a new array with value at index 0, and values from index 1 to values.length, in order.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FILE_ID

private static final AtomicInteger FILE_ID
To make temporary file names unique during serialization copy.

Constructor Detail

Util

private Util()
Private off-limit constructor.

Method Detail

add

public static final <E> List<E> add(List<E> list,
                                    E... elements)
Adds each element in elements, in order, to the list supplied as list.

Null elements in elements will be ignored.

Type Parameters:
E - The type of elements.
Parameters:
list - The list; cannot be null.
elements - The elements to add to list; can contain null entries, which will be ignored.
Returns:
list; never null.
Throws:
NullPointerException - If list is null.

add

public static final <E> Set<E> add(Set<E> set,
                                   E... elements)
Adds each element in elements, in order, to the set supplied as set.

Null elements in elements will be ignored.

Type Parameters:
E - The type of elements.
Parameters:
set - The set; cannot be null.
elements - The elements to add to set; can be null or can contain null entries, which will be ignored.
Returns:
set; never null.
Throws:
NullPointerException - If set is null.

copy

public static final <E> E[] copy(Object[] from,
                                 E[] to)
Copies all elements from from into to, from index zero.

Type Parameters:
E - The type of elements stored in to.
Parameters:
from - The from array; cannot be null.
to - The to array; cannot be null.
Returns:
The to array; never null.
Throws:
ArrayStoreException - If elements in from cannot be stored in to.

copy

public static final <E> E[] copy(Object[] from,
                                 E[] to,
                                 int length)
Copies the first length elements from from into to, from index zero.

Type Parameters:
E - The type of elements stored in to.
Parameters:
from - The from array; cannot be null.
to - The to array; cannot be null.
length - The number of elements to copy.
Returns:
The to array; never null.
Throws:
IndexOutOfBoundsException - If length is illegal.
ArrayStoreException - If elements in from cannot be stored in to.

equals

public static final boolean equals(Object first,
                                   Object... rest)
Returns true first equals all other objects in rest, false if not.

The comparison between two objects is performed using the equals(Object, Object) method.

If rest is empty, true is returned.

Parameters:
first - The first object; can be null.
rest - The rest of the objects; can be null or empty.
Returns:
True if all objects are equal, false if not.

equals

public static final boolean equals(Object a,
                                   Object b)
Returns true if a equals b, including true if both a and b are null.

Array types are compared based on a proper Arrays.equals(..) method, including support for primitives.

Parameters:
a - The first object; can be null.
b - The second object; can be null.
Returns:
True if equal, false if not.

getDataDirectory

public static final File getDataDirectory()
Returns the data directory used for test purposes. The directory is named data and is placed in the root directory of the class path.

Returns:
The directory; never null.

getDirectory

public static final File getDirectory(String name,
                                      boolean create)
Returns the directory named name from the root of the class path.

If create is true and no such directory exist, it will be created.

Parameters:
name - The directory name; cannot be null.
create - True to create if no such directory exist.
Returns:
The directory; never null.
Throws:
NullPointerException - If name is null.
IllegalArgumentException - If name does not denote a (created) directory.

getLogDirectory

public static final File getLogDirectory()
Returns the log directory used for test purposes.

The directory is named log and is placed in the root directory of the class path. The directory is created if it does not exist.

Returns:
The directory; never null.

newIdentityHashSet

public static final <E> Set<E> newIdentityHashSet()
Returns a new identity hash set.

Type Parameters:
E - The type of elements stored in the set.
Returns:
A new identity hash set; never null.

readBytes

public static final byte[] readBytes(InputStream in)
                              throws IOException
Reads the contents of the input stream supplied as in and returns the contents as a byte array.

in is not closed by this method.

Parameters:
in - The input stream; cannot be null.
Returns:
A byte array representing the contents of in; never null.
Throws:
NullPointerException - If in is null.
IOException - If the reading fails.

serializableCopy

public static final <T extends Serializable> T serializableCopy(T object)
Copies object by serializing it and returning the deserialized copy.

Implementation notes:
This implementation is not fast, as it utilises a temporary file to store the serialized data. I truly doubt serialization was invented for this purpose... :)

Type Parameters:
T - The type of serializable object.
Parameters:
object - The object to copy; cannot be null.
Returns:
A copy of object; never null.
Throws:
NullPointerException - If object is null.
RuntimeException - If the copying (serialization) fails.

shift

public static final Object[] shift(Object value,
                                   Object... values)
Creates a new array with value at index 0, and values from index 1 to values.length, in order.

Null values are handled like any other value.

Parameters:
value - The first value in the new array; can be null.
values - The last values, in order, in the new array; can contain null entries.
Returns:
A new array containing all the values; never null.

Gunni Rode / rode.dk

Feel free to use and/or modify the Java 6 source code developed for this thesis AT YOUR OWN RISK, but note that the source code comes WITHOUT ANY — and I do mean WITHOUT ANY — form of warranty WHAT SO EVER!

The original thesis and source code are available at rode.dk/thesis.