Package com.blackrook.sql.struct
Class Utils
java.lang.Object
com.blackrook.sql.struct.Utils
A utility class.
- Author:
- Matthew Tropiano
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Tconstruct(Constructor<T> constructor, Object... params) Creates a new instance of a class from a class type.static <T> TCreates a new instance of a class from a class type.static intgetArrayDimensions(Class<?> arrayType) Gets how many dimensions that this array, represented by the provided type, has.static intgetArrayDimensions(Object array) Gets how many array dimensions that an object (presumably an array) has.static Class<?>getArrayType(Class<?> arrayType) Gets the class type of this array type, if this is an array type.static Class<?>getArrayType(Object object) Gets the class type of this array, if this is an array.static <T extends Enum<T>>
TgetEnumInstance(String value, Class<T> enumClass) Returns the enum instance of a class given class and name, or null if not a valid name.static ObjectgetFieldValue(Object instance, Field field) Gets the value of a field on an object.static ObjectinvokeBlind(Method method, Object instance, Object... params) Blindly invokes a method, only throwing aRuntimeExceptionif something goes wrong.static booleanTests if a class is actually an array type.static booleanTests if an object is actually an array type.static booleanChecks if a value is "empty." The following is considered "empty": Null references.static <T> TisNull(T testObject, T nullReturn) Returns the first object if it is not null, otherwise returns the second.static booleanAttempts to parse a boolean from a string.static booleanparseBoolean(String s, boolean def) Attempts to parse a boolean from a string.static byteAttempts to parse a byte from a string.static byteAttempts to parse a byte from a string.static charAttempts to parse a char from a string.static charAttempts to parse a byte from a string.static doubleAttempts to parse a double from a string.static doubleparseDouble(String s, double def) Attempts to parse a double from a string.static floatparseFloat(String s) Attempts to parse a float from a string.static floatparseFloat(String s, float def) Attempts to parse a float from a string.static intAttempts to parse an int from a string.static intAttempts to parse an int from a string.static longAttempts to parse a long from a string.static longAttempts to parse a long from a string.static shortparseShort(String s) Attempts to parse a short from a string.static shortparseShort(String s, short def) Attempts to parse a short from a string.static voidsetFieldValue(Object instance, Field field, Object value) Sets the value of a field on an object.
-
Constructor Details
-
Utils
public Utils()
-
-
Method Details
-
isNull
public static <T> T isNull(T testObject, T nullReturn) Returns the first object if it is not null, otherwise returns the second.- Type Parameters:
T- class that extends Object.- Parameters:
testObject- the first ("tested") object.nullReturn- the object to return if testObject is null.- Returns:
- testObject if not null, nullReturn otherwise.
-
setFieldValue
Sets the value of a field on an object.- Parameters:
instance- the object instance to set the field on.field- the field to set.value- the value to set.- Throws:
NullPointerException- if the field or object provided is null.ClassCastException- if the value could not be cast to the proper type.RuntimeException- if anything goes wrong (bad field name, bad target, bad argument, or can't access the field).- See Also:
-
getFieldValue
Gets the value of a field on an object.- Parameters:
instance- the object instance to get the field value of.field- the field to get the value of.- Returns:
- the current value of the field.
- Throws:
NullPointerException- if the field or object provided is null.RuntimeException- if anything goes wrong (bad target, bad argument, or can't access the field).- See Also:
-
invokeBlind
Blindly invokes a method, only throwing aRuntimeExceptionif something goes wrong. Here for the convenience of not making a billion try/catch clauses for a method invocation.- Parameters:
method- the method to invoke.instance- the object instance that is the method target.params- the parameters to pass to the method.- Returns:
- the return value from the method invocation. If void, this is null.
- Throws:
ClassCastException- if one of the parameters could not be cast to the proper type.RuntimeException- if anything goes wrong (bad target, bad argument, or can't access the method).- See Also:
-
isEmpty
Checks if a value is "empty." The following is considered "empty":- Null references.
Arrayobjects that have a length of 0.Booleanobjects that are false.Characterobjects that are the null character ('\0', ' ').Numberobjects that are zero.Stringobjects that are the empty string, or areString.trim()'ed down to the empty string.Collectionobjects whereCollection.isEmpty()returns true.
- Parameters:
obj- the object to check.- Returns:
- true if the provided object is considered "empty", false otherwise.
-
isArray
Tests if a class is actually an array type.- Parameters:
clazz- the class to test.- Returns:
- true if so, false if not.
-
isArray
Tests if an object is actually an array type.- Parameters:
object- the object to test.- Returns:
- true if so, false if not.
-
getArrayDimensions
Gets how many dimensions that this array, represented by the provided type, has.- Parameters:
arrayType- the type to inspect.- Returns:
- the number of array dimensions, or 0 if not an array.
-
getArrayDimensions
Gets how many array dimensions that an object (presumably an array) has.- Parameters:
array- the object to inspect.- Returns:
- the number of array dimensions, or 0 if not an array.
-
getArrayType
Gets the class type of this array type, if this is an array type.- Parameters:
arrayType- the type to inspect.- Returns:
- this array's type, or null if the provided type is not an array, or if the found class is not on the classpath.
-
getArrayType
Gets the class type of this array, if this is an array.- Parameters:
object- the object to inspect.- Returns:
- this array's type, or null if the provided object is not an array, or if the found class is not on the classpath.
-
create
Creates a new instance of a class from a class type. This essentially callsClass.getDeclaredConstructor(Class...)with no arguments andClass.newInstance(), but wraps the call in a try/catch block that only throws an exception if something goes wrong.- Type Parameters:
T- the return object type.- Parameters:
clazz- the class type to instantiate.- Returns:
- a new instance of an object.
- Throws:
RuntimeException- if instantiation cannot happen, either due to a non-existent constructor or a non-visible constructor.
-
construct
Creates a new instance of a class from a class type. This essentially callsClass.newInstance(), but wraps the call in a try/catch block that only throws an exception if something goes wrong.- Type Parameters:
T- the return object type.- Parameters:
constructor- the constructor to call.params- the constructor parameters.- Returns:
- a new instance of an object created via the provided constructor.
- Throws:
RuntimeException- if instantiation cannot happen, either due to a non-existent constructor or a non-visible constructor.
-
getEnumInstance
Returns the enum instance of a class given class and name, or null if not a valid name. If value is null, this returns null.- Type Parameters:
T- the Enum object type.- Parameters:
value- the value to search for.enumClass- the Enum class to inspect.- Returns:
- the enum value or null if the target does not exist.
-
parseBoolean
Attempts to parse a boolean from a string. If the string is null, this returns false. If the string does not equal "true" (case ignored), this returns false.- Parameters:
s- the input string.- Returns:
- the interpreted boolean.
-
parseByte
Attempts to parse a byte from a string. If the string is null or the empty string, this returns 0.- Parameters:
s- the input string.- Returns:
- the interpreted byte.
-
parseShort
Attempts to parse a short from a string. If the string is null or the empty string, this returns 0.- Parameters:
s- the input string.- Returns:
- the interpreted short.
-
parseChar
Attempts to parse a char from a string. If the string is null or the empty string, this returns '\0'.- Parameters:
s- the input string.- Returns:
- the first character in the string.
-
parseInt
Attempts to parse an int from a string. If the string is null or the empty string, this returns 0.- Parameters:
s- the input string.- Returns:
- the interpreted integer.
-
parseLong
Attempts to parse a long from a string. If the string is null or the empty string, this returns 0.- Parameters:
s- the input string.- Returns:
- the interpreted long integer.
-
parseFloat
Attempts to parse a float from a string. If the string is null or the empty string, this returns 0.0f.- Parameters:
s- the input string.- Returns:
- the interpreted float.
-
parseDouble
Attempts to parse a double from a string. If the string is null or the empty string, this returns 0.0.- Parameters:
s- the input string.- Returns:
- the interpreted double.
-
parseBoolean
Attempts to parse a boolean from a string. If the string is null or the empty string, this returnsdef. If the string does not equal "true," this returns false.- Parameters:
s- the input string.def- the fallback value to return.- Returns:
- the interpreted boolean or def if the input string is blank.
-
parseByte
Attempts to parse a byte from a string. If the string is null or the empty string, this returnsdef.- Parameters:
s- the input string.def- the fallback value to return.- Returns:
- the interpreted byte or def if the input string is blank.
-
parseShort
Attempts to parse a short from a string. If the string is null or the empty string, this returnsdef.- Parameters:
s- the input string.def- the fallback value to return.- Returns:
- the interpreted short or def if the input string is blank.
-
parseChar
Attempts to parse a byte from a string. If the string is null or the empty string, this returnsdef.- Parameters:
s- the input string.def- the fallback value to return.- Returns:
- the first character in the string or def if the input string is blank.
-
parseInt
Attempts to parse an int from a string. If the string is null or the empty string, this returnsdef.- Parameters:
s- the input string.def- the fallback value to return.- Returns:
- the interpreted integer or def if the input string is blank.
-
parseLong
Attempts to parse a long from a string. If the string is null or the empty string, this returnsdef.- Parameters:
s- the input string.def- the fallback value to return.- Returns:
- the interpreted long integer or def if the input string is blank.
-
parseFloat
Attempts to parse a float from a string. If the string is null or the empty string, this returnsdef.- Parameters:
s- the input string.def- the fallback value to return.- Returns:
- the interpreted float or def if the input string is blank.
-
parseDouble
Attempts to parse a double from a string. If the string is null or the empty string, this returnsdef.- Parameters:
s- the input string.def- the fallback value to return.- Returns:
- the interpreted double or def if the input string is blank.
-