Class FormulaParser


  • public class FormulaParser
    extends Object
    Parser that is able to parse a formula string and calculate the result.
    Since:
    1.4
    • Constructor Detail

      • FormulaParser

        public FormulaParser​(IDataProvider dataProvider)
        Creates and initializes a new FormulaParser.
        Parameters:
        dataProvider - The IDataProvider that provides the data to perform calculations.
    • Method Detail

      • registerFunction

        public void registerFunction​(String functionName,
                                     Class<? extends AbstractFunction> value)
        Register a new function that can be evaluated.
        Parameters:
        functionName - The name of the function that is used in a formula
        value - The type of AbstractFunction that should be used when evaluation a formula that contains the given function.
      • getRegisteredFunctions

        public Collection<String> getRegisteredFunctions()
        Returns:
        The names of the registered functions that can be evaluated by this FormulaParser.
      • initFunctions

        protected void initFunctions()
        Initialize the functions that are supported by this FormulaParser .
      • updateFunctionRegex

        protected void updateFunctionRegex()
        Update the regular expression that is used to identify a function in a function string.
      • parseFunction

        public FunctionValue parseFunction​(String function)
        Parses the given function string to a FunctionValue to perform calculation.
        Parameters:
        function - The function string to parse.
        Returns:
        The FunctionValue that represents the calculation result of the parsed function string.
      • parseFunction

        protected FunctionValue parseFunction​(String function,
                                              Map<IndexCoordinate,​Set<IndexCoordinate>> parsedReferences,
                                              IndexCoordinate referer)
        Parses the given function string to a FunctionValue to perform calculation. Creates a new replacement map but keeps the parsed references for cycle detection.
        Parameters:
        function - The function string to parse.
        parsedReferences - The references that where parsed already together with their references if any. Needed for cycle detection.
        referer - The coordinate of the cell that refers to the value to add. Needed for cycle detection.
        Returns:
        The FunctionValue that represents the calculation result of the parsed function string.
      • parseFunction

        protected FunctionValue parseFunction​(String function,
                                              Map<Integer,​FunctionValue> replacements,
                                              Map<IndexCoordinate,​Set<IndexCoordinate>> parsedReferences,
                                              IndexCoordinate referer)
        Parses the given function string to a FunctionValue to perform calculation.
        Parameters:
        function - The function string to parse.
        replacements - The map of replacements to support iterative parsing of sub-functions.
        parsedReferences - The references that where parsed already together with their references if any. Needed for cycle detection.
        referer - The coordinate of the cell that refers to the value to add. Needed for cycle detection.
        Returns:
        The FunctionValue that represents the calculation result of the parsed function string.
      • processFunctions

        protected String processFunctions​(String function,
                                          Map<Integer,​FunctionValue> replacements,
                                          Map<IndexCoordinate,​Set<IndexCoordinate>> parsedReferences,
                                          IndexCoordinate referer)
        Process parts of a function that represent a function by name. Replaces the function result with placeholders whose representations are put in the given replacements map.
        Parameters:
        function - The function string
        replacements - The map of replacements to store the result of the function parsing.
        parsedReferences - The references that where parsed already together with their references if any. Needed for cycle detection.
        referer - The coordinate of the cell that refers to the value to add. Needed for cycle detection.
        Returns:
        The modified string that contains placeholders for functions.
      • processParenthesis

        protected String processParenthesis​(String function,
                                            Map<Integer,​FunctionValue> replacements,
                                            Map<IndexCoordinate,​Set<IndexCoordinate>> parsedReferences,
                                            IndexCoordinate referer)
        Process parts of a function that are combined in parenthesis. Replaces the parenthesis with placeholders whose representations are put in the given replacements map.
        Parameters:
        function - The function string.
        replacements - The map of replacements to store the result of the parenthesis parsing.
        parsedReferences - The references that where parsed already together with their references if any. Needed for cycle detection.
        referer - The coordinate of the cell that refers to the value to add. Needed for cycle detection.
        Returns:
        The modified string that contains placeholders for parenthesis.
      • evaluateReference

        protected int[] evaluateReference​(String reference)
        Evaluates a reference string to cell coordinates.
        Parameters:
        reference - The reference string to evaluate.
        Returns:
        The cell coordinates for the given reference string.
      • getColumnIndex

        protected int getColumnIndex​(String columnLiteral)
        Calculate the column index out of a character based string, e.g. A = 0, AA = 26
        Parameters:
        columnLiteral - The string to calculate the column index from.
        Returns:
        The column index for the given string.
      • convertIndexToColumnString

        protected String convertIndexToColumnString​(int index)
        Converts the given column index to a character based representation for reference handling.
        Parameters:
        index - The column index to convert.
        Returns:
        The parsed character representation for a column index.
      • addDataProviderValue

        protected void addDataProviderValue​(int column,
                                            int row,
                                            List<FunctionValue> values,
                                            Map<IndexCoordinate,​Set<IndexCoordinate>> parsedReferences,
                                            IndexCoordinate referer)
        Retrieves a value from the IDataProvider for the given coordinates and adds it to the given list of FunctionValues for further processing.
        Parameters:
        column - The column index of the value.
        row - The row index of the value.
        parsedReferences - The references that where parsed already together with their references if any. Needed for cycle detection.
        referer - The coordinate of the cell that refers to the value to add. Needed for cycle detection.
        values - The list of FunctionValue to add the data provider value to.
      • updateLocalizedDigitRegex

        protected void updateLocalizedDigitRegex()
        Updates the localized regular expression for decimal values. Uses the current set DecimalFormat to determine the decimal separator.
        See Also:
        setDecimalFormat(DecimalFormat)
      • isFunction

        public boolean isFunction​(String function)
        Checks if a given String is a function or not.
        Parameters:
        function - The function String to check
        Returns:
        true if the given String represents a function, false if not
      • getFunctionOnly

        public String getFunctionOnly​(String function)
        Checks if the given string contains a function marker (default: leading'=') and returns a string without that marker to have the function only.
        Parameters:
        function - The function string to modify.
        Returns:
        The function only string
      • isNumber

        public boolean isNumber​(String value)
        Checks if the given value is a number value.
        Parameters:
        value - The value to check.
        Returns:
        true if the given value is an integral or decimal value, false if not
      • isIntegerValue

        public boolean isIntegerValue​(BigDecimal value)
        Checks if the given BigDecimal is an integer or a decimal value.
        Parameters:
        value - The value to check.
        Returns:
        true if the given value is an integer, false if it is a decimal.
      • convertToBigDecimal

        public BigDecimal convertToBigDecimal​(String value)
        Converts a given String into a BigDecimal. Is able to convert decimal values with localized decimal separators.
        Parameters:
        value - The value to convert.
        Returns:
        The BigDecimal for the given value.
      • getUnderlyingColumnCount

        protected int getUnderlyingColumnCount()
        Returns:
        The column count of the underlying data model. The base implementation uses the underlying IDataProvider.
      • getUnderlyingRowCount

        protected int getUnderlyingRowCount()
        Returns:
        The row count of the underlying data model. The base implementation uses the underlying IDataProvider.
      • getUnderlyingDataValue

        protected Object getUnderlyingDataValue​(int column,
                                                int row)
        Parameters:
        column - The column index of the cell whose value is requested.
        row - The row index of the cell whose value is requested.
        Returns:
        The data value for the given column and row index out of the underlying data model. The base implementation uses the underlying IDataProvider.
      • updateReferences

        public String updateReferences​(String function,
                                       int fromColumn,
                                       int fromRow,
                                       int toColumn,
                                       int toRow)
        Updates the references in a function string. Needed for copy operations.
        Parameters:
        function - The function string to update the references.
        fromColumn - The column index from where a formula is transfered.
        fromRow - The row index from where a formula is transfered.
        toColumn - The column index to where a formula is transfered.
        toRow - The row index to where a formula is transfered.
        Returns:
        The function string with updated references.