de.fu_berlin.ties.util
Class Util

java.lang.Object
  extended byde.fu_berlin.ties.util.Util

public final class Util
extends Object

A static class that provides general utility methods. No instances of this class can be created, only the static members should be used.

Version:
$Revision: 1.13 $, $Date: 2004/04/07 09:44:17 $, $Author: siefkes $
Author:
Christian Siefkes

Field Summary
static Logger LOG
          The logger used in the TIES system.
 
Constructor Summary
Util()
          Util instances should NOT be constructed in standard programming.
 
Method Summary
static Set arrayAsSet(Object[] array)
          Wraps an array into a set.
static boolean asBoolean(Object obj)
          Converts an object into a boolean primitive.
static byte asByte(Object obj)
          Converts an object into a byte primitive.
static char asChar(Object obj)
          Converts an object into a char primitive.
static double asDouble(Object obj)
          Converts an object into a double primitive.
static float asFloat(Object obj)
          Converts an object into a float primitive.
static int asInt(Object obj)
          Converts an object into an integer primitive.
static long asLong(Object obj)
          Converts an object into a long primitive.
static short asShort(Object obj)
          Converts an object into a short primitive.
static String asString(Object obj)
          Converts an object into a String.
static void combineArrays(Object[] array1, Object[] array2, Object[] targetArray)
          Combines two array into a target array, inserting all elements of the first array and then all elements of the second array in the target array.
static Object createObject(Class type, Object[] params, Class paramType)
          Creates an object of a specified type.
static Object createObject(Class type, Object[] params, Class[] paramTypes)
          Creates an object of a specified type.
static Object createObject(Class type, String[] params)
          Delegates to createObject(Class, Object[], Class), setting the paramType to the String class.
static Object createObject(String[] classDef)
          Delegates to createObject(Class, String[]), reading the class name from the first element in the array.
static void ensureNonNegative(double number, String identifier)
          Ensures that a number is positive or 0, throwing an exception if this condition is violated.
static void ensureNonNegative(long number, String identifier)
          Ensures that a number is positive or 0, throwing an exception if this condition is violated.
static String format(double number)
          Formats the given number, using at most 4 fraction digits.
static String format(double number, int maxFractDigits)
          Formats the given number, using at most the specified number of fraction digits.
static String formatDurationInMillisecs(long msecs)
          Formats a duration in XML Schema format (without using years and months).
static String formatDurationInSeconds(double secs)
          Formats a duration in XML Schema format (without using years and months).
static ArrayList lastN(List list, int number)
          Copied the last n elements from a list into a new list (or all elements, if the size of the input list is smaller or equal to n).
static String showDuration(long startTime)
          Calculated and prints the time passed since a given start time (in English).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOG

public static final Logger LOG
The logger used in the TIES system. The Avalon LogKit (bundled with Velocity) is used for logging. All log messages are written to a log file; message with priority INFO or higher are also written to standard out.

Constructor Detail

Util

public Util()
Util instances should NOT be constructed in standard programming. Instead, the method of this class should be called in a static way. This constructor is public to permit tools that require a JavaBean instance to operate.

Method Detail

arrayAsSet

public static Set arrayAsSet(Object[] array)
Wraps an array into a set. Duplicates will be ignored.

Parameters:
array - the array to wrap
Returns:
a set containing the contents of the set (in random order, without duplicates)

asBoolean

public static boolean asBoolean(Object obj)
                         throws IllegalArgumentException
Converts an object into a boolean primitive. For a Boolean object, the contained boolean primitive is returned.

Otherwise the object's toString() output is parsed calling BooleanUtils.toBooleanObject(java.lang.String), i.e. The strings "true", "on", and "yes" are accepted for a true value; "false", "off", and "no" are accepted for false; case and surrounding whitespace are ignored. An exception is thrown for all other strings.

If the specified object is null, false is returned.

Parameters:
obj - the object to check
Returns:
the converted boolean
Throws:
IllegalArgumentException - if the object's toString() output cannot be parsed as a boolean

asByte

public static byte asByte(Object obj)
                   throws NumberFormatException
Converts an object into a byte primitive. For a Number object, the contained number is converted to a byte -- this may involve rounding or truncation.

Otherwise the object's toString() output is parsed using the Byte.decode(java.lang.String) method.

If the specified object is null, -1 is returned.

Parameters:
obj - the object to check
Returns:
the converted byte
Throws:
NumberFormatException - if the object's toString() output does not contain a parsable byte

asChar

public static char asChar(Object obj)
                   throws IndexOutOfBoundsException
Converts an object into a char primitive. For a Character object, the contained char primitive is returned.

Otherwise the first non-whitespace character of the object's toString() output is returned. An exception is thrown if the string is empty or contains only whitespace.

If the specified object is null, Character.MIN_VALUE (the smallest character value) is returned.

Parameters:
obj - the object to check
Returns:
the converted char
Throws:
IndexOutOfBoundsException - if the object's toString() output is the empty string (after trimming outer whitespace)

asDouble

public static double asDouble(Object obj)
                       throws NumberFormatException
Converts an object into a double primitive. For a Number object, the contained number is converted to a double -- this may involve rounding.

Otherwise the object's toString() output is parsed using the Double.parseDouble(java.lang.String) method.

If the specified object is null, -1.0 is returned.

Parameters:
obj - the object to check
Returns:
the converted double
Throws:
NumberFormatException - if the object's toString() output does not contain a parsable double

asFloat

public static float asFloat(Object obj)
                     throws NumberFormatException
Converts an object into a float primitive. For a Number object, the contained number is converted to a float -- this may involve rounding.

Otherwise the object's toString() output is parsed using the Float.parseFloat(java.lang.String) method.

If the specified object is null, -1 is returned.

Parameters:
obj - the object to check
Returns:
the converted float
Throws:
NumberFormatException - if the object's toString() output does not contain a parsable float

asInt

public static int asInt(Object obj)
                 throws NumberFormatException
Converts an object into an integer primitive. For a Number object, the contained number is converted to a int -- this may involve rounding or truncation.

Otherwise the object's toString() output is parsed using the Integer.decode(java.lang.String) method.

If the specified object is null, -1 is returned.

Parameters:
obj - the object to check
Returns:
the converted int
Throws:
NumberFormatException - if the object's toString() output does not contain a parsable int

asLong

public static long asLong(Object obj)
                   throws NumberFormatException
Converts an object into a long primitive. For a Number object, the contained number is converted to a long -- this may involve rounding or truncation.

Otherwise the object's toString() output is parsed using the Long.decode(java.lang.String) method.

If the specified object is null, -1 is returned.

Parameters:
obj - the object to check
Returns:
the converted long
Throws:
NumberFormatException - if the object's toString() output does not contain a parsable long

asShort

public static short asShort(Object obj)
                     throws NumberFormatException
Converts an object into a short primitive. For a Number object, the contained number is converted to a short -- this may involve rounding or truncation.

Otherwise the object's toString() output is parsed using the Short.decode(java.lang.String) method.

If the specified object is null, -1 is returned.

Parameters:
obj - the object to check
Returns:
the converted short
Throws:
NumberFormatException - if the object's toString() output does not contain a parsable short

asString

public static String asString(Object obj)
Converts an object into a String. For non-null objects, the output of the Object.toString() is returned; otherwise, null is returned.

Parameters:
obj - the object to check
Returns:
the output of the object's Object.toString(); or null if obj is null

combineArrays

public static void combineArrays(Object[] array1,
                                 Object[] array2,
                                 Object[] targetArray)
Combines two array into a target array, inserting all elements of the first array and then all elements of the second array in the target array.

Parameters:
array1 - the first array to copy
array2 - the second array to copy
targetArray - the array to copy the two other array into; the type of this array must be suitable to accept elements from both array; the length of this array must be equal or greater than array1.length + array2.length

createObject

public static Object createObject(String[] classDef)
                           throws IllegalArgumentException,
                                  ClassNotFoundException,
                                  InstantiationException,
                                  SecurityException
Delegates to createObject(Class, String[]), reading the class name from the first element in the array.

Parameters:
classDef - defines the class to create: the first element must be a string giving the fully qualified class name; any further elements are passed as constructor parameters
Returns:
the created object; the returned object will be an instance of the specified type
Throws:
IllegalArgumentException - if the class name is missing (array is empty)
ClassNotFoundException - if the class cannot be located
InstantiationException - if instantiation failed
SecurityException - if access to the required reflection information is denied

createObject

public static Object createObject(Class type,
                                  String[] params)
                           throws InstantiationException,
                                  SecurityException
Delegates to createObject(Class, Object[], Class), setting the paramType to the String class.

Parameters:
type - the class of the object to create
params - the objects in this array are passed to the constructor; if null or empty, the default constructor is used
Returns:
the created object; the returned object will be an instance of type
Throws:
InstantiationException - if instantiation failed
SecurityException - if access to the required reflection information is denied

createObject

public static Object createObject(Class type,
                                  Object[] params,
                                  Class paramType)
                           throws InstantiationException,
                                  SecurityException
Creates an object of a specified type. The object must have a public constructor that accepts params.length arguments of the specified paramType.

Parameters:
type - the class of the object to create
params - the objects in this array are passed to the constructor; if null or empty, the default constructor is used
paramType - the type of the elements stored in the params array
Returns:
the created object; the returned object will be an instance of type
Throws:
InstantiationException - if instantiation failed
SecurityException - if access to the required reflection information is denied

createObject

public static Object createObject(Class type,
                                  Object[] params,
                                  Class[] paramTypes)
                           throws IllegalArgumentException,
                                  InstantiationException,
                                  SecurityException
Creates an object of a specified type. The object must have a public constructor that accepts params.length arguments of the specified types.

Parameters:
type - the class of the object to create
params - the objects in this array are passed to the constructor; if null or empty, the default constructor is used
paramTypes - the types of the elements stored in the params list
Returns:
the created object; the returned object will be an instance of type
Throws:
IllegalArgumentException - if params and paramTypes are of different lengths
InstantiationException - if instantiation failed
SecurityException - if access to the required reflection information is denied

formatDurationInMillisecs

public static String formatDurationInMillisecs(long msecs)
Formats a duration in XML Schema format (without using years and months). The generated string has the form "PdaysDThoursHminutesMsecs.millisecsS" -- left-side fields that are 0 are omitted.

Parameters:
msecs - the number of milliseconds
Returns:
a formatted representation as specified above

formatDurationInSeconds

public static String formatDurationInSeconds(double secs)
                                      throws IllegalArgumentException
Formats a duration in XML Schema format (without using years and months). The generated string has the form "PdaysDThoursHminutesMsecs.millisecsS" -- left-side fields that are 0 are omitted.

Parameters:
secs - the number of seconds; must be nonnegative
Returns:
a formatted representation as specified above
Throws:
IllegalArgumentException - if secs < 0

ensureNonNegative

public static void ensureNonNegative(double number,
                                     String identifier)
                              throws IllegalArgumentException
Ensures that a number is positive or 0, throwing an exception if this condition is violated.

Parameters:
number - the number to check
identifier - string used to describe the number in the thrown exception
Throws:
IllegalArgumentException - if number < 0.0

ensureNonNegative

public static void ensureNonNegative(long number,
                                     String identifier)
                              throws IllegalArgumentException
Ensures that a number is positive or 0, throwing an exception if this condition is violated.

Parameters:
number - the number to check
identifier - string used to describe the number in the thrown exception
Throws:
IllegalArgumentException - if number < 0

format

public static String format(double number)
Formats the given number, using at most 4 fraction digits.

Parameters:
number - the number for format
Returns:
the formatted number

format

public static String format(double number,
                            int maxFractDigits)
Formats the given number, using at most the specified number of fraction digits.

Parameters:
number - the number for format
maxFractDigits - maximum number of digits allowed in the fraction portion of the number
Returns:
the formatted number

lastN

public static ArrayList lastN(List list,
                              int number)
Copied the last n elements from a list into a new list (or all elements, if the size of the input list is smaller or equal to n). Modifications of the returned list will not affect the original list and vice versa.

If number is 0 or negative or if the original list is null or empty, an empty list is returned.

Note that this is somewhat inefficient if the input list is a LinkedList because repeated calls to List.get(int) are necessary (unless the whole list is copied).

Parameters:
list - the input list
number - the number of elements to copy
Returns:
an ArrayList containing the last elements from the original list; or an empty ArrayList iff the list is null

showDuration

public static String showDuration(long startTime)
Calculated and prints the time passed since a given start time (in English).

Parameters:
startTime - the start time in milliseconds
Returns:
a formatted string containing the time difference between the start time and the current time


Copyright © 2003-2004 Christian Siefkes. All Rights Reserved.