Package com.blackrook.expression.struct
Class Utils
- java.lang.Object
-
- com.blackrook.expression.struct.Utils
-
public final class Utils extends Object
-
-
Constructor Summary
Constructors Constructor Description Utils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description 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 double
degToRad(double degrees)
Converts degrees to radians.static byte[]
digest(byte[] bytes, String algorithmName)
Returns a hash of a set of bytes digested by an encryption algorithm.static boolean
isEmpty(Object obj)
Checks if a value is "empty." The following is considered "empty": Null references.static double
linearInterpolate(double factor, double x, double y)
Gives a value that is the result of a linear interpolation between two values.static double
radToDeg(double radians)
Converts radians to degrees.static byte[]
sha1(byte[] bytes)
Returns a 20-byte SHA-1 hash of a set of bytes.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.
-
-
-
Method Detail
-
isEmpty
public static boolean isEmpty(Object obj)
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.
-
sortFrom
public static <T extends Comparable<T>> int sortFrom(T[] array, int index)
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
public static <T> int sortFrom(T[] array, int index, Comparator<? super T> comparator)
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.
-
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.
-
digest
public static byte[] digest(byte[] bytes, String algorithmName)
Returns a hash of a set of bytes digested by an encryption algorithm. Can return null if this Java implementation cannot perform this. Do not use this if you care if the algorithm is provided or not.- Parameters:
bytes
- the bytes to encode.algorithmName
- the name to the algorithm to use.- Returns:
- the resultant byte digest, or null if the algorithm is not supported.
-
sha1
public static byte[] sha1(byte[] bytes)
Returns a 20-byte SHA-1 hash of a set of bytes. Can return null if this Java implementation cannot perform this, but it shouldn't, since SHA-1 is mandatorily implemented for all implementations.- Parameters:
bytes
- the input bytes.- Returns:
- the resultant 20-byte digest.
- See Also:
digest(byte[], String)
-
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.
-
-