Class Utilities
- java.lang.Object
-
- org.flexdock.util.Utilities
-
public class Utilities extends java.lang.Object
- Author:
- Christopher Butler
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static java.lang.Object
createInstance(java.lang.String className)
Creates and returns an instance of the specified class name usingClass.newInstance()
.static java.lang.Object
createInstance(java.lang.String className, boolean failSilent)
Creates and returns an instance of the specified class name usingClass.newInstance()
.static java.lang.Object
createInstance(java.lang.String className, java.lang.Class superType)
Creates and returns an instance of the specified class name usingClass.newInstance()
.static java.lang.Object
createInstance(java.lang.String className, java.lang.Class superType, boolean failSilent)
Creates and returns an instance of the specified class name usingClass.newInstance()
.static float
getFloat(java.lang.String data, float defaultValue)
Returns afloat
value for the specifiedString
.static java.lang.Object
getInstance(java.lang.String className)
Returns an instance of the specified class name.static java.lang.Object
getInstance(java.lang.String className, boolean failSilent)
Returns an instance of the specified class name.static int
getInt(java.lang.String data)
Returns anint
value for the specifiedString
.static int
getInt(java.lang.String data, int defaultValue)
Returns anint
value for the specifiedString
.static java.lang.Object
getValue(java.lang.Object obj, java.lang.String fieldName)
Returns the value of the specifiedfieldName
within the specifiedObject
.static boolean
isChanged(java.lang.Object oldObj, java.lang.Object newObj)
Checks for inequality between the two specifiedObjects
.static boolean
isEmpty(java.lang.String data)
Returnstrue
if the specifiedString
isnull
or contains only whitespace.static boolean
isEqual(java.lang.Object obj1, java.lang.Object obj2)
Checks for equality between the two specifiedObjects
.static void
put(java.util.Map map, java.lang.Object key, java.lang.Object value)
Puts the suppliedvalue
into the specifiedMap
using the specifiedkey
.static boolean
setValue(java.lang.Object obj, java.lang.String fieldName, java.lang.Object value)
Returns the value of the specifiedfieldName
within the specifiedObject
.static void
sleep(long millis)
Puts the currentThread
to sleep for the specified timeout.static boolean
sysTrue(java.lang.String key)
Deprecated.UseBoolean.getBoolean(String)
.
-
-
-
Field Detail
-
JAVA_1_4
public static final boolean JAVA_1_4
A constant representing the Java version. This constant istrue
if the version is 1.4.
-
JAVA_1_5
public static final boolean JAVA_1_5
A constant representing the Java version. This constant istrue
if the version is 1.5.
-
VERSION
public static final java.lang.String VERSION
A String representing the flexdock version. This constant is a string.- See Also:
- Constant Field Values
-
-
Method Detail
-
getInt
public static int getInt(java.lang.String data)
Returns anint
value for the specifiedString
. This method callsInteger.parseInt(String s)
and returns the resultingint
value. If anyException
is thrown, this method returns a value of0
.- Parameters:
data
- aString
containing theint
representation to be parsed- Returns:
- the integer value represented by the argument in decimal
- See Also:
getInt(String, int)
,Integer.parseInt(java.lang.String)
-
getInt
public static int getInt(java.lang.String data, int defaultValue)
Returns anint
value for the specifiedString
. This method callsInteger.parseInt(String s)
and returns the resultingint
value. If anyException
is thrown, this method returns the value supplied by thedefaultValue
parameter.- Parameters:
data
- aString
containing theint
representation to be parseddefaultValue
- the value to return if anException
is encountered.- Returns:
- the integer value represented by the argument in decimal
- See Also:
Integer.parseInt(java.lang.String)
-
getFloat
public static float getFloat(java.lang.String data, float defaultValue)
Returns afloat
value for the specifiedString
. This method callsFloat.parseFloat(String s)
and returns the resultingfloat
value. If anyException
is thrown byparseFloat
, this method returns the value supplied by thedefaultValue
parameter.- Parameters:
data
- aString
containing thefloat
representation to be parseddefaultValue
- the value to return if anException
is encountered on the underlying parse mechanism.- Returns:
- the floating-point value represented by the argument in decimal
- See Also:
Float.parseFloat(java.lang.String)
-
isEmpty
public static boolean isEmpty(java.lang.String data)
Returnstrue
if the specifiedString
isnull
or contains only whitespace. Otherwise, returnsfalse
. The whitespace check is performed by callingtrim()
and checking to see if the trimmed stringlength()
is zero.- Parameters:
data
- theString
to check for non-whitespace content- Returns:
true
if the specifiedString
isnull
or contains only whitespace,false
otherwise.
-
getInstance
public static java.lang.Object getInstance(java.lang.String className)
Returns an instance of the specified class name. IfclassName
isnull
, then this method returns anull
reference.This method will try two different means of obtaining an instance of
className
. First, it will attempt to resolve theClass
ofclassName
viaClass.forName(String className)
. It will then use reflection to search for a method on the class named"getInstance()"
. If the method is found, then it is invoked and the object instance is returned.If there are any problems encountered while attempting to invoke
getInstance()
on the specified class, theThrowable
is caught and this method dispatches tocreateInstance(String className, boolean failSilent)
with an argument offalse
forfailSilent
.createInstance(String className, boolean failSilent)
will attempt to invokenewInstance()
on theClass
for the specified class name. If anyThrowable
is encountered during this process, the value offalse
forfailSilent
will cause the stack trace to be printed to theSystem.err
and anull
reference will be returned.- Parameters:
className
- the fully qualified name of the desired class.- Returns:
- an instance of the specified class
- See Also:
getInstance(String, boolean)
,createInstance(String, boolean)
,Class.forName(java.lang.String)
,Class.getMethod(java.lang.String, java.lang.Class[])
,Method.invoke(java.lang.Object, java.lang.Object[])
,Class.newInstance()
-
getInstance
public static java.lang.Object getInstance(java.lang.String className, boolean failSilent)
Returns an instance of the specified class name. IfclassName
isnull
, then this method returns anull
reference.This method will try two different means of obtaining an instance of
className
. First, it will attempt to resolve theClass
ofclassName
viaClass.forName(String className)
. It will then use reflection to search for a method on the class named"getInstance()"
. If the method is found, then it is invoked and the object instance is returned.If there are any problems encountered while attempting to invoke
getInstance()
on the specified class, theThrowable
is caught and this method dispatches tocreateInstance(String className, boolean failSilent)
, passing the specified value forfailSilent
.createInstance(String className, boolean failSilent)
will attempt to invokenewInstance()
on theClass
for the specified class name. If anyThrowable
is encountered during this process, the value offailSilent
is checked to determine whether the stack stack trace should be printed to theSystem.err
. Anull
reference will be returned if any problems are encountered.- Parameters:
className
- the fully qualified name of the desired class.failSilent
-true
if the stack trace should not be printed to theSystem.err
when aThrowable
is caught,false
otherwise.- Returns:
- an instance of the specified class
- See Also:
createInstance(String, boolean)
,Class.forName(java.lang.String)
,Class.getMethod(java.lang.String, java.lang.Class[])
,Method.invoke(java.lang.Object, java.lang.Object[])
,Class.newInstance()
-
createInstance
public static java.lang.Object createInstance(java.lang.String className)
Creates and returns an instance of the specified class name usingClass.newInstance()
. IfclassName
isnull
, then this method returns anull
reference. This dispatches tocreateInstance(String className, Class superType, boolean failSilent)
with an argument ofnull
forsuperType
andfalse
forfailSilent
.This method will attempt to resolve the
Class
ofclassName
viaClass.forName(String className)
. No class assignability checkes are performed because this method uses anull
superType
.Once the desired class has been resolved, a new instance of it is created and returned by invoking its
newInstance()
method. If there are any problems encountered during this process, the value offalse
forfailSilent
will ensure the stack stack trace is be printed to theSystem.err
. Anull
reference will be returned if any problems are encountered.- Parameters:
className
- the fully qualified name of the desired class.- Returns:
- an instance of the specified class
- See Also:
createInstance(String, Class, boolean)
,Class.forName(java.lang.String)
,Class.newInstance()
-
createInstance
public static java.lang.Object createInstance(java.lang.String className, boolean failSilent)
Creates and returns an instance of the specified class name usingClass.newInstance()
. IfclassName
isnull
, then this method returns anull
reference. ThefailSilent
parameter will determine whether error stack traces should be reported to theSystem.err
before this method returnsnull
. This method dispatches tocreateInstance(String className, Class superType, boolean failSilent)
with an argument ofnull
forsuperType
.This method will attempt to resolve the
Class
ofclassName
viaClass.forName(String className)
. No class assignability checkes are performed because this method uses anull
superType
.Once the desired class has been resolved, a new instance of it is created and returned by invoking its
newInstance()
method. If there are any problems encountered during this process, the value offailSilent
is checked to determine whether the stack stack trace should be printed to theSystem.err
. Anull
reference will be returned if any problems are encountered.- Parameters:
className
- the fully qualified name of the desired class.failSilent
-true
if the stack trace should not be printed to theSystem.err
when aThrowable
is caught,false
otherwise.- Returns:
- an instance of the specified class
- See Also:
createInstance(String, Class, boolean)
,Class.forName(java.lang.String)
,Class.newInstance()
-
createInstance
public static java.lang.Object createInstance(java.lang.String className, java.lang.Class superType)
Creates and returns an instance of the specified class name usingClass.newInstance()
. IfclassName
isnull
, then this method returns anull
reference. IfsuperType
is non-null
, then this method will enforce polymorphic identity viaClass.isAssignableFrom(Class cls)
. This method dispatches tocreateInstance(String className, Class superType, boolean failSilent)
with an argument offalse
forfailSilent
.This method will attempt to resolve the
Class
ofclassName
viaClass.forName(String className)
. IfsuperType
is non-null
, then class identity is checked by callingsuperType.isAssignableFrom(c)
to ensure the resolved class is an valid equivalent, descendent, or implementation of the specifiedclassName
. If this check fails, then aClassCastException
is thrown and caught internally and this method returnsnull
. IfsuperType
isnull
, then no assignability checks are performed on the resolved class.Once the desired class has been resolved, a new instance of it is created and returned by invoking its
newInstance()
method. If there are any problems encountered during this process, the value offalse
forfailSilent
will ensure the stack stack trace is be printed to theSystem.err
. Anull
reference will be returned if any problems are encountered.- Parameters:
className
- the fully qualified name of the desired class.superType
- optional paramter used as a means of enforcing the inheritance hierarchy- Returns:
- an instance of the specified class
- See Also:
createInstance(String, Class, boolean)
,Class.forName(java.lang.String)
,Class.isAssignableFrom(java.lang.Class)
,Class.newInstance()
-
createInstance
public static java.lang.Object createInstance(java.lang.String className, java.lang.Class superType, boolean failSilent)
Creates and returns an instance of the specified class name usingClass.newInstance()
. IfclassName
isnull
, then this method returns anull
reference. IfsuperType
is non-null
, then this method will enforce polymorphic identity viaClass.isAssignableFrom(Class cls)
. ThefailSilent
parameter will determine whether error stack traces should be reported to theSystem.err
before this method returnsnull
.This method will attempt to resolve the
Class
ofclassName
viaClass.forName(String className)
. IfsuperType
is non-null
, then class identity is checked by callingsuperType.isAssignableFrom(c)
to ensure the resolved class is an valid equivalent, descendent, or implementation of the specifiedclassName
. If this check fails, then aClassCastException
is thrown and caught internally and this method returnsnull
. IfsuperType
isnull
, then no assignability checks are performed on the resolved class.Once the desired class has been resolved, a new instance of it is created and returned by invoking its
newInstance()
method. If there are any problems encountered during this process, the value offailSilent
is checked to determine whether the stack stack trace should be printed to theSystem.err
. Anull
reference will be returned if any problems are encountered.- Parameters:
className
- the fully qualified name of the desired class.superType
- optional paramter used as a means of enforcing the inheritance hierarchyfailSilent
-true
if the stack trace should not be printed to theSystem.err
when aThrowable
is caught,false
otherwise.- Returns:
- an instance of the specified class
- See Also:
Class.forName(java.lang.String)
,Class.isAssignableFrom(java.lang.Class)
,Class.newInstance()
-
isEqual
public static boolean isEqual(java.lang.Object obj1, java.lang.Object obj2)
Checks for equality between the two specifiedObjects
. If both arguments are the sameObject
reference using an==
relationship, then this method returnstrue
. Failing that check, if either of the arguments isnull
, then the other must not be and this method returnsfalse
. Finally, if both arguments are non-null
with differentObject
references, then this method returns the value ofobj1.equals(obj2)
.This method is the exact opposite of
isChanged(Object oldObj, Object newObj)
.- Parameters:
obj1
- the firstObject
to be checked for equalityobj2
- the secondObject
to be checked for equality- Returns:
true
if theObjects
are equal,false
otherwise.- See Also:
isChanged(Object, Object)
,Object.equals(java.lang.Object)
-
isChanged
public static boolean isChanged(java.lang.Object oldObj, java.lang.Object newObj)
Checks for inequality between the two specifiedObjects
. If both arguments are the sameObject
reference using an==
relationship, then this method returnsfalse
. Failing that check, if either of the arguments isnull
, then the other must not be and this method returnstrue
. Finally, if both arguments are non-null
with differentObject
references, then this method returns the opposite value ofobj1.equals(obj2)
.This method is the exact opposite of
isEqual(Object obj1, Object obj2)
.- Parameters:
oldObj
- the firstObject
to be checked for inequalitynewObj
- the secondObject
to be checked for inequality- Returns:
false
if theObjects
are equal,true
otherwise.- See Also:
isEqual(Object, Object)
,Object.equals(java.lang.Object)
-
sysTrue
public static boolean sysTrue(java.lang.String key)
Deprecated.UseBoolean.getBoolean(String)
.Returnstrue
if there is currently aSystem
property with the specifiedkey
whose value is "true". If theSystem
property does not exist, or the value is inequal to "true", this method returnsfalse
. This method returnsfalse
if the specifiedkey
parameter isnull
.- Parameters:
key
- theSystem
property to test.- Returns:
true
if there is currently aSystem
property with the specifiedkey
whose value is "true".- See Also:
System.getProperty(java.lang.String)
,String.equals(java.lang.Object)
-
put
public static void put(java.util.Map map, java.lang.Object key, java.lang.Object value)
Puts the suppliedvalue
into the specifiedMap
using the specifiedkey
. This is a convenience method to automate null-checks. Avalue
parameter ofnull
is interpreted as a removal from the specifiedMap
rather than anput
operation.If either
map
orkey
arenull
then this method returns with no action taken. Ifvalue
isnull
, then this method callsmap.remove(key)
. Otherwise, this method callsmap.put(key, value)
.- Parameters:
map
- theMap
whose contents is to be modifiedkey
- with which the specified value is to be associated.value
- value to be associated with the specified key.- See Also:
Map.put(java.lang.Object, java.lang.Object)
,Map.remove(java.lang.Object)
-
setValue
public static boolean setValue(java.lang.Object obj, java.lang.String fieldName, java.lang.Object value)
Returns the value of the specifiedfieldName
within the specifiedObject
. This is a convenience method for reflection hacks to retrieve the value of protected, private, or package-private field members while hiding the boilerplate reflection code within a single utility method call. This method will returntrue
if the operation was successful andfalse
if errors were encountered.This method calls
obj.getClass()
to retrieve theClass
of the specifiedObject
. It then retrieves the desired field by calling the classes'getDeclaredField(String name)
method, passing the specified field name. If the field is deemed inaccessible via it'sisAccessible()
method, then it is made accessible by callingsetAccessible(true)
. The field value is set by invoking the field'sset(Object obj, Object value)
method and passing the originalObject
andvalue
parameter as arguments. Before returning, the field's accessibility is reset to its original state.If either
obj
orfieldName
arenull
, then this method returnsfalse
.It should be understood that this method will not function properly for inaccessible fields in the presence of a
SecurityManager
. Nor will it function properly for non-existent fields (if a field calledfieldName
does not exist on the class). AllThrowables
encountered by this method will be caught and eaten and the method will returnfalse
. This works under the assumption that the operation might likely fail because the method itself is, in reality, a convenience hack. Therefore, specifics of any generated errors on the call stack are discarded and only the final outcome (true/false
of the operation is deemed relevant. If call stack data is required within the application for any thrown exceptions, then this method should not be used.}- Parameters:
obj
- the object for which the represented field's value is to be modifiedfieldName
- the name of the field to be setvalue
- the new value for the field ofobj
being modified- See Also:
Object.getClass()
,Class.getDeclaredField(java.lang.String)
,AccessibleObject.isAccessible()
,Field.setAccessible(boolean)
,Field.set(Object, Object)
-
getValue
public static java.lang.Object getValue(java.lang.Object obj, java.lang.String fieldName) throws java.lang.IllegalAccessException
Returns the value of the specifiedfieldName
within the specifiedObject
. This is a convenience method for reflection hacks to retrieve the value of protected, private, or package-private field members while hiding the boilerplate reflection code within a single utility method call.This method calls
obj.getClass()
to retrieve theClass
of the specifiedObject
. It then retrieves the desired field by calling the classes'getDeclaredField(String name)
method, passing the specified field name. If the field is deemed inaccessible via it'sisAccessible()
method, then it is made accessible by callingsetAccessible(true)
. The return value is retrieved by invoking the field'sget(Object obj)
method and passing the originalObject
parameter as an argument. Before returning, the field's accessibility is reset to its original state.If either
obj
orfieldName
arenull
, then this method returnsnull
.It should be understood that this method will not function properly for inaccessible fields in the presence of a
SecurityManager
. Nor will it function properly for non-existent fields (if a field calledfieldName
does not exist on the class). AllThrowables
encountered by this method will be rethrown asIllegalAccessException
. For wrappedThrowables
, the original cause can be accessed viaIllegalAccessException's
getCause()
method.- Parameters:
obj
- the object from which the represented field's value is to be extractedfieldName
- the name of the field to be checked- Returns:
- the value of the represented field in object
obj
; primitive values are wrapped in an appropriate object before being returned - Throws:
java.lang.IllegalAccessException
- See Also:
Object.getClass()
,Class.getDeclaredField(java.lang.String)
,AccessibleObject.isAccessible()
,Field.setAccessible(boolean)
,Field.get(java.lang.Object)
,Throwable.getCause()
-
sleep
public static void sleep(long millis)
Puts the currentThread
to sleep for the specified timeout. This method callsThread.sleep(long millis)
, catching any thrownInterruptedException
and printing a stack trace to theSystem.err
.- Parameters:
millis
- the length of time to sleep in milliseconds.- See Also:
Thread.sleep(long)
-
-