Class FunctionUtil


  • public final class FunctionUtil
    extends Object
    A collection of static methods that may be useful for the implementation of built-in functions.

    Module: equations-api

    To use this in your app, include the following dependency in your POM:

    <dependency>
        <groupId>org.cytoscape</groupId>
        <artifactId>equations-api</artifactId>
    </dependency>

    Cytoscape Backwards Compatibility (Static Class): This class is static and therefore can't be extended by users. This means that we may add methods for minor version updates. Methods will only be removed for major version updates.
    • Method Detail

      • getArgAsDouble

        public static double getArgAsDouble​(Object arg)
                                     throws IllegalArgumentException
        Assumes that "arg" is a "String", "Boolean", "Long" or a "Double and converts it to "double".
        Parameters:
        arg - the object instance that will be converted to a double, if possible
        Returns:
        the converted argument as a "double"
        Throws:
        IllegalArgumentException - if the argument cannot be converted to a "double"
      • getArgAsString

        public static String getArgAsString​(Object arg)
                                     throws IllegalArgumentException
        Assumes that "arg" is a "String", "Boolean", "Long" or a "Double and converts it to "String".
        Parameters:
        arg - the object instance that will be converted to a String
        Returns:
        the converted argument as a "double"
        Throws:
        IllegalArgumentException - if the argument cannot be converted to a "double"
      • getArgAsLong

        public static long getArgAsLong​(Object arg)
                                 throws IllegalArgumentException
        Assumes that "arg" is a "String", "Boolean", "Long" or a "Double and converts it to "long".
        Parameters:
        arg - the object instance that will be converted to a long, if possible
        Returns:
        the converted argument as a "long"
        Throws:
        IllegalArgumentException - if the argument cannot be converted to a "long"
      • getArgAsBoolean

        public static boolean getArgAsBoolean​(Object arg)
                                       throws IllegalArgumentException
        Assumes that "arg" is a "String", "Boolean", "Long" or a "Double and converts it to "boolean".
        Parameters:
        arg - the object instance that will be converted to a boolean, if possible
        Returns:
        the converted argument as a "boolean"
        Throws:
        IllegalArgumentException - if the argument cannot be converted to a "boolean"
      • numericallySafeSum

        public static double numericallySafeSum​(double[] a)
        Carefully adds the numbers in "a" minimising loss of precision.
        Parameters:
        a - the numbers that will be summed up
        Returns:
        the sum of the elements of "a"
      • getOrdinal

        public static String getOrdinal​(int i)
        Converts an integer to an ordinal string.
        Parameters:
        i - the integer that will be converted
        Returns:
        the String representation of the ith ordinal
      • calcSampleVariance

        public static double calcSampleVariance​(double[] x)
        Calculates the sample variance of some numbers.
        Parameters:
        x - the numbers whose sample variance will be calculated
        Returns:
        the sample variance of the numbers in x[]
      • listToArray

        public static double[] listToArray​(List<Double> a)
        Converts a List to a regular double[]
        Parameters:
        a - the list that will be converted
        Returns:
        the converted array
      • isScalarArgType

        public static boolean isScalarArgType​(Class type)
        Tests whether the argument is one of the scalar types used by equations.
        Parameters:
        type - the type that will be tested
        Returns:
        true, if type is Double.class, Long.class, String.class or Boolean.class, else false
      • isTypeOfList

        public static boolean isTypeOfList​(Class listClassCandidate)
        Tests whether the argument is some kind of List.
        Parameters:
        listClassCandidate - the type that will be tested
        Returns:
        true if "listClassCandidate" is an implementer of interface List, else false
      • addScalarArgumentTypes

        public static void addScalarArgumentTypes​(List<Class> argTypes)
        Adds the scalar types that equations use internally to the argument, i.e. adds Double.class, Long.class, String.class and Boolean.class to "argTypes".
        Parameters:
        argTypes - the list that will be augmented
      • getDoubles

        public static double[] getDoubles​(Object[] args)
                                   throws FunctionError
        Attempts to convert all arguments, including Lists to a uniform array of doubles.
        Parameters:
        args - the objects that will be converted to doubles, if possible
        Returns:
        the converted array
        Throws:
        FunctionError
        IllegalArgumentException - if any scalar argument cannot be converted to a double or any list argument contains an element that cannot be converted to a number.
      • getLongs

        public static long[] getLongs​(Object[] args)
                               throws FunctionError
        Attempts to convert all arguments, including Lists to a uniform array of longs.
        Parameters:
        args - the objects that will be converted to longs, if possible
        Returns:
        the converted array
        Throws:
        FunctionError
        IllegalArgumentException - if any scalar argument cannot be converted to a long or any list argument contains an element that cannot be converted to a number.
      • getStrings

        public static String[] getStrings​(Object[] args)
        Converts all arguments, including Lists to a uniform array of strings.
        Parameters:
        args - the objects that will be converted to Strings
        Returns:
        the converted array
      • getBooleans

        public static boolean[] getBooleans​(Object[] args)
                                     throws FunctionError
        Attempts to convert all arguments, including Lists to a uniform array of booleans.
        Parameters:
        args - the objects that will be converted to booleans, if possible
        Returns:
        the converted array
        Throws:
        FunctionError
        IllegalArgumentException - if any scalar argument cannot be converted to a boolean or any list argument contains an element that cannot be converted to a number.
      • translateObjectType

        public static Object translateObjectType​(Object input)
        Tries to map "input" to one of Double, Long, Boolean or String.
        Parameters:
        input - the object that will be mapped to one of the scalar types that are internally supported by equations
        Returns:
        null if the translation failed, the input object if no translation was necessary or a new object if a successful translation was possible