Package jisa
Class Util
- java.lang.Object
-
- jisa.Util
-
public class Util extends java.lang.Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
Util.Build<T>
static interface
Util.MapBuild<K,V>
static class
Util.MapBuilder<K,V>
-
Field Summary
Fields Modifier and Type Field Description static java.io.PrintStream
errLog
-
Constructor Summary
Constructors Constructor Description Util()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
addShutdownHook(SRunnable toRun)
static boolean
areAnyNull(java.lang.Object... objects)
static <T> T
build(T toBuild, Util.Build<T> builder)
static <K,V>
Util.MapBuilder<K,V>buildMap(java.lang.Class<K> keyType, java.lang.Class<V> valueType)
static <K,V>
java.util.Map<K,V>buildMap(Util.MapBuild<K,V> build)
static <T> T
castOrDefault(java.lang.Object toCast, T orElse)
static javafx.scene.image.Image
colourImage(javafx.scene.image.Image toColour, javafx.scene.paint.Color colour)
static java.lang.String
colourToCSS(javafx.scene.paint.Color colour)
static void
exceptionHandler(java.lang.Exception e)
Pass an exception to the standard exception handler.static java.lang.String
getCurrentTimeString()
static double
getNiceValue(double range, boolean round)
static javafx.scene.image.Image
invertImage(javafx.scene.image.Image toInvert)
static boolean
isBetween(java.lang.Number value, java.lang.Number min, java.lang.Number max)
Checks whether a number is within a range.static boolean
isValidIndex(int index, boolean[] array)
static boolean
isValidIndex(int index, byte[] array)
static boolean
isValidIndex(int index, char[] array)
static boolean
isValidIndex(int index, double[] array)
static boolean
isValidIndex(int index, float[] array)
static boolean
isValidIndex(int index, int[] array)
static boolean
isValidIndex(int index, long[] array)
static boolean
isValidIndex(int index, short[] array)
static boolean
isValidIndex(int index, java.lang.Object[] array)
static double[]
iterableToArray(java.lang.Iterable<java.lang.Double> iterable)
static <T> java.util.List<T>
iterableToList(java.lang.Iterable<T> iterable)
static <U,V>
voiditerateCombined(java.lang.Iterable<U> x, java.lang.Iterable<V> y, java.util.function.BiConsumer<U,V> forEach)
static <T> void
iterateCombined(java.util.function.Consumer<T[]> forEach, java.lang.Iterable<T>... iterables)
static java.lang.String
joinDoubles(java.lang.String delim, java.util.Collection<java.lang.Double> doubles)
static java.lang.String
joinInts(java.lang.String delim, int... ints)
static java.lang.String
joinPath(java.lang.String first, java.lang.String... more)
static int[]
makeCountingArray(int start, int stop)
static java.lang.String[]
makeCountingString(int start, int length, java.lang.String pattern)
Returns an array of Strings specified by the pattern and a counting integer.static double[]
makeLinearArray(java.lang.Number min, java.lang.Number max, int numSteps)
Creates an equally spaced array of numbers, starting at min, ending at max in numSteps steps.static double[]
makeLogarithmicArray(java.lang.Number min, java.lang.Number max, int numSteps)
Creates an array of logarithmically spaced numbers.static double[]
makeSymLinearArray(java.lang.Number min, java.lang.Number max, int stepsEachWay)
Creates an equally spaced symmetric array of numbers, starting at min, ending at max in numSteps steps, and then back again to min in numSteps.static <K,V>
java.util.Map<K,V>mapOf(java.lang.Object... values)
static java.lang.String
msToPaddedString(long millis)
static java.lang.String
msToString(long millis)
static double
oneSigFigCeil(double value)
Ceilings the number to 1 significant figure.static double
oneSigFigFloor(java.lang.Number value)
Floors the number to 1 significant figure.static void
openInBrowser(java.lang.String url)
static byte[]
padArray(byte[] toTrim)
static java.lang.String
pathToSVG(javafx.scene.shape.Path path)
static java.lang.String
pluralise(java.lang.String word, java.lang.Number value)
static java.lang.String
pluralise(java.lang.String word, java.lang.String plural, java.lang.Number value)
static java.lang.String
polygonToSVG(javafx.scene.shape.Polygon path)
static double[]
primitiveArray(java.lang.Double... values)
static double[]
reverseArray(double[] toReverse)
Reverses an array of doubles.static <T> T[]
reverseArray(T[] toReverse)
static double
roundSigFig(double value, int nSigDig, int dir)
static void
runAsync(java.lang.Runnable toRun)
static void
runInParallel(SRunnable... runnables)
Runs multiple Runnables in parallel, returning only when all have completed.static void
runRegardless(SRunnable toRun)
static void
runRegardless(SRunnable... toRun)
static void
setExceptionHandler(ERunnable handler)
Set what the standard exception handler should be.static void
sleep(long msec)
Sleep function that doesn't throw interrupted exceptions.static double[]
symArray(double[] array)
Takes an array of doubles, reverses it and appends it onto the end of the original whilst avoiding repeating the last element.static byte[]
trimBytes(byte[] toTrim)
static byte[]
trimBytes(byte[] toTrim, int start, int length)
static byte[]
trimBytes(java.nio.ByteBuffer buffer, int start, int length)
static int
truncate(int value, int min, int max)
static double
truncate(java.lang.Number value, java.lang.Number min, java.lang.Number max)
-
-
-
Method Detail
-
sleep
public static void sleep(long msec)
Sleep function that doesn't throw interrupted exceptions. Upon an interrupt it will simply stop sleeping.- Parameters:
msec
- Number of milliseconds to sleep for
-
pluralise
public static java.lang.String pluralise(java.lang.String word, java.lang.Number value)
-
pluralise
public static java.lang.String pluralise(java.lang.String word, java.lang.String plural, java.lang.Number value)
-
runRegardless
public static void runRegardless(SRunnable toRun)
-
runRegardless
public static void runRegardless(SRunnable... toRun)
-
msToString
public static java.lang.String msToString(long millis)
-
msToPaddedString
public static java.lang.String msToPaddedString(long millis)
-
joinPath
public static java.lang.String joinPath(java.lang.String first, java.lang.String... more)
-
colourToCSS
public static java.lang.String colourToCSS(javafx.scene.paint.Color colour)
-
getNiceValue
public static double getNiceValue(double range, boolean round)
-
truncate
public static double truncate(java.lang.Number value, java.lang.Number min, java.lang.Number max)
-
truncate
public static int truncate(int value, int min, int max)
-
runAsync
public static void runAsync(java.lang.Runnable toRun)
-
pathToSVG
public static java.lang.String pathToSVG(javafx.scene.shape.Path path)
-
polygonToSVG
public static java.lang.String polygonToSVG(javafx.scene.shape.Polygon path)
-
addShutdownHook
public static void addShutdownHook(SRunnable toRun)
-
invertImage
public static javafx.scene.image.Image invertImage(javafx.scene.image.Image toInvert)
-
runInParallel
public static void runInParallel(SRunnable... runnables) throws java.lang.InterruptedException
Runs multiple Runnables in parallel, returning only when all have completed.- Parameters:
runnables
- Runnables to run- Throws:
java.lang.InterruptedException
- Upon thread being interrupted
-
colourImage
public static javafx.scene.image.Image colourImage(javafx.scene.image.Image toColour, javafx.scene.paint.Color colour)
-
isBetween
public static boolean isBetween(java.lang.Number value, java.lang.Number min, java.lang.Number max)
Checks whether a number is within a range.- Parameters:
value
- Value to checkmin
- Minimum of rangemax
- Maximum of range- Returns:
- Is it bound by the range?
-
mapOf
public static <K,V> java.util.Map<K,V> mapOf(java.lang.Object... values)
-
buildMap
public static <K,V> Util.MapBuilder<K,V> buildMap(java.lang.Class<K> keyType, java.lang.Class<V> valueType)
-
buildMap
public static <K,V> java.util.Map<K,V> buildMap(Util.MapBuild<K,V> build)
-
castOrDefault
public static <T> T castOrDefault(java.lang.Object toCast, T orElse)
-
build
public static <T> T build(T toBuild, Util.Build<T> builder)
-
isValidIndex
public static boolean isValidIndex(int index, java.lang.Object[] array)
-
isValidIndex
public static boolean isValidIndex(int index, double[] array)
-
isValidIndex
public static boolean isValidIndex(int index, float[] array)
-
isValidIndex
public static boolean isValidIndex(int index, int[] array)
-
isValidIndex
public static boolean isValidIndex(int index, long[] array)
-
isValidIndex
public static boolean isValidIndex(int index, short[] array)
-
isValidIndex
public static boolean isValidIndex(int index, boolean[] array)
-
isValidIndex
public static boolean isValidIndex(int index, char[] array)
-
isValidIndex
public static boolean isValidIndex(int index, byte[] array)
-
setExceptionHandler
public static void setExceptionHandler(ERunnable handler)
Set what the standard exception handler should be.- Parameters:
handler
- Lambda
-
exceptionHandler
public static void exceptionHandler(java.lang.Exception e)
Pass an exception to the standard exception handler.- Parameters:
e
- Exception to handle
-
makeLinearArray
public static double[] makeLinearArray(java.lang.Number min, java.lang.Number max, int numSteps)
Creates an equally spaced array of numbers, starting at min, ending at max in numSteps steps.- Parameters:
min
- Number to start atmax
- Number to end atnumSteps
- Number of steps to do it in- Returns:
- Array of numbers
-
makeCountingArray
public static int[] makeCountingArray(int start, int stop)
-
joinInts
public static java.lang.String joinInts(java.lang.String delim, int... ints)
-
joinDoubles
public static java.lang.String joinDoubles(java.lang.String delim, java.util.Collection<java.lang.Double> doubles)
-
makeSymLinearArray
public static double[] makeSymLinearArray(java.lang.Number min, java.lang.Number max, int stepsEachWay)
Creates an equally spaced symmetric array of numbers, starting at min, ending at max in numSteps steps, and then back again to min in numSteps.- Parameters:
min
- Number to start atmax
- Number to end atstepsEachWay
- Number of steps each way- Returns:
- Array of numbers
-
symArray
public static double[] symArray(double[] array)
Takes an array of doubles, reverses it and appends it onto the end of the original whilst avoiding repeating the last element.- Parameters:
array
- Array to symmetrise- Returns:
- Symmetrised array
-
reverseArray
public static double[] reverseArray(double[] toReverse)
Reverses an array of doubles.- Parameters:
toReverse
- Array to reverse- Returns:
- Reversed array
-
primitiveArray
public static double[] primitiveArray(java.lang.Double... values)
-
reverseArray
public static <T> T[] reverseArray(T[] toReverse)
-
makeLogarithmicArray
public static double[] makeLogarithmicArray(java.lang.Number min, java.lang.Number max, int numSteps)
Creates an array of logarithmically spaced numbers.- Parameters:
min
- Value to start atmax
- Value to end atnumSteps
- Number of steps- Returns:
- Logarithmic array
-
areAnyNull
public static boolean areAnyNull(java.lang.Object... objects)
-
getCurrentTimeString
public static java.lang.String getCurrentTimeString()
-
openInBrowser
public static void openInBrowser(java.lang.String url)
-
makeCountingString
public static java.lang.String[] makeCountingString(int start, int length, java.lang.String pattern)
Returns an array of Strings specified by the pattern and a counting integer.- Parameters:
start
- Integer to start atlength
- How many integers to go throughpattern
- The pattern to use- Returns:
- Array of Strings
-
oneSigFigFloor
public static double oneSigFigFloor(java.lang.Number value)
Floors the number to 1 significant figure.- Parameters:
value
- Number to floor.- Returns:
- Floored number
-
oneSigFigCeil
public static double oneSigFigCeil(double value)
Ceilings the number to 1 significant figure.- Parameters:
value
- Number to floor.- Returns:
- Ceilinged number
-
roundSigFig
public static double roundSigFig(double value, int nSigDig, int dir)
-
trimBytes
public static byte[] trimBytes(byte[] toTrim)
-
trimBytes
public static byte[] trimBytes(java.nio.ByteBuffer buffer, int start, int length)
-
trimBytes
public static byte[] trimBytes(byte[] toTrim, int start, int length)
-
padArray
public static byte[] padArray(byte[] toTrim)
-
iterateCombined
public static <U,V> void iterateCombined(java.lang.Iterable<U> x, java.lang.Iterable<V> y, java.util.function.BiConsumer<U,V> forEach)
-
iterateCombined
public static <T> void iterateCombined(java.util.function.Consumer<T[]> forEach, java.lang.Iterable<T>... iterables)
-
iterableToArray
public static double[] iterableToArray(java.lang.Iterable<java.lang.Double> iterable)
-
iterableToList
public static <T> java.util.List<T> iterableToList(java.lang.Iterable<T> iterable)
-
-