Package com.blackrook.redis.struct
Class Utils
- java.lang.Object
-
- com.blackrook.redis.struct.Utils
-
public final class Utils extends Object
A utility class.- Author:
- Matthew Tropiano
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
close(Closeable c)
Attempts to close aCloseable
object.static void
close(AutoCloseable c)
Attempts to close anAutoCloseable
object.static <T> T
construct(Constructor<T> constructor, Object... params)
Creates a new instance of a class from a class type.static <T> T
create(Class<T> clazz)
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 String
getTextualContents(File f)
Retrieves the textual contents of a file in the system's current encoding.static String
getTextualContents(InputStream in)
Retrieves the textual contents of a stream in the system's current encoding.static String
getTextualContents(InputStream in, String encoding)
Retrieves the textual contents of a stream.static Object
invokeBlind(Method method, Object instance, Object... params)
Blindly invokes a method, only throwing aRuntimeException
if something goes wrong.static boolean
isArray(Class<?> clazz)
Tests if a class is actually an array type.static boolean
isArray(Object object)
Tests if an object is actually an array type.static <T> T
isNull(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 boolean
parseBoolean(String s)
Attempts to parse a boolean from a string.static byte
parseByte(String s)
Attempts to parse a byte from a string.static char
parseChar(String s)
Attempts to parse a char from a string.static double
parseDouble(String s)
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 int
parseInt(String s)
Attempts to parse an int from a string.static long
parseLong(String s)
Attempts to parse a long from a string.static long
parseLong(String s, long def)
Attempts to parse a long from a string.static short
parseShort(String s)
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.static void
sleep(long millis)
Calls Thread.sleep() but in an encapsulated try to avoid catching InterruptedException.static void
sleep(long millis, int nanos)
Calls Thread.sleep() but in an encapsulated try to avoid catching InterruptedException.
-
-
-
Method Detail
-
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
public static <T> T create(Class<T> clazz)
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
public static <T> T construct(Constructor<T> constructor, Object... params)
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
public static void setFieldValue(Object instance, Field field, Object value)
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:
Field.set(Object, Object)
-
getFieldValue
public static Object getFieldValue(Object instance, Field field)
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:
Field.set(Object, Object)
-
invokeBlind
public static Object invokeBlind(Method method, Object instance, Object... params)
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:
Method.invoke(Object, Object...)
-
getEnumInstance
public static <T extends Enum<T>> T getEnumInstance(String value, Class<T> enumClass)
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
public static boolean isArray(Class<?> clazz)
Tests if a class is actually an array type.- Parameters:
clazz
- the class to test.- Returns:
- true if so, false if not.
-
isArray
public static boolean isArray(Object object)
Tests if an object is actually an array type.- Parameters:
object
- the object to test.- Returns:
- true if so, false if not.
-
getArrayType
public static Class<?> getArrayType(Class<?> arrayType)
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
public static Class<?> getArrayType(Object object)
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.
-
getArrayDimensions
public static int getArrayDimensions(Class<?> arrayType)
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
public static int getArrayDimensions(Object array)
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
public static void close(Closeable c)
Attempts to close aCloseable
object. If the object is null, this does nothing.- Parameters:
c
- the reference to the closeable object.
-
close
public static void close(AutoCloseable c)
Attempts to close anAutoCloseable
object. If the object is null, this does nothing.- Parameters:
c
- the reference to the AutoCloseable object.
-
getTextualContents
public static String getTextualContents(File f) throws IOException
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
public static String getTextualContents(InputStream in) throws IOException
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
public static String getTextualContents(InputStream in, String encoding) throws IOException
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
public static boolean parseBoolean(String s)
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
public static byte parseByte(String s)
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
public static short parseShort(String s)
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
public static char parseChar(String s)
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
public static int parseInt(String s)
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
public static long parseLong(String s)
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
public static long parseLong(String s, long def)
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
public static float parseFloat(String s)
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
public static double parseDouble(String s)
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
public static double parseDouble(String s, double def)
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(long)
-
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:
sleep(long, int)
-
-