Class Util


  • public class Util
    extends Object
    Some static utility methods that don't really fit anywhere else.
    Author:
    Torsten Hildebrandt
    • Field Detail

      • DEF_POOL

        public static final ExecutorService DEF_POOL
        A default thread pool without an upper limit. Used instead of ForkJoinPool.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 - The Throwable 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 from a.
        Returns:
        The array a with the first occurrence of elementToRemove 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}
      • 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)
      • 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 array vs to a certain number of decimal places. This method does not create a copy of vs, 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 - If arbitraryArray 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 statistic
        prefix - name prefix
        res - 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.