|
Evaluating Software Design Patterns — the "Gang of Four" patterns implemented in Java 6 |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object dk.rode.thesis.meta.util.Util
public class Util
Various utility functions.
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
|
add(List<E> list,
E... elements)
Adds each element in elements , in order, to the list
supplied as list . |
|
static
|
add(Set<E> set,
E... elements)
Adds each element in elements , in order, to the set
supplied as set . |
|
static
|
copy(Object[] from,
E[] to)
Copies all elements from from into to ,
from index zero. |
|
static
|
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
|
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
|
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 |
---|
private static final AtomicInteger FILE_ID
Constructor Detail |
---|
private Util()
Method Detail |
---|
public static final <E> List<E> add(List<E> list, E... elements)
elements
, in order, to the list
supplied as list
.
Null elements in elements
will be ignored.
E
- The type of elements.list
- The list; cannot be null.elements
- The elements to add to list
; can contain
null entries, which will be ignored.
list
; never null.
NullPointerException
- If list
is null.public static final <E> Set<E> add(Set<E> set, E... elements)
elements
, in order, to the set
supplied as set
.
Null elements in elements
will be ignored.
E
- The type of elements.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.
set
; never null.
NullPointerException
- If set
is null.public static final <E> E[] copy(Object[] from, E[] to)
from
into to
,
from index zero.
E
- The type of elements stored in to
.from
- The from array; cannot be null.to
- The to array; cannot be null.
to
array; never null.
ArrayStoreException
- If elements in from
cannot
be stored in to
.public static final <E> E[] copy(Object[] from, E[] to, int length)
length
elements from from
into to
, from index zero.
E
- The type of elements stored in to
.from
- The from array; cannot be null.to
- The to array; cannot be null.length
- The number of elements to copy.
to
array; never null.
IndexOutOfBoundsException
- If length
is illegal.
ArrayStoreException
- If elements in from
cannot
be stored in to
.public static final boolean equals(Object first, Object... rest)
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.
first
- The first object; can be null.rest
- The rest of the objects; can be null or empty.
public static final boolean equals(Object a, Object b)
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
.
a
- The first object; can be null.b
- The second object; can be null.
public static final File getDataDirectory()
data
and is placed
in the root directory of the class path.
public static final File getDirectory(String name, boolean create)
name
from the root
of the class path.
If create
is true and no such directory exist, it
will be created.
name
- The directory name; cannot be null.create
- True to create if no such directory exist.
NullPointerException
- If name
is null.
IllegalArgumentException
- If name
does not denote a
(created) directory.public static final File getLogDirectory()
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.
public static final <E> Set<E> newIdentityHashSet()
E
- The type of elements stored in the set.
public static final byte[] readBytes(InputStream in) throws IOException
in
and returns the contents as a byte array.
in
is not closed by this method.
in
- The input stream; cannot be null.
in
;
never null.
NullPointerException
- If in
is null.
IOException
- If the reading fails.public static final <T extends Serializable> T serializableCopy(T object)
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... :)
T
- The type of serializable object.object
- The object to copy; cannot be null.
object
; never null.
NullPointerException
- If object
is null.
RuntimeException
- If the copying (serialization) fails.public static final Object[] shift(Object value, Object... values)
value
at index 0,
and values
from index 1 to values.length
,
in order. Null values are handled like any other value.
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.
|
Gunni Rode / rode.dk | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |