|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectde.fu_berlin.ties.util.Util
public final class 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 String |
CONFIG_LOGGER_LOG
Configuration key: Only messages with this priority or higher are logged. |
static String |
CONFIG_LOGGER_SHOW
Configuration key: Only messages with this priority or higher are written to standard output (but only if covered by CONFIG_LOGGER_LOG ). |
static char |
FALSE_CHAR
Character representing the boolean value false : '-'. |
static org.apache.log.Logger |
LOG
The logger used in the TIE system. |
static String |
NAN
String recognized as Not-a-Number when parsing floating-point numbers: "NaN" (ignoring case). |
static char |
TRUE_CHAR
Character representing the boolean value true : '+'. |
Constructor Summary | |
---|---|
Util()
Util instances should NOT be constructed in standard
programming. |
Method Summary | |
---|---|
static void |
appendPositiveNumber(StringBuilder appender,
long number,
int minDigits,
String prefix,
boolean appendAlways)
Appends a non-negative number to a string buffer. |
static boolean |
asBoolean(char character)
Converts a character into a boolean primitive. |
static boolean |
asBoolean(Object obj)
Converts an object into a boolean 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 |
configureLog(TiesConfiguration config)
Reconfigures the logger from the CONFIG_LOGGER_LOG
and CONFIG_LOGGER_SHOW values in the provided configuration. |
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. |
static String |
formatDurationInSeconds(double secs)
Formats a duration. |
static long |
longHash(String string)
Returns a long hash code for a string. |
static Random |
reproducibleRandom()
Returns an instance of a pseudo-random number generator that uses a fixed seed, so the same sequence of calls to each object created by this method will yield the same sequence of pseudo-random numbers. |
static String |
showDuration(long startTime)
Calculated and prints the time passed since a given start time (in English). |
static char |
toChar(boolean value)
Converts a boolean value to a single character representing this value. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final org.apache.log.Logger LOG
INFO
or higher are also written
to standard out.
public static final String CONFIG_LOGGER_LOG
public static final String CONFIG_LOGGER_SHOW
CONFIG_LOGGER_LOG
).
public static final String NAN
public static final char TRUE_CHAR
true
: '+'.
public static final char FALSE_CHAR
false
: '-'.
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 void appendPositiveNumber(StringBuilder appender, long number, int minDigits, String prefix, boolean appendAlways) throws IllegalArgumentException
appender
- the string buffer to append tonumber
- the number to append; must not be negativeminDigits
- the minimum number of digits to printprefix
- a prefix to print in front of the number, if not
null
appendAlways
- if true
, the number is always printed;
otherwise only positive numbers are printed -- in this case the appender
is not modified if the number is 0
IllegalArgumentException
- if number < 0
public static boolean asBoolean(char character) throws IllegalArgumentException
TRUE_CHAR
is
converted to true
; FALSE_CHAR
is
converted to false
. An exception is thrown in all other
cases.
character
- the character to convert
IllegalArgumentException
- if the character cannot be converted
into a booleanpublic 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. If the string contains a
single character, the result of asBoolean(char)
is returned.
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 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 string value to equal to NAN
(ignoring case),
Double.NaN
is returned.
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 string value to equal to NAN
(ignoring case),
Float.NaN
is returned.
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)
CollUtils.flatten(Object[])
is returned;
for other non-null objects, the output of Object.toString()
is
returned; otherwise, null
is returned.
obj
- the object to check
Object.toString()
; or
null
if obj
is null
public static void configureLog(TiesConfiguration config)
logger
from the CONFIG_LOGGER_LOG
and CONFIG_LOGGER_SHOW
values in the provided configuration.
This method should be used with care since it affects the global logging.
It is called by Main.main(String[])
after
configurations and command-line propertis have been read.
config
- the configuration to usepublic 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 long longHash(String string)
String.hashCode()
method, but returns a long (64 bit) value
instead of an integer (32 bit). The hash code for a is computed as
usings[0]*313^(n-1) + s[1]*313^(n-2) + ... + s[n-1]
int
arithmetic, where s[i]
is the
ith character of the string, n
is the length of
the string, and ^
indicates exponentiation.
(The hash value of the empty string is zero.)
string
- the string to hash
public static Random reproducibleRandom()
public static String showDuration(long startTime)
startTime
- the start time in milliseconds
public static char toChar(boolean value)
value
- the boolean value to convert
TRUE_CHAR
if the value is true
;
FALSE_CHAR
otherwise
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |