Class Utils
java.lang.Object
com.blackrook.redis.struct.Utils
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidAttempts to close aCloseableobject.static voidAttempts to close anAutoCloseableobject.static <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 StringRetrieves the textual contents of a file in the system's current encoding.static StringRetrieves the textual contents of a stream in the system's current encoding.static StringgetTextualContents(InputStream in, String encoding) Retrieves the textual contents of a stream.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 <T> TisNull(T testObject, T nullReturn) Returns the first object if it is not null, otherwise returns the second.static <T> T[]joinArrays(T[]... arrays) Concatenates a set of arrays together, such that the contents of each array are joined into one array.static booleanAttempts to parse a boolean from a string.static byteAttempts to parse a byte from a string.static charAttempts to parse a char 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 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 voidsetFieldValue(Object instance, Field field, Object value) Sets the value of a field on an object.static voidsleep(long millis) Calls Thread.sleep() but in an encapsulated try to avoid catching InterruptedException.static voidsleep(long millis, int nanos) Calls Thread.sleep() but in an encapsulated try to avoid catching InterruptedException.
-
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.
-
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.
-
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:
-
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.
-
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.
-
getArrayType
-
getArrayType
-
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.
-
joinArrays
public static <T> T[] joinArrays(T[]... arrays) Concatenates a set of arrays together, such that the contents of each array are joined into one array. Null arrays are skipped.- Type Parameters:
T- the object type stored in the arrays.- Parameters:
arrays- the list of arrays.- Returns:
- a new array with all objects in each provided array added to the resultant one in the order in which they appear.
-
close
-
close
Attempts to close anAutoCloseableobject. If the object is null, this does nothing.- Parameters:
c- the reference to the AutoCloseable object.
-
getTextualContents
Retrieves the textual contents of a file in the system's current encoding.- Parameters:
f- the file to use.- Returns:
- a contiguous string (including newline characters) of the file's contents.
- Throws:
IOException- if the read cannot be done.
-
getTextualContents
Retrieves the textual contents of a stream in the system's current encoding.- Parameters:
in- the input stream to use.- Returns:
- a contiguous string (including newline characters) of the stream's contents.
- Throws:
IOException- if the read cannot be done.
-
getTextualContents
Retrieves the textual contents of a stream.- Parameters:
in- the input stream to use.encoding- name of the encoding type.- Returns:
- a contiguous string (including newline characters) of the stream's contents.
- Throws:
IOException- if the read cannot be done.
-
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.
-
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 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.
-
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.
-
sleep
public static void sleep(long millis) Calls Thread.sleep() but in an encapsulated try to avoid catching InterruptedException. Convenience method for making the current thread sleep when you don't care if it's interrupted or not and want to keep code neat.- Parameters:
millis- the amount of milliseconds to sleep.- See Also:
-
sleep
public static void sleep(long millis, int nanos) Calls Thread.sleep() but in an encapsulated try to avoid catching InterruptedException. Convenience method for making the current thread sleep when you don't care if it's interrupted or not and want to keep code neat.- Parameters:
millis- the amount of milliseconds to sleep.nanos- the amount of additional nanoseconds to sleep.- See Also:
-