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_PATH
Class search path containing all packaged in jasima-main.static ExecutorService
DEF_POOL
A 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 String
arrayToString(Object arbitraryArray)
Converts an array (either Object[] or of a primitive type) to a String containing it's elements in square brackets.static double
deleteArrayElement(double[] prios, int elemIdx, double fillWith)
static <T> T
deleteArrayElement(T[] prios, int elemIdx, T fillWith)
static String
exceptionToString(Throwable t)
Converts an exception's stack trace to a single line string.static String[]
filter(String[] ss, String prefix)
static String
getIdString()
Returns a descriptive String showing name, current version and project URL.static String
getJavaEnvString()
Returns a string that characterizes the current Java environment by using various system properties.static String
getOsEnvString()
Returns a string that characterizes the host operating system by using various system properties.static String
getVersion()
Utility method to get the current version (obtained from git/Maven during the build).static String
getWorkingDirString()
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 double
max(double[] vs)
static int
max(int[] vs)
static int
maxIdx(double[] vs)
static double
mean(Collection<? extends Number> coll)
static double
min(double[] vs)
static int
min(int[] vs)
static int
minIdx(double[] vs)
static int
minIdx(double[] vs, int startIdx)
static int
minIdx(int[] vs)
static int
minIdx(int[] vs, int startIdx)
static <T> T
moveArrayElementToBack(T[] prios, int elemIdx)
static String
nextNonEmptyLine(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 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.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 arrayvs
to a certain number of decimal places.static double
round(double val, int decimals)
Rounds the given double value to a certain number of decimal places.static void
shuffle(double[] arr, Random rnd)
Randomly permute the given double array.static void
shuffle(int[] arr, Random rnd)
Randomly permute the given int array.static double
stdDev(Collection<? extends Number> coll)
static double
sum(double[] productMix)
static int
sum(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
- TheThrowable
to 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
a
with the first occurrence ofelementToRemove
removed. If no such element cound be found,a
is 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.decimals
can 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 arrayvs
to 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
vs
to 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
- IfarbitraryArray
if 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.
-
-