Class ConditionParser

java.lang.Object
org.apache.maven.internal.impl.model.profile.ConditionParser

public class ConditionParser extends Object
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.
  • Field Details

  • Constructor Details

    • ConditionParser

      public ConditionParser(Map<String,ConditionParser.ExpressionFunction> functions, Function<String,String> propertyResolver)
      Constructs a new ConditionParser with the given function mappings.
      Parameters:
      functions - a map of function names to their corresponding ExpressionFunction implementations
      propertyResolver - the property resolver
  • Method Details

    • parse

      public Object parse(String expression)
      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

      private List<String> tokenize(String expression)
      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

      private Object 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

      private Object parseLogicalOr()
      Parses logical OR operations.
      Returns:
      the result of parsing logical OR operations
    • parseLogicalAnd

      private Object parseLogicalAnd()
      Parses logical AND operations.
      Returns:
      the result of parsing logical AND operations
    • parseComparison

      private Object parseComparison()
      Parses comparison operations.
      Returns:
      the result of parsing comparison operations
    • parseAddSubtract

      private Object parseAddSubtract()
      Parses addition and subtraction operations.
      Returns:
      the result of parsing addition and subtraction operations
    • parseMultiplyDivide

      private Object parseMultiplyDivide()
      Parses multiplication and division operations.
      Returns:
      the result of parsing multiplication and division operations
    • parseUnary

      private Object parseUnary()
      Parses unary operations (negation).
      Returns:
      the result of parsing unary operations
    • parseTerm

      private Object 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

      private Object 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

      private List<Object> 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

      private Object parseFunction()
      Parses a function call.
      Returns:
      the result of the function call
    • parseParentheses

      private Object 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

      private static Object add(Object left, Object right)
      Adds two objects, handling string concatenation and numeric addition.
      Parameters:
      left - the left operand
      right - the right operand
      Returns:
      the result of the addition
      Throws:
      RuntimeException - if the operands cannot be added
    • negate

      private Object negate(Object value)
      Negates a numeric value.
      Parameters:
      value - the value to negate
      Returns:
      the negated value
      Throws:
      RuntimeException - if the value cannot be negated
    • subtract

      private static Object subtract(Object left, Object right)
      Subtracts the right operand from the left operand.
      Parameters:
      left - the left operand
      right - the right operand
      Returns:
      the result of the subtraction
      Throws:
      RuntimeException - if the operands cannot be subtracted
    • multiply

      private static Object multiply(Object left, Object right)
      Multiplies two numeric operands.
      Parameters:
      left - the left operand
      right - the right operand
      Returns:
      the result of the multiplication
      Throws:
      RuntimeException - if the operands cannot be multiplied
    • divide

      private static Object divide(Object left, Object right)
      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 divided
      ArithmeticException - if attempting to divide by zero
    • compare

      private static Object compare(Object left, String operator, Object right)
      Compares two objects based on the given operator. Supports comparison of numbers and strings, and equality checks for null values.
      Parameters:
      left - the left operand
      operator - 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 provided
      RuntimeException - if the operands cannot be compared
    • toString

      public static String toString(Object value)
      Converts an object to a string representation. If the object is a Double, it formats it without any decimal places. Otherwise, it uses the String.valueOf method.
      Parameters:
      value - the object to convert to a string
      Returns:
      the string representation of the object
    • toBoolean

      public static Boolean toBoolean(Object value)
      Converts an object to a boolean value. If the object is: - a Boolean, returns its value directly. - a String, returns true if the string is non-blank. - a Number, returns true if its integer value is not zero. For other object types, returns true if the object is non-null.
      Parameters:
      value - the object to convert to a boolean
      Returns:
      the boolean representation of the object
    • toDouble

      public static double toDouble(Object value)
      Converts an object to a double value. If the object is: - a Number, returns its double value. - a String, tries to parse it as a double. - a Boolean, returns 1.0 for true, 0.0 for false. If the object cannot be converted, a RuntimeException 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

      public static int toInt(Object value)
      Converts an object to an integer value. If the object is: - a Number, returns its integer value. - a String, tries to parse it as an integer, or as a double then converted to an integer. - a Boolean, returns 1 for true, 0 for false. If the object cannot be converted, a RuntimeException 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