Class ConditionParser
java.lang.Object
org.apache.maven.internal.impl.model.profile.ConditionParser
The
ConditionParser
class is responsible for parsing and evaluating expressions.
It supports tokenizing the input expression and resolving custom functions passed in a map.
This class implements a recursive descent parser to handle various operations including
arithmetic, logical, and comparison operations, as well as function calls.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
A functional interface that represents an expression function to be applied to a list of arguments. -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionConditionParser
(Map<String, ConditionParser.ExpressionFunction> functions, Function<String, String> propertyResolver) Constructs a newConditionParser
with the given function mappings. -
Method Summary
Modifier and TypeMethodDescriptionprivate static Object
Adds two objects, handling string concatenation and numeric addition.private static Object
Compares two objects based on the given operator.private static Object
Divides the left operand by the right operand.private static Object
Multiplies two numeric operands.private Object
Negates a numeric value.Parses the given expression and returns the result of the evaluation.private Object
Parses addition and subtraction operations.Parses a list of arguments for a function call.private Object
Parses comparison operations.private Object
Parses the next expression from the list of tokens.private Object
Parses a function call.private Object
Parses logical AND operations.private Object
Parses logical OR operations.private Object
Parses multiplication and division operations.private Object
Parses an expression within parentheses.private Object
Parses individual terms (numbers, strings, booleans, parentheses, functions).private Object
Parses unary operations (negation).private Object
Parses a token that could be either a variable or an unknown function.private static Object
Subtracts the right operand from the left operand.static Boolean
Converts an object to a boolean value.static double
Converts an object to a double value.static int
Converts an object to an integer value.Tokenizes the input expression into a list of string tokens for further parsing.static String
Converts an object to a string representation.
-
Field Details
-
functions
-
propertyResolver
-
tokens
-
current
private int current
-
-
Constructor Details
-
ConditionParser
public ConditionParser(Map<String, ConditionParser.ExpressionFunction> functions, Function<String, String> propertyResolver) Constructs a newConditionParser
with the given function mappings.- Parameters:
functions
- a map of function names to their correspondingExpressionFunction
implementationspropertyResolver
- the property resolver
-
-
Method Details
-
parse
Parses the given expression and returns the result of the evaluation.- Parameters:
expression
- the expression to be parsed- Returns:
- the result of parsing and evaluating the expression
-
tokenize
Tokenizes the input expression into a list of string tokens for further parsing. This method handles quoted strings, property aliases, and various operators.- Parameters:
expression
- the expression to tokenize- Returns:
- a list of tokens
-
parseExpression
Parses the next expression from the list of tokens.- Returns:
- the parsed expression as an object
- Throws:
RuntimeException
- if there are unexpected tokens after the end of the expression
-
parseLogicalOr
Parses logical OR operations.- Returns:
- the result of parsing logical OR operations
-
parseLogicalAnd
Parses logical AND operations.- Returns:
- the result of parsing logical AND operations
-
parseComparison
Parses comparison operations.- Returns:
- the result of parsing comparison operations
-
parseAddSubtract
Parses addition and subtraction operations.- Returns:
- the result of parsing addition and subtraction operations
-
parseMultiplyDivide
Parses multiplication and division operations.- Returns:
- the result of parsing multiplication and division operations
-
parseUnary
Parses unary operations (negation).- Returns:
- the result of parsing unary operations
-
parseTerm
Parses individual terms (numbers, strings, booleans, parentheses, functions).- Returns:
- the parsed term
- Throws:
RuntimeException
- if the expression ends unexpectedly or contains unknown tokens
-
parseVariableOrUnknownFunction
Parses a token that could be either a variable or an unknown function.- Returns:
- the result of parsing a variable or unknown function
- Throws:
RuntimeException
- if an unknown function is encountered
-
parseArgumentList
Parses a list of arguments for a function call.- Returns:
- a list of parsed arguments
- Throws:
RuntimeException
- if there's a mismatch in parentheses
-
parseFunction
Parses a function call.- Returns:
- the result of the function call
-
parseParentheses
Parses an expression within parentheses.- Returns:
- the result of parsing the expression within parentheses
- Throws:
RuntimeException
- if there's a mismatch in parentheses
-
add
Adds two objects, handling string concatenation and numeric addition.- Parameters:
left
- the left operandright
- the right operand- Returns:
- the result of the addition
- Throws:
RuntimeException
- if the operands cannot be added
-
negate
Negates a numeric value.- Parameters:
value
- the value to negate- Returns:
- the negated value
- Throws:
RuntimeException
- if the value cannot be negated
-
subtract
Subtracts the right operand from the left operand.- Parameters:
left
- the left operandright
- the right operand- Returns:
- the result of the subtraction
- Throws:
RuntimeException
- if the operands cannot be subtracted
-
multiply
Multiplies two numeric operands.- Parameters:
left
- the left operandright
- the right operand- Returns:
- the result of the multiplication
- Throws:
RuntimeException
- if the operands cannot be multiplied
-
divide
Divides the left operand by the right operand.- Parameters:
left
- the left operand (dividend)right
- the right operand (divisor)- Returns:
- the result of the division
- Throws:
RuntimeException
- if the operands cannot be dividedArithmeticException
- if attempting to divide by zero
-
compare
Compares two objects based on the given operator. Supports comparison of numbers and strings, and equality checks for null values.- Parameters:
left
- the left operandoperator
- the comparison operator (">", "invalid input: '<'", ">=", "invalid input: '<'=", "==", or "!=")right
- the right operand- Returns:
- the result of the comparison (a boolean value)
- Throws:
IllegalStateException
- if an unknown operator is providedRuntimeException
- if the operands cannot be compared
-
toString
Converts an object to a string representation. If the object is aDouble
, it formats it without any decimal places. Otherwise, it uses theString.valueOf
method.- Parameters:
value
- the object to convert to a string- Returns:
- the string representation of the object
-
toBoolean
Converts an object to a boolean value. If the object is: - aBoolean
, returns its value directly. - aString
, returnstrue
if the string is non-blank. - aNumber
, returnstrue
if its integer value is not zero. For other object types, returnstrue
if the object is non-null.- Parameters:
value
- the object to convert to a boolean- Returns:
- the boolean representation of the object
-
toDouble
Converts an object to a double value. If the object is: - aNumber
, returns its double value. - aString
, tries to parse it as a double. - aBoolean
, returns1.0
fortrue
,0.0
forfalse
. If the object cannot be converted, aRuntimeException
is thrown.- Parameters:
value
- the object to convert to a double- Returns:
- the double representation of the object
- Throws:
RuntimeException
- if the object cannot be converted to a double
-
toInt
Converts an object to an integer value. If the object is: - aNumber
, returns its integer value. - aString
, tries to parse it as an integer, or as a double then converted to an integer. - aBoolean
, returns1
fortrue
,0
forfalse
. If the object cannot be converted, aRuntimeException
is thrown.- Parameters:
value
- the object to convert to an integer- Returns:
- the integer representation of the object
- Throws:
RuntimeException
- if the object cannot be converted to an integer
-