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
FieldsModifier and TypeFieldDescriptionstatic final Stringprotected IDataProviderprotected DecimalFormatstatic final Stringprotected Map<String,Class<? extends AbstractFunction>> protected Patternprotected Stringprotected Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringprotected Patternstatic final String -
Constructor Summary
ConstructorsConstructorDescriptionFormulaParser(IDataProvider dataProvider) Creates and initializes a newFormulaParser. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddDataProviderValue(int column, int row, List<FunctionValue> values, Map<IndexCoordinate, Set<IndexCoordinate>> parsedReferences, IndexCoordinate referer) Retrieves a value from theIDataProviderfor the given coordinates and adds it to the given list ofFunctionValues for further processing.protected FunctionValuecombineFunctions(List<FunctionValue> values) CombinesFunctionValues for processing the parsed values.protected StringconvertIndexToColumnString(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 booleandetectCycle(Map<IndexCoordinate, Set<IndexCoordinate>> parsedReferences) protected int[]evaluateReference(String reference) Evaluates a reference string to cell coordinates.protected intgetColumnIndex(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 intprotected ObjectgetUnderlyingDataValue(int column, int row) protected intprotected voidInitialize the functions that are supported by thisFormulaParser.booleanisFunction(String function) Checks if a given String is a function or not.booleanisIntegerValue(BigDecimal value) Checks if the givenBigDecimalis an integer or a decimal value.booleanChecks if the given value is a number value.parseFunction(String function) Parses the given function string to aFunctionValueto perform calculation.protected FunctionValueparseFunction(String function, Map<Integer, FunctionValue> replacements, Map<IndexCoordinate, Set<IndexCoordinate>> parsedReferences, IndexCoordinate referer) Parses the given function string to aFunctionValueto perform calculation.protected FunctionValueparseFunction(String function, Map<IndexCoordinate, Set<IndexCoordinate>> parsedReferences, IndexCoordinate referer) Parses the given function string to aFunctionValueto perform calculation.protected StringprocessFunctions(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 divisionFunctionValues first.protected StringprocessParenthesis(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 powerFunctionValues first.voidregisterFunction(String functionName, Class<? extends AbstractFunction> value) Register a new function that can be evaluated.voidsetDecimalFormat(DecimalFormat format) Set theDecimalFormatthat should be used to determine the decimal separator.protected voidUpdate the regular expression that is used to identify a function in a function string.protected voidUpdates 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- TheIDataProviderthat 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 ofAbstractFunctionthat 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 aFunctionValueto perform calculation.- Parameters:
function- The function string to parse.- Returns:
- The
FunctionValuethat 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 aFunctionValueto 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
FunctionValuethat 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 aFunctionValueto 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
FunctionValuethat 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 powerFunctionValues first.- Parameters:
values- The list of parsedFunctionValues.- Returns:
- The list of
FunctionValues where power is already combined. - Since:
- 1.6
-
processMultiplicationAndDivision
Process multiplication and divisionFunctionValues first.- Parameters:
values- The list of parsedFunctionValues.- Returns:
- The list of
FunctionValues where multiplication and division is already combined.
-
combineFunctions
CombinesFunctionValues for processing the parsed values.- Parameters:
values- The list ofFunctionValues to combine.- Returns:
- The single
FunctionValueas 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 theIDataProviderfor the given coordinates and adds it to the given list ofFunctionValues 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 ofFunctionValueto add the data provider value to.
-
updateLocalizedDigitRegex
protected void updateLocalizedDigitRegex()Updates the localized regular expression for decimal values. Uses the current setDecimalFormatto determine the decimal separator.- See Also:
-
setDecimalFormat
Set theDecimalFormatthat should be used to determine the decimal separator. By default theNumberFormat.getInstance()is set which uses the current defaultLocale.- Parameters:
format- TheDecimalFormatto 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:
trueif the given String represents a function,falseif 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:
trueif the given value is an integral or decimal value,falseif not
-
isIntegerValue
Checks if the givenBigDecimalis an integer or a decimal value.- Parameters:
value- The value to check.- Returns:
trueif the given value is an integer,falseif 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
BigDecimalfor 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
-