Class FormulaParser
- java.lang.Object
-
- org.eclipse.nebula.widgets.nattable.formula.FormulaParser
-
public class FormulaParser extends Object
Parser that is able to parse a formula string and calculate the result.- Since:
- 1.4
-
-
Field Summary
Fields Modifier and Type Field Description static String
COLUMN_RANGE_REGEX
protected IDataProvider
dataProvider
protected DecimalFormat
decimalFormat
static String
DIGIT_REGEX
protected Map<String,Class<? extends AbstractFunction>>
functionMapping
protected Pattern
functionPattern
protected String
functionRegex
protected String
localizedDigitRegex
static String
OPERATOR_REGEX
static String
OPERATOR_SPLIT_REGEX
static String
PLACEHOLDER_REGEX
static String
RANGE_REGEX
static String
REFERENCE_RANGE_REGEX
static String
REFERENCE_REGEX
protected Pattern
referencePattern
static String
ROW_RANGE_REGEX
-
Constructor Summary
Constructors Constructor Description FormulaParser(IDataProvider dataProvider)
Creates and initializes a newFormulaParser
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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.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.BigDecimal
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.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.Collection<String>
getRegisteredFunctions()
protected int
getUnderlyingColumnCount()
protected Object
getUnderlyingDataValue(int column, int row)
protected int
getUnderlyingRowCount()
protected void
initFunctions()
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
isNumber(String value)
Checks if the given value is a number value.FunctionValue
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>
processMultiplicationAndDivision(List<FunctionValue> values)
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
updateFunctionRegex()
Update the regular expression that is used to identify a function in a function string.protected void
updateLocalizedDigitRegex()
Updates the localized regular expression for decimal values.String
updateReferences(String function, int fromColumn, int fromRow, int toColumn, int toRow)
Updates the references in a function string.
-
-
-
Field Detail
-
OPERATOR_REGEX
public static final String OPERATOR_REGEX
- Since:
- 2.0
- See Also:
- Constant Field Values
-
DIGIT_REGEX
public static final String DIGIT_REGEX
- Since:
- 2.0
- See Also:
- Constant Field Values
-
PLACEHOLDER_REGEX
public static final String PLACEHOLDER_REGEX
- Since:
- 2.0
- See Also:
- Constant Field Values
-
OPERATOR_SPLIT_REGEX
public static final String OPERATOR_SPLIT_REGEX
- Since:
- 2.0
- See Also:
- Constant Field Values
-
REFERENCE_REGEX
public static final String REFERENCE_REGEX
- Since:
- 2.0
- See Also:
- Constant Field Values
-
REFERENCE_RANGE_REGEX
public static final String REFERENCE_RANGE_REGEX
- Since:
- 2.0
- See Also:
- Constant Field Values
-
COLUMN_RANGE_REGEX
public static final String COLUMN_RANGE_REGEX
- Since:
- 2.0
- See Also:
- Constant Field Values
-
ROW_RANGE_REGEX
public static final String ROW_RANGE_REGEX
- Since:
- 2.0
- See Also:
- Constant Field Values
-
RANGE_REGEX
public static final String RANGE_REGEX
- Since:
- 2.0
- See Also:
- Constant Field Values
-
decimalFormat
protected DecimalFormat decimalFormat
-
localizedDigitRegex
protected String localizedDigitRegex
-
functionRegex
protected String functionRegex
-
functionPattern
protected Pattern functionPattern
-
referencePattern
protected Pattern referencePattern
-
functionMapping
protected Map<String,Class<? extends AbstractFunction>> functionMapping
-
dataProvider
protected IDataProvider dataProvider
-
-
Constructor Detail
-
FormulaParser
public FormulaParser(IDataProvider dataProvider)
Creates and initializes a newFormulaParser
.- Parameters:
dataProvider
- TheIDataProvider
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 formulavalue
- The type ofAbstractFunction
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 thisFormulaParser
.
-
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 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
protected List<FunctionValue> processPower(List<FunctionValue> values)
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
protected List<FunctionValue> processMultiplicationAndDivision(List<FunctionValue> values)
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
protected FunctionValue combineFunctions(List<FunctionValue> values)
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
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 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(DecimalFormat)
-
setDecimalFormat
public void setDecimalFormat(DecimalFormat format)
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
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 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
public BigDecimal convertToBigDecimal(String value)
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
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.
-
detectCycle
protected boolean detectCycle(Map<IndexCoordinate,Set<IndexCoordinate>> parsedReferences)
-
-