Package jasima.core.util
Class Util
- java.lang.Object
-
- jasima.core.util.Util
-
public class Util extends Object
Some static utility methods that don't really fit anywhere else.- Author:
- Torsten Hildebrandt
-
-
Field Summary
Fields Modifier and Type Field Description static String[]DEF_CLASS_SEARCH_PATHClass search path containing all packaged in jasima-main.static ExecutorServiceDEF_POOLA default thread pool without an upper limit.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T[]addToArray(T[] a, Class<T> compType, T... newElement)Generic method to add an element to an array.static StringarrayToString(Object arbitraryArray)Converts an array (either Object[] or of a primitive type) to a String containing it's elements in square brackets.static doubledeleteArrayElement(double[] prios, int elemIdx, double fillWith)static <T> TdeleteArrayElement(T[] prios, int elemIdx, T fillWith)static StringexceptionToString(Throwable t)Converts an exception's stack trace to a single line string.static String[]filter(String[] ss, String prefix)static StringgetIdString()Returns a descriptive String showing name, current version and project URL.static StringgetJavaEnvString()Returns a string that characterizes the current Java environment by using various system properties.static StringgetOsEnvString()Returns a string that characterizes the host operating system by using various system properties.static StringgetVersion()Utility method to get the current version (obtained from git/Maven during the build).static StringgetWorkingDirString()static <T> T[]initializedArray(int numElements, Class<T> componentType)Returns a new array with a certain number of new objects of a certain type.static <T extends SimComponent>
T[]initializedCompArray(int numElements, Class<T> componentType, String namePrefix)static doublemax(double[] vs)static intmax(int[] vs)static intmaxIdx(double[] vs)static doublemean(Collection<? extends Number> coll)static doublemin(double[] vs)static intmin(int[] vs)static intminIdx(double[] vs)static intminIdx(double[] vs, int startIdx)static intminIdx(int[] vs)static intminIdx(int[] vs, int startIdx)static <T> TmoveArrayElementToBack(T[] prios, int elemIdx)static StringnextNonEmptyLine(BufferedReader r)static double[]parseDblList(String s)Converts a list of comma-separated double values (with dot as decimal separator) to a double-array.static int[]parseIntList(String list)Examples: "5" -> {5}; "23,5..10,3" -%gt; {23,5,6,7,8,9,10,3}; "1,2,3" -%gt; {1,2,3}static voidputMeanMaxVar(SummaryStat vs, String prefix, Map<String,Object> res)Convenience method to put mean, max and variance of a ValueStat object in a result map.static String[][]read2DimStrings(BufferedReader r, int numRows)static <T> T[]removeFromArray(T[] a, T elementToRemove)Generic method to remove the first occurrence of an element from an array.static double[]round(double[] vs, int decimals)Rounds all values in the double arrayvsto a certain number of decimal places.static doubleround(double val, int decimals)Rounds the given double value to a certain number of decimal places.static voidshuffle(double[] arr, Random rnd)Randomly permute the given double array.static voidshuffle(int[] arr, Random rnd)Randomly permute the given int array.static doublestdDev(Collection<? extends Number> coll)static doublesum(double[] productMix)static intsum(int[] is)
-
-
-
Field Detail
-
DEF_POOL
public static final ExecutorService DEF_POOL
A default thread pool without an upper limit. Used instead ofForkJoinPool.commonPool()but without the potential problems the work stealing algorithm can create.
-
DEF_CLASS_SEARCH_PATH
public static final String[] DEF_CLASS_SEARCH_PATH
Class search path containing all packaged in jasima-main.
-
-
Method Detail
-
exceptionToString
public static String exceptionToString(@Nullable Throwable t)
Converts an exception's stack trace to a single line string.- Parameters:
t- TheThrowableto convert to a String.- Returns:
- A String representation of
t.
-
initializedArray
public static <T> T[] initializedArray(int numElements, Class<T> componentType)Returns a new array with a certain number of new objects of a certain type.- Type Parameters:
T- The component type.- Parameters:
numElements- Number of elements in the result array.componentType- Class of the array elements.- Returns:
- The new array with all elements initialized with new objects.
-
initializedCompArray
public static <T extends SimComponent> T[] initializedCompArray(int numElements, Class<T> componentType, String namePrefix)
-
removeFromArray
public static <T> T[] removeFromArray(T[] a, T elementToRemove)Generic method to remove the first occurrence of an element from an array. A new array without the given element is returned (or the old array if element was not found).- Type Parameters:
T- Type of the array components.- Parameters:
a- The array to work with.elementToRemove- The element to remove froma.- Returns:
- The array
awith the first occurrence ofelementToRemoveremoved. If no such element cound be found,ais returned unchanged.
-
addToArray
@SafeVarargs public static <T> T[] addToArray(T[] a, Class<T> compType, T... newElement)
Generic method to add an element to an array. A new array additionally containing the given elements is returned.
-
nextNonEmptyLine
public static String nextNonEmptyLine(BufferedReader r) throws IOException
- Returns:
- the next non-empty line (everything after '#' is a comment and ignored unless it is escaped with a preceding back slash)
- Throws:
IOException
-
filter
public static String[] filter(String[] ss, String prefix)
- Returns:
- An array containing all entries of "ss" not starting with "prefix".
-
parseIntList
public static int[] parseIntList(String list)
Examples: "5" -> {5}; "23,5..10,3" -%gt; {23,5,6,7,8,9,10,3}; "1,2,3" -%gt; {1,2,3}
-
parseDblList
public static double[] parseDblList(String s)
Converts a list of comma-separated double values (with dot as decimal separator) to a double-array. Example: parseDblList("1.23,4.56") -> {1.23,4.56}
-
read2DimStrings
public static String[][] read2DimStrings(BufferedReader r, int numRows) throws IOException
- Throws:
IOException
-
deleteArrayElement
public static double deleteArrayElement(double[] prios, int elemIdx, double fillWith)
-
deleteArrayElement
public static <T> T deleteArrayElement(T[] prios, int elemIdx, T fillWith)
-
moveArrayElementToBack
public static <T> T moveArrayElementToBack(T[] prios, int elemIdx)
-
mean
public static double mean(Collection<? extends Number> coll)
-
stdDev
public static double stdDev(Collection<? extends Number> coll)
-
sum
public static double sum(double[] productMix)
-
sum
public static int sum(int[] is)
-
min
public static int min(int[] vs)
-
minIdx
public static int minIdx(int[] vs)
-
minIdx
public static int minIdx(int[] vs, int startIdx)
-
min
public static double min(double[] vs)
-
minIdx
public static int minIdx(double[] vs)
-
minIdx
public static int minIdx(double[] vs, int startIdx)
-
max
public static int max(int[] vs)
-
max
public static double max(double[] vs)
-
maxIdx
public static int maxIdx(double[] vs)
-
shuffle
public static void shuffle(double[] arr, Random rnd)Randomly permute the given double array.- Parameters:
arr- The array to shuffle.rnd- The randomness source to use.
-
shuffle
public static void shuffle(int[] arr, Random rnd)Randomly permute the given int array.- Parameters:
arr- The array to shuffle.rnd- The randomness source to use.
-
round
public static double round(double val, int decimals)Rounds the given double value to a certain number of decimal places.decimalscan be positive or negative.- Parameters:
val- The value to round.decimals- The number of decimals to round to.- Returns:
- The rounded values.
- See Also:
round(double[], int)
-
round
public static double[] round(double[] vs, int decimals)Rounds all values in the double arrayvsto a certain number of decimal places. This method does not create a copy ofvs, but modifies its contents.- Parameters:
vs- An array of doubles to round.decimals- The number of decimals to round the values.- Returns:
- the parameter
vsto allow easy chaining of method calls. - See Also:
round(double, int)
-
arrayToString
public static String arrayToString(Object arbitraryArray) throws IllegalArgumentException
Converts an array (either Object[] or of a primitive type) to a String containing it's elements in square brackets.- Parameters:
arbitraryArray- The array to convert to a String.- Returns:
- A String representation of the array
arbitraryArray. - Throws:
IllegalArgumentException- IfarbitraryArrayif not an array.
-
putMeanMaxVar
public static void putMeanMaxVar(SummaryStat vs, String prefix, Map<String,Object> res)
Convenience method to put mean, max and variance of a ValueStat object in a result map.- Parameters:
vs- the statisticprefix- name prefixres- result map where keys should be added
-
getVersion
public static String getVersion()
Utility method to get the current version (obtained from git/Maven during the build).- Returns:
- The current jasima version as a String.
-
getIdString
public static String getIdString()
Returns a descriptive String showing name, current version and project URL.
-
getJavaEnvString
public static String getJavaEnvString()
Returns a string that characterizes the current Java environment by using various system properties.- Returns:
- The execution environment.
-
getOsEnvString
public static String getOsEnvString()
Returns a string that characterizes the host operating system by using various system properties.- Returns:
- The OS environment.
-
getWorkingDirString
public static String getWorkingDirString()
- Returns:
- the current working directory.
-
-