|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectde.fu_berlin.ties.util.Util
A static class that provides general utility methods. No instances of this class can be created, only the static members should be used.
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 |
public static final Logger LOG
INFO
or higher are also written
to standard out.
Constructor Detail |
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 |
public static Set arrayAsSet(Object[] array)
array
- the array to wrap
public static boolean asBoolean(Object obj) throws IllegalArgumentException
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.
obj
- the object to check
IllegalArgumentException
- if the object's toString()
output cannot be parsed as a booleanpublic static byte asByte(Object obj) throws NumberFormatException
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.
obj
- the object to check
NumberFormatException
- if the object's toString()
output does not contain a parsable bytepublic static char asChar(Object obj) throws IndexOutOfBoundsException
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.
obj
- the object to check
IndexOutOfBoundsException
- if the object's toString()
output is the empty string (after trimming outer whitespace)public static double asDouble(Object obj) throws NumberFormatException
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.
obj
- the object to check
NumberFormatException
- if the object's toString()
output does not contain a parsable doublepublic static float asFloat(Object obj) throws NumberFormatException
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.
obj
- the object to check
NumberFormatException
- if the object's toString()
output does not contain a parsable floatpublic static int asInt(Object obj) throws NumberFormatException
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.
obj
- the object to check
NumberFormatException
- if the object's toString()
output does not contain a parsable intpublic static long asLong(Object obj) throws NumberFormatException
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.
obj
- the object to check
NumberFormatException
- if the object's toString()
output does not contain a parsable longpublic static short asShort(Object obj) throws NumberFormatException
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.
obj
- the object to check
NumberFormatException
- if the object's toString()
output does not contain a parsable shortpublic static String asString(Object obj)
Object.toString()
is returned; otherwise,
null
is returned.
obj
- the object to check
Object.toString()
; or
null
if obj
is null
public static void combineArrays(Object[] array1, Object[] array2, Object[] targetArray)
array1
- the first array to copyarray2
- the second array to copytargetArray
- 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
public static Object createObject(String[] classDef) throws IllegalArgumentException, ClassNotFoundException, InstantiationException, SecurityException
createObject(Class, String[])
, reading the
class name from the first element in the array.
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
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 deniedpublic static Object createObject(Class type, String[] params) throws InstantiationException, SecurityException
createObject(Class, Object[], Class)
, setting
the paramType
to the String
class.
type
- the class of the object to createparams
- the objects in this array are passed to the constructor;
if null
or empty, the default constructor is used
type
InstantiationException
- if instantiation failed
SecurityException
- if access to the required reflection
information is deniedpublic static Object createObject(Class type, Object[] params, Class paramType) throws InstantiationException, SecurityException
params.length
arguments of the
specified paramType
.
type
- the class of the object to createparams
- the objects in this array are passed to the constructor;
if null
or empty, the default constructor is usedparamType
- the type of the elements stored in the
params
array
type
InstantiationException
- if instantiation failed
SecurityException
- if access to the required reflection
information is deniedpublic static Object createObject(Class type, Object[] params, Class[] paramTypes) throws IllegalArgumentException, InstantiationException, SecurityException
params.length
arguments of the
specified types.
type
- the class of the object to createparams
- the objects in this array are passed to the constructor;
if null
or empty, the default constructor is usedparamTypes
- the types of the elements stored in the
params
list
type
IllegalArgumentException
- if params
and
paramTypes
are of different lengths
InstantiationException
- if instantiation failed
SecurityException
- if access to the required reflection
information is deniedpublic static String formatDurationInMillisecs(long msecs)
msecs
- the number of milliseconds
public static String formatDurationInSeconds(double secs) throws IllegalArgumentException
secs
- the number of seconds; must be nonnegative
IllegalArgumentException
- if secs < 0
public static void ensureNonNegative(double number, String identifier) throws IllegalArgumentException
number
- the number to checkidentifier
- string used to describe the number in the thrown
exception
IllegalArgumentException
- if number < 0.0
public static void ensureNonNegative(long number, String identifier) throws IllegalArgumentException
number
- the number to checkidentifier
- string used to describe the number in the thrown
exception
IllegalArgumentException
- if number < 0
public static String format(double number)
number
- the number for format
public static String format(double number, int maxFractDigits)
number
- the number for formatmaxFractDigits
- maximum number of digits allowed in the fraction
portion of the number
public static ArrayList lastN(List list, int number)
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).
list
- the input listnumber
- the number of elements to copy
list
is null
public static String showDuration(long startTime)
startTime
- the start time in milliseconds
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |