Class FormulaParser
java.lang.Object
org.eclipse.nebula.widgets.nattable.formula.FormulaParser
Parser that is able to parse a formula string and calculate the result.
- Since:
- 1.4
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
protected IDataProvider
protected DecimalFormat
static final String
protected Map<String,
Class<? extends AbstractFunction>> protected Pattern
protected String
protected String
static final String
static final String
static final String
static final String
static final String
static final String
protected Pattern
static final String
-
Constructor Summary
ConstructorDescriptionFormulaParser
(IDataProvider dataProvider) Creates and initializes a newFormulaParser
. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
addDataProviderValue
(int column, int row, List<FunctionValue> values, Map<IndexCoordinate, Set<IndexCoordinate>> parsedReferences, IndexCoordinate referer) Retrieves a value from theIDataProvider
for the given coordinates and adds it to the given list ofFunctionValue
s for further processing.protected FunctionValue
combineFunctions
(List<FunctionValue> values) CombinesFunctionValue
s for processing the parsed values.protected String
convertIndexToColumnString
(int index) Converts the given column index to a character based representation for reference handling.convertToBigDecimal
(String value) Converts a given String into aBigDecimal
.protected boolean
detectCycle
(Map<IndexCoordinate, Set<IndexCoordinate>> parsedReferences) protected int[]
evaluateReference
(String reference) Evaluates a reference string to cell coordinates.protected int
getColumnIndex
(String columnLiteral) Calculate the column index out of a character based string, e.g.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.protected int
protected Object
getUnderlyingDataValue
(int column, int row) protected int
protected void
Initialize the functions that are supported by thisFormulaParser
.boolean
isFunction
(String function) Checks if a given String is a function or not.boolean
isIntegerValue
(BigDecimal value) Checks if the givenBigDecimal
is an integer or a decimal value.boolean
Checks if the given value is a number value.parseFunction
(String function) Parses the given function string to aFunctionValue
to perform calculation.protected FunctionValue
parseFunction
(String function, Map<Integer, FunctionValue> replacements, Map<IndexCoordinate, Set<IndexCoordinate>> parsedReferences, IndexCoordinate referer) Parses the given function string to aFunctionValue
to perform calculation.protected FunctionValue
parseFunction
(String function, Map<IndexCoordinate, Set<IndexCoordinate>> parsedReferences, IndexCoordinate referer) Parses the given function string to aFunctionValue
to perform calculation.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.protected List<FunctionValue>
Process multiplication and divisionFunctionValue
s first.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.protected List<FunctionValue>
processPower
(List<FunctionValue> values) Process powerFunctionValue
s first.void
registerFunction
(String functionName, Class<? extends AbstractFunction> value) Register a new function that can be evaluated.void
setDecimalFormat
(DecimalFormat format) Set theDecimalFormat
that should be used to determine the decimal separator.protected void
Update the regular expression that is used to identify a function in a function string.protected void
Updates the localized regular expression for decimal values.updateReferences
(String function, int fromColumn, int fromRow, int toColumn, int toRow) Updates the references in a function string.
-
Field Details
-
OPERATOR_REGEX
- Since:
- 2.0
- See Also:
-
DIGIT_REGEX
- Since:
- 2.0
- See Also:
-
PLACEHOLDER_REGEX
- Since:
- 2.0
- See Also:
-
OPERATOR_SPLIT_REGEX
- Since:
- 2.0
- See Also:
-
REFERENCE_REGEX
- Since:
- 2.0
- See Also:
-
REFERENCE_RANGE_REGEX
- Since:
- 2.0
- See Also:
-
COLUMN_RANGE_REGEX
- Since:
- 2.0
- See Also:
-
ROW_RANGE_REGEX
- Since:
- 2.0
- See Also:
-
RANGE_REGEX
- Since:
- 2.0
- See Also:
-
decimalFormat
-
localizedDigitRegex
-
functionRegex
-
functionPattern
-
referencePattern
-
functionMapping
-
dataProvider
-
-
Constructor Details
-
FormulaParser
Creates and initializes a newFormulaParser
.- Parameters:
dataProvider
- TheIDataProvider
that provides the data to perform calculations.
-
-
Method Details
-
registerFunction
Register a new function that can be evaluated.- Parameters:
functionName
- The name of the function that is used in a formulavalue
- The type ofAbstractFunction
that should be used when evaluation a formula that contains the given function.
-
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 thisFormulaParser
. -
updateFunctionRegex
protected void updateFunctionRegex()Update the regular expression that is used to identify a function in a function string. -
parseFunction
Parses the given function string to aFunctionValue
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 aFunctionValue
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 aFunctionValue
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 stringreplacements
- 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.
-
processPower
Process powerFunctionValue
s first.- Parameters:
values
- The list of parsedFunctionValue
s.- Returns:
- The list of
FunctionValue
s where power is already combined. - Since:
- 1.6
-
processMultiplicationAndDivision
Process multiplication and divisionFunctionValue
s first.- Parameters:
values
- The list of parsedFunctionValue
s.- Returns:
- The list of
FunctionValue
s where multiplication and division is already combined.
-
combineFunctions
CombinesFunctionValue
s for processing the parsed values.- Parameters:
values
- The list ofFunctionValue
s to combine.- Returns:
- The single
FunctionValue
as a result of the value combination.
-
evaluateReference
Evaluates a reference string to cell coordinates.- Parameters:
reference
- The reference string to evaluate.- Returns:
- The cell coordinates for the given reference string.
-
getColumnIndex
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
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 theIDataProvider
for the given coordinates and adds it to the given list ofFunctionValue
s 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 ofFunctionValue
to add the data provider value to.
-
updateLocalizedDigitRegex
protected void updateLocalizedDigitRegex()Updates the localized regular expression for decimal values. Uses the current setDecimalFormat
to determine the decimal separator.- See Also:
-
setDecimalFormat
Set theDecimalFormat
that should be used to determine the decimal separator. By default theNumberFormat.getInstance()
is set which uses the current defaultLocale
.- Parameters:
format
- TheDecimalFormat
to use for determine the decimal separator.
-
isFunction
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
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
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
Checks if the givenBigDecimal
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
Converts a given String into aBigDecimal
. 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
- 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.
-
detectCycle
-