Package com.blackrook.rookscript.struct
Class Utils
java.lang.Object
com.blackrook.rookscript.struct.Utils
Utility functions.
- Author:
- Matthew Tropiano
-
Field Summary
Modifier and TypeFieldDescriptionstatic final InputStream
A null input stream (never has data to read).static final OutputStream
A null output stream (eats all data written). -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T
arrayElement
(T[] array, int index) Gets the element at an index in the array, but returns null if the index is outside of the array bounds.static <T> void
arraySwap
(T[] array, int a, int b) Swaps the contents of two indices of an array.static double
clampValue
(double val, double lo, double hi) Coerces a double to the range bounded by lo and hi.static void
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
Creates a new instance of a class from a class type.static <T> T
createForType
(Object object, Class<T> targetType) Creates a new instance of an object for placement in a POJO or elsewhere.static double
degToRad
(double degrees) Converts degrees to radians.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 double
Gets a double floating-point number from an array.static Object
getFieldValue
(Object instance, Field field) Gets the value of a field on an object.static String
getFileExtension
(File file, String extensionSeparator) Returns the extension of a file's name.static String
getFileExtension
(String filename, String extensionSeparator) Returns the extension of a filename.static String
getFileNameWithoutExtension
(File file, String extensionSeparator) Returns the file's name, no extension.static String
getFileNameWithoutExtension
(String filename, String extensionSeparator) Returns the file's name, no extension.static float
Gets a floating-point number from an array.static int
getInteger
(ByteOrder order, byte[] data, int offset) Gets an integer from an array.static long
Gets a long integer from an array.static <T> TypeProfileFactory.Profile<T>
getProfile
(Class<T> clazz) Creates a new profile for a provided type.static short
Gets a short from an array.static long
getUnsignedInteger
(ByteOrder order, byte[] data, int offset) Gets an unsigned integer from an array.static int
getUnsignedShort
(ByteOrder order, byte[] data, int offset) Gets an unsigned short from an array.static Object
invokeBlind
(Method method, Object instance) Blindly invokes a method, with no parameters, only throwing aRuntimeException
if something goes wrong.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
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 double
linearInterpolate
(double factor, double x, double y) Gives a value that is the result of a linear interpolation between two values.static InputStream
openResource
(String pathString) Opens anInputStream
to a resource using the current thread'sClassLoader
.static double
parseDouble
(String s, double def) Attempts to parse a double from a string.static long
Attempts to parse a long from a string.static void
Puts a double floating-point number into an array.static void
Puts a floating-point number into an array.static void
putInteger
(int value, ByteOrder order, byte[] data, int offset) Puts an integer into an array.static void
Puts a long integer into an array.static void
Puts a short into an array.static void
putUnsignedInteger
(long value, ByteOrder order, byte[] data, int offset) Puts an unsigned integer into an array.static void
putUnsignedShort
(int value, ByteOrder order, byte[] data, int offset) Puts an unsigned short into an array.static <T extends Comparable<T>>
voidquicksort
(T[] array) Performs an in-place QuickSort on the provided array.static <T extends Comparable<T>>
voidquicksort
(T[] array, int lo, int hi) Performs an in-place QuickSort on the provided array within an interval of indices.static <T> void
quicksort
(T[] array, int lo, int hi, Comparator<? super T> comparator) Performs an in-place QuickSort on the provided array within an interval of indices.static <T> void
quicksort
(T[] array, Comparator<? super T> comparator) Performs an in-place QuickSort on the provided array using a compatible Comparator.static double
radToDeg
(double radians) Converts radians to degrees.static int
Reads a set of bytes until end-of-stream or file.static int
Reads a set of bytes until end-of-stream or file.static int
relay
(DataInput in, DataOutput out, int bufferSize, int maxLength) Reads from an input stream, reading in a consistent set of data and writing it to the output stream.static void
setFieldValue
(Object instance, Field field, Object value) Sets the value of a field on an object.static <T extends Comparable<T>>
intsortFrom
(T[] array, int index) Shifts an object to an appropriate position according to the object'sComparable.compareTo(Object)
function.static <T> int
sortFrom
(T[] array, int index, Comparator<? super T> comparator) Shifts an object to an appropriate position according to the providedcomparator
function.static double
wrapValue
(double val, double lo, double hi) Coerces a double to the range bounded by lo and hi, by "wrapping" the value.
-
Field Details
-
NULL_INPUT
A null input stream (never has data to read). -
NULL_OUTPUT
A null output stream (eats all data written).
-
-
Constructor Details
-
Utils
public Utils()
-
-
Method Details
-
isWindows
public static boolean isWindows()- Returns:
- true if we using Windows.
-
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.
-
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.
-
radToDeg
public static double radToDeg(double radians) Converts radians to degrees.- Parameters:
radians
- the input angle in radians.- Returns:
- the resultant angle in degrees.
-
degToRad
public static double degToRad(double degrees) Converts degrees to radians.- Parameters:
degrees
- the input angle in degrees.- Returns:
- the resultant angle in radians.
-
clampValue
public static double clampValue(double val, double lo, double hi) Coerces a double to the range bounded by lo and hi.
Example: clampValue(32,-16,16) returns 16.
Example: clampValue(4,-16,16) returns 4.
Example: clampValue(-1000,-16,16) returns -16.- Parameters:
val
- the double.lo
- the lower bound.hi
- the upper bound.- Returns:
- the value after being "forced" into the range.
-
wrapValue
public static double wrapValue(double val, double lo, double hi) Coerces a double to the range bounded by lo and hi, by "wrapping" the value.
Example: wrapValue(32,-16,16) returns 0.
Example: wrapValue(4,-16,16) returns 4.
Example: wrapValue(-1000,-16,16) returns 8.- Parameters:
val
- the double.lo
- the lower bound.hi
- the upper bound.- Returns:
- the value after being "wrapped" into the range.
-
linearInterpolate
public static double linearInterpolate(double factor, double x, double y) Gives a value that is the result of a linear interpolation between two values.- Parameters:
factor
- the interpolation factor.x
- the first value.y
- the second value.- Returns:
- the interpolated value.
-
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.
-
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.
-
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, with no parameters, only throwing aRuntimeException
if something goes wrong. Here for the convenience of not making a billion try/catch clauses for a method invocation. This method exists to avoid unnecessary memory allocations.- Parameters:
method
- the method to invoke.instance
- the object instance that is the method target.- 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:
-
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:
-
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.
-
arrayElement
public static <T> T arrayElement(T[] array, int index) Gets the element at an index in the array, but returns null if the index is outside of the array bounds.- Type Parameters:
T
- the array type.- Parameters:
array
- the array to use.index
- the index to use.- Returns:
array[index]
or null if out of bounds.
-
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
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.
-
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.
-
openResource
Opens anInputStream
to a resource using the current thread'sClassLoader
.- Parameters:
pathString
- the resource pathname.- Returns:
- an open
InputStream
for reading the resource or null if not found. - See Also:
-
relay
public static int relay(DataInput in, DataOutput out, int bufferSize, int maxLength) throws IOException Reads from an input stream, reading in a consistent set of data and writing it to the output stream. The read/write is buffered so that it does not bog down the OS's other I/O requests. This method finishes when the end of the source stream is reached. Note that this may block if the input stream is a type of stream that will block if the input stream blocks for additional input. This method is thread-safe.- Parameters:
in
- the input stream to grab data from.out
- the output stream to write the data to.bufferSize
- the buffer size for the I/O. Must be > 0.maxLength
- the maximum amount of bytes to relay, or a value < 0 for no max.- Returns:
- the total amount of bytes relayed.
- Throws:
IOException
- if a read or write error occurs.
-
close
Attempts to close anAutoCloseable
object. If the object is null, this does nothing.- Parameters:
c
- the reference to the AutoCloseable object.
-
getProfile
Creates a new profile for a provided type. Generated profiles are stored in memory, and retrieved again by class type.This method is thread-safe.
- Type Parameters:
T
- the class type.- Parameters:
clazz
- the class.- Returns:
- a new profile.
-
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.
-
createForType
Creates a new instance of an object for placement in a POJO or elsewhere.- Type Parameters:
T
- the return object type.- Parameters:
object
- the object to convert to another objecttargetType
- the target class type to convert to, if the types differ.- Returns:
- a suitable object of type
targetType
. - Throws:
ClassCastException
- if the incoming type cannot be converted.
-
arraySwap
public static <T> void arraySwap(T[] array, int a, int b) Swaps the contents of two indices of an array.- Type Parameters:
T
- the object type stored in the array.- Parameters:
array
- the input array.a
- the first index.b
- the second index.
-
sortFrom
Shifts an object to an appropriate position according to the object'sComparable.compareTo(Object)
function.- Type Parameters:
T
- the object type stored in the array that extendsComparable
.- Parameters:
array
- the array to shift the contents of.index
- the index to add it to (the contents are replaced).- Returns:
- the final index in the array of the sorted object.
-
sortFrom
Shifts an object to an appropriate position according to the providedcomparator
function.- Type Parameters:
T
- the object type stored in the arrays.- Parameters:
array
- the array to shift the contents of.index
- the index to add it to (the contents are replaced).comparator
- the comparator to use.- Returns:
- the final index in the array of the sorted object.
-
quicksort
Performs an in-place QuickSort on the provided array. The array's contents will change upon completion. Convenience method forquicksort(array, 0, array.length - 1);
- Type Parameters:
T
- the object type stored in the array that extendsComparable
.- Parameters:
array
- the input array.
-
quicksort
Performs an in-place QuickSort on the provided array using a compatible Comparator. The array's contents will change upon completion. Convenience method forquicksort(array, 0, array.length - 1, comparator);
- Type Parameters:
T
- the object type stored in the array.- Parameters:
array
- the input array.comparator
- the comparator to use for comparing.
-
quicksort
Performs an in-place QuickSort on the provided array within an interval of indices. The array's contents will change upon completion. Iflo
is greater thanhi
, this does nothing.- Type Parameters:
T
- the object type stored in the array that extendsComparable
.- Parameters:
array
- the input array.lo
- the low index to start the sort (inclusive).hi
- the high index to start the sort (inclusive).
-
quicksort
Performs an in-place QuickSort on the provided array within an interval of indices. The array's contents will change upon completion. Iflo
is greater thanhi
, this does nothing.- Type Parameters:
T
- the object type stored in the array.- Parameters:
array
- the input array.lo
- the low index to start the sort (inclusive).hi
- the high index to start the sort (inclusive).comparator
- the comparator to use for comparing.
-
getFileExtension
Returns the extension of a filename.- Parameters:
filename
- the file name.extensionSeparator
- the text or characters that separates file name from extension.- Returns:
- the file's extension, or an empty string for no extension.
-
getFileExtension
Returns the extension of a file's name.- Parameters:
file
- the file.extensionSeparator
- the text or characters that separates file name from extension.- Returns:
- the file's extension, or an empty string for no extension.
-
getFileNameWithoutExtension
Returns the file's name, no extension.- Parameters:
file
- the file.extensionSeparator
- the text or characters that separates file name from extension.- Returns:
- the file's name without extension.
-
getFileNameWithoutExtension
Returns the file's name, no extension.- Parameters:
filename
- the file name.extensionSeparator
- the text or characters that separates file name from extension.- Returns:
- the file's name without extension.
-
putShort
Puts a short into an array. Writes 2 bytes.- Parameters:
value
- the value to convert.order
- the byte ordering.data
- the output array.offset
- the offset into the array to write.- Throws:
ArrayIndexOutOfBoundsException
- ifoffset + 2
exceedsdata.length
.
-
read
Reads a set of bytes until end-of-stream or file.- Parameters:
input
- the DataInput to read from.- Returns:
- the byte read (0 - 255), or -1 if end-of-stream/file at time of call.
- Throws:
IOException
- if a read error occurs.
-
read
Reads a set of bytes until end-of-stream or file.- Parameters:
input
- the DataInput to read from.data
- the recipient array for the byte data.offset
- the starting offset into the array to put the bytes.length
- the maximum amount of bytes to read.- Returns:
- the amount of bytes read, or -1 if end-of-stream/file at time of call.
- Throws:
IOException
- if a read error occurs.ArrayIndexOutOfBoundsException
- ifoffset + length
breaches the end of the data array.
-
getShort
Gets a short from an array. Reads 2 bytes.- Parameters:
order
- the byte ordering.data
- the input array.offset
- the offset into the array to read.- Returns:
- the resultant short.
- Throws:
ArrayIndexOutOfBoundsException
- ifoffset + 2
exceedsdata.length
.
-
putUnsignedShort
Puts an unsigned short into an array. Writes 2 bytes.- Parameters:
value
- the value to convert.order
- the byte ordering.data
- the output array.offset
- the offset into the array to write.- Throws:
ArrayIndexOutOfBoundsException
- ifoffset + 2
exceedsdata.length
.
-
getUnsignedShort
Gets an unsigned short from an array. Reads 2 bytes.- Parameters:
order
- the byte ordering.data
- the input array.offset
- the offset into the array to read.- Returns:
- the resultant short.
- Throws:
ArrayIndexOutOfBoundsException
- ifoffset + 2
exceedsdata.length
.
-
putInteger
Puts an integer into an array. Writes 4 bytes.- Parameters:
value
- the value to convert.order
- the byte ordering.data
- the output array.offset
- the offset into the array to write.- Throws:
ArrayIndexOutOfBoundsException
- ifoffset + 4
exceedsdata.length
.
-
getInteger
Gets an integer from an array. Reads 4 bytes.- Parameters:
order
- the byte ordering.data
- the input array.offset
- the offset into the array to read.- Returns:
- the resultant integer.
- Throws:
ArrayIndexOutOfBoundsException
- ifoffset + 4
exceedsdata.length
.
-
putUnsignedInteger
Puts an unsigned integer into an array. Writes 4 bytes.- Parameters:
value
- the value to convert.order
- the byte ordering.data
- the output array.offset
- the offset into the array to write.- Throws:
ArrayIndexOutOfBoundsException
- ifoffset + 4
exceedsdata.length
.
-
getUnsignedInteger
Gets an unsigned integer from an array. Reads 4 bytes.- Parameters:
order
- the byte ordering.data
- the input array.offset
- the offset into the array to read.- Returns:
- the resultant integer.
- Throws:
ArrayIndexOutOfBoundsException
- ifoffset + 4
exceedsdata.length
.
-
putFloat
Puts a floating-point number into an array. Writes 4 bytes.- Parameters:
value
- the value to convert.order
- the byte ordering.data
- the output array.offset
- the offset into the array to write.- Throws:
ArrayIndexOutOfBoundsException
- ifoffset + 4
exceedsdata.length
.
-
getFloat
Gets a floating-point number from an array. Reads 4 bytes.- Parameters:
order
- the byte ordering.data
- the input array.offset
- the offset into the array to read.- Returns:
- the resultant float.
- Throws:
ArrayIndexOutOfBoundsException
- ifoffset + 4
exceedsdata.length
.
-
putLong
Puts a long integer into an array. Writes 8 bytes.- Parameters:
value
- the value to convert.order
- the byte ordering.data
- the output array.offset
- the offset into the array to write.- Throws:
ArrayIndexOutOfBoundsException
- ifoffset + 8
exceedsdata.length
.
-
getLong
Gets a long integer from an array. Reads 8 bytes.- Parameters:
order
- the byte ordering.data
- the input array.offset
- the offset into the array to read.- Returns:
- the resultant integer.
- Throws:
ArrayIndexOutOfBoundsException
- ifoffset + 8
exceedsdata.length
.
-
putDouble
Puts a double floating-point number into an array. Writes 8 bytes.- Parameters:
value
- the value to convert.order
- the byte ordering.data
- the output array.offset
- the offset into the array to write.- Throws:
ArrayIndexOutOfBoundsException
- ifoffset + 8
exceedsdata.length
.
-
getDouble
Gets a double floating-point number from an array. Reads 8 bytes.- Parameters:
order
- the byte ordering.data
- the input array.offset
- the offset into the array to read.- Returns:
- the resultant float.
- Throws:
ArrayIndexOutOfBoundsException
- ifoffset + 8
exceedsdata.length
.
-