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.ObjectcreateInstance(java.lang.String className)Creates and returns an instance of the specified class name usingClass.newInstance().static java.lang.ObjectcreateInstance(java.lang.String className, boolean failSilent)Creates and returns an instance of the specified class name usingClass.newInstance().static java.lang.ObjectcreateInstance(java.lang.String className, java.lang.Class superType)Creates and returns an instance of the specified class name usingClass.newInstance().static java.lang.ObjectcreateInstance(java.lang.String className, java.lang.Class superType, boolean failSilent)Creates and returns an instance of the specified class name usingClass.newInstance().static floatgetFloat(java.lang.String data, float defaultValue)Returns afloatvalue for the specifiedString.static java.lang.ObjectgetInstance(java.lang.String className)Returns an instance of the specified class name.static java.lang.ObjectgetInstance(java.lang.String className, boolean failSilent)Returns an instance of the specified class name.static intgetInt(java.lang.String data)Returns anintvalue for the specifiedString.static intgetInt(java.lang.String data, int defaultValue)Returns anintvalue for the specifiedString.static java.lang.ObjectgetValue(java.lang.Object obj, java.lang.String fieldName)Returns the value of the specifiedfieldNamewithin the specifiedObject.static booleanisChanged(java.lang.Object oldObj, java.lang.Object newObj)Checks for inequality between the two specifiedObjects.static booleanisEmpty(java.lang.String data)Returnstrueif the specifiedStringisnullor contains only whitespace.static booleanisEqual(java.lang.Object obj1, java.lang.Object obj2)Checks for equality between the two specifiedObjects.static voidput(java.util.Map map, java.lang.Object key, java.lang.Object value)Puts the suppliedvalueinto the specifiedMapusing the specifiedkey.static booleansetValue(java.lang.Object obj, java.lang.String fieldName, java.lang.Object value)Returns the value of the specifiedfieldNamewithin the specifiedObject.static voidsleep(long millis)Puts the currentThreadto sleep for the specified timeout.static booleansysTrue(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 istrueif the version is 1.4.
-
JAVA_1_5
public static final boolean JAVA_1_5
A constant representing the Java version. This constant istrueif 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 anintvalue for the specifiedString. This method callsInteger.parseInt(String s)and returns the resultingintvalue. If anyExceptionis thrown, this method returns a value of0.- Parameters:
data- aStringcontaining theintrepresentation 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 anintvalue for the specifiedString. This method callsInteger.parseInt(String s)and returns the resultingintvalue. If anyExceptionis thrown, this method returns the value supplied by thedefaultValueparameter.- Parameters:
data- aStringcontaining theintrepresentation to be parseddefaultValue- the value to return if anExceptionis 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 afloatvalue for the specifiedString. This method callsFloat.parseFloat(String s)and returns the resultingfloatvalue. If anyExceptionis thrown byparseFloat, this method returns the value supplied by thedefaultValueparameter.- Parameters:
data- aStringcontaining thefloatrepresentation to be parseddefaultValue- the value to return if anExceptionis 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)
Returnstrueif the specifiedStringisnullor contains only whitespace. Otherwise, returnsfalse. The whitespace check is performed by callingtrim()and checking to see if the trimmed stringlength()is zero.- Parameters:
data- theStringto check for non-whitespace content- Returns:
trueif the specifiedStringisnullor contains only whitespace,falseotherwise.
-
getInstance
public static java.lang.Object getInstance(java.lang.String className)
Returns an instance of the specified class name. IfclassNameisnull, then this method returns anullreference.This method will try two different means of obtaining an instance of
className. First, it will attempt to resolve theClassofclassNameviaClass.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, theThrowableis caught and this method dispatches tocreateInstance(String className, boolean failSilent)with an argument offalseforfailSilent.createInstance(String className, boolean failSilent)will attempt to invokenewInstance()on theClassfor the specified class name. If anyThrowableis encountered during this process, the value offalseforfailSilentwill cause the stack trace to be printed to theSystem.errand anullreference 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. IfclassNameisnull, then this method returns anullreference.This method will try two different means of obtaining an instance of
className. First, it will attempt to resolve theClassofclassNameviaClass.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, theThrowableis 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 theClassfor the specified class name. If anyThrowableis encountered during this process, the value offailSilentis checked to determine whether the stack stack trace should be printed to theSystem.err. Anullreference will be returned if any problems are encountered.- Parameters:
className- the fully qualified name of the desired class.failSilent-trueif the stack trace should not be printed to theSystem.errwhen aThrowableis caught,falseotherwise.- 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(). IfclassNameisnull, then this method returns anullreference. This dispatches tocreateInstance(String className, Class superType, boolean failSilent)with an argument ofnullforsuperTypeandfalseforfailSilent.This method will attempt to resolve the
ClassofclassNameviaClass.forName(String className). No class assignability checkes are performed because this method uses anullsuperType.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 offalseforfailSilentwill ensure the stack stack trace is be printed to theSystem.err. Anullreference 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(). IfclassNameisnull, then this method returns anullreference. ThefailSilentparameter will determine whether error stack traces should be reported to theSystem.errbefore this method returnsnull. This method dispatches tocreateInstance(String className, Class superType, boolean failSilent)with an argument ofnullforsuperType.This method will attempt to resolve the
ClassofclassNameviaClass.forName(String className). No class assignability checkes are performed because this method uses anullsuperType.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 offailSilentis checked to determine whether the stack stack trace should be printed to theSystem.err. Anullreference will be returned if any problems are encountered.- Parameters:
className- the fully qualified name of the desired class.failSilent-trueif the stack trace should not be printed to theSystem.errwhen aThrowableis caught,falseotherwise.- 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(). IfclassNameisnull, then this method returns anullreference. IfsuperTypeis 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 offalseforfailSilent.This method will attempt to resolve the
ClassofclassNameviaClass.forName(String className). IfsuperTypeis 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 aClassCastExceptionis thrown and caught internally and this method returnsnull. IfsuperTypeisnull, 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 offalseforfailSilentwill ensure the stack stack trace is be printed to theSystem.err. Anullreference 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(). IfclassNameisnull, then this method returns anullreference. IfsuperTypeis non-null, then this method will enforce polymorphic identity viaClass.isAssignableFrom(Class cls). ThefailSilentparameter will determine whether error stack traces should be reported to theSystem.errbefore this method returnsnull.This method will attempt to resolve the
ClassofclassNameviaClass.forName(String className). IfsuperTypeis 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 aClassCastExceptionis thrown and caught internally and this method returnsnull. IfsuperTypeisnull, 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 offailSilentis checked to determine whether the stack stack trace should be printed to theSystem.err. Anullreference 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-trueif the stack trace should not be printed to theSystem.errwhen aThrowableis caught,falseotherwise.- 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 sameObjectreference 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-nullwith differentObjectreferences, then this method returns the value ofobj1.equals(obj2).This method is the exact opposite of
isChanged(Object oldObj, Object newObj).- Parameters:
obj1- the firstObjectto be checked for equalityobj2- the secondObjectto be checked for equality- Returns:
trueif theObjectsare equal,falseotherwise.- 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 sameObjectreference 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-nullwith differentObjectreferences, 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 firstObjectto be checked for inequalitynewObj- the secondObjectto be checked for inequality- Returns:
falseif theObjectsare equal,trueotherwise.- See Also:
isEqual(Object, Object),Object.equals(java.lang.Object)
-
sysTrue
public static boolean sysTrue(java.lang.String key)
Deprecated.UseBoolean.getBoolean(String).Returnstrueif there is currently aSystemproperty with the specifiedkeywhose value is "true". If theSystemproperty does not exist, or the value is inequal to "true", this method returnsfalse. This method returnsfalseif the specifiedkeyparameter isnull.- Parameters:
key- theSystemproperty to test.- Returns:
trueif there is currently aSystemproperty with the specifiedkeywhose 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 suppliedvalueinto the specifiedMapusing the specifiedkey. This is a convenience method to automate null-checks. Avalueparameter ofnullis interpreted as a removal from the specifiedMaprather than anputoperation.If either
maporkeyarenullthen this method returns with no action taken. Ifvalueisnull, then this method callsmap.remove(key). Otherwise, this method callsmap.put(key, value).- Parameters:
map- theMapwhose 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 specifiedfieldNamewithin 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 returntrueif the operation was successful andfalseif errors were encountered.This method calls
obj.getClass()to retrieve theClassof 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 originalObjectandvalueparameter as arguments. Before returning, the field's accessibility is reset to its original state.If either
objorfieldNamearenull, 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 calledfieldNamedoes not exist on the class). AllThrowablesencountered 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/falseof 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 ofobjbeing 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.IllegalAccessExceptionReturns the value of the specifiedfieldNamewithin 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 theClassof 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 originalObjectparameter as an argument. Before returning, the field's accessibility is reset to its original state.If either
objorfieldNamearenull, 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 calledfieldNamedoes not exist on the class). AllThrowablesencountered by this method will be rethrown asIllegalAccessException. For wrappedThrowables, the original cause can be accessed viaIllegalAccessException'sgetCause()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 currentThreadto sleep for the specified timeout. This method callsThread.sleep(long millis), catching any thrownInterruptedExceptionand printing a stack trace to theSystem.err.- Parameters:
millis- the length of time to sleep in milliseconds.- See Also:
Thread.sleep(long)
-
-