Package com.blackrook.json.struct
Class Utils
java.lang.Object
com.blackrook.json.struct.Utils
A utility class.
- Author:
- Matthew Tropiano
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T
construct
(Constructor<T> constructor, Object... params) Creates a new instance of a class from a class type.static <T> T
Creates a new instance of a class from a class type.static int
getArrayDimensions
(Class<?> arrayType) Gets how many dimensions that this array, represented by the provided type, has.static int
getArrayDimensions
(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 Object
getFieldValue
(Object instance, Field field) Gets the value of a field on an object.static Object
invokeBlind
(Method method, Object instance, Object... params) Blindly invokes a method, only throwing aRuntimeException
if something goes wrong.static boolean
Tests if a class is actually an array type.static boolean
Tests if an object is actually an array type.static boolean
Checks if a value is "empty." The following is considered "empty": Null references.static <T> T
isNull
(T testObject, T nullReturn) Returns the first object if it is not null, otherwise returns the second.static boolean
Attempts to parse a boolean from a string.static boolean
parseBoolean
(String s, boolean def) Attempts to parse a boolean from a string.static byte
Attempts to parse a byte from a string.static byte
Attempts to parse a byte from a string.static char
Attempts to parse a char from a string.static char
Attempts to parse a byte from a string.static double
Attempts to parse a double from a string.static double
parseDouble
(String s, double def) Attempts to parse a double from a string.static float
parseFloat
(String s) Attempts to parse a float from a string.static float
parseFloat
(String s, float def) Attempts to parse a float from a string.static int
Attempts to parse an int from a string.static int
Attempts to parse an int from a string.static long
Attempts to parse a long from a string.static long
Attempts to parse a long from a string.static short
parseShort
(String s) Attempts to parse a short from a string.static short
parseShort
(String s, short def) Attempts to parse a short from a string.static void
setFieldValue
(Object instance, Field field, Object value) Sets the value of a field on an object.
-
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 aRuntimeException
if 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.
Array
objects that have a length of 0.Boolean
objects that are false.Character
objects that are the null character ('\0', ' ').Number
objects that are zero.String
objects that are the empty string, or areString.trim()
'ed down to the empty string.Collection
objects 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.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.
-