Class OperatorEvaluator

java.lang.Object
cloud.opencode.base.expression.eval.OperatorEvaluator

public final class OperatorEvaluator extends Object
Operator Evaluator 运算符求值器

Evaluates binary and unary operators on operand values.

对操作数值求值二元和一元运算符。

Features | 主要功能:

  • Binary operators: +, -, *, /, %, **, ==, !=, <, <=, >, >=, &&, ||, matches - 二元运算符
  • Unary operators: -, !, + - 一元运算符
  • Overflow-safe integer arithmetic with Math.*Exact - 溢出安全的整数运算
  • Automatic type widening (int to long to double) - 自动类型拓宽
  • String-to-number coercion - 字符串到数字的强制转换

Usage Examples | 使用示例:

Object sum = OperatorEvaluator.evaluateBinary("+", 10, 20);  // 30
Object neg = OperatorEvaluator.evaluateUnary("-", 5);  // -5
boolean eq = OperatorEvaluator.equals(1, 1.0);  // true

Security | 安全性:

  • Thread-safe: Yes, stateless utility class - 线程安全: 是,无状态工具类
  • Null-safe: Partially, null treated as 0/false/empty string - 空值安全: 部分,null视为0/false/空字符串
  • ReDoS protection: regex matching has timeout and length limits - ReDoS防护: 正则匹配有超时和长度限制

Performance | 性能特性:

  • Time complexity: O(1) per arithmetic, comparison, and logical operation; O(m*n) for regex matches where m is pattern complexity and n is input length - 时间复杂度: 算术、比较、逻辑操作均为 O(1);正则匹配为 O(m*n),m为模式复杂度,n为输入长度
  • Space complexity: O(1) per operation - 空间复杂度: O(1)
Since:
JDK 25, opencode-base-expression V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • evaluateBinary

      public static Object evaluateBinary(String operator, Object left, Object right)
      Evaluate binary operator 求值二元运算符
      Parameters:
      operator - the operator | 运算符
      left - the left operand | 左操作数
      right - the right operand | 右操作数
      Returns:
      the result | 结果
    • evaluateUnary

      public static Object evaluateUnary(String operator, Object operand)
      Evaluate unary operator 求值一元运算符
      Parameters:
      operator - the operator | 运算符
      operand - the operand | 操作数
      Returns:
      the result | 结果
    • add

      public static Object add(Object left, Object right)
      Add two values 加法
      Parameters:
      left - left operand | 左操作数
      right - right operand | 右操作数
      Returns:
      result | 结果
    • subtract

      public static Object subtract(Object left, Object right)
      Subtract two values 减法
    • multiply

      public static Object multiply(Object left, Object right)
      Multiply two values 乘法
    • divide

      public static Object divide(Object left, Object right)
      Divide two values 除法
    • modulo

      public static Object modulo(Object left, Object right)
      Modulo two values 取模
    • power

      public static Object power(Object left, Object right)
      Power operation 幂运算
    • equals

      public static boolean equals(Object left, Object right)
      Equals comparison 相等比较
    • notEquals

      public static boolean notEquals(Object left, Object right)
      Not equals comparison 不等比较
    • lessThan

      public static boolean lessThan(Object left, Object right)
      Less than comparison 小于比较
    • lessThanOrEqual

      public static boolean lessThanOrEqual(Object left, Object right)
      Less than or equal comparison 小于等于比较
    • greaterThan

      public static boolean greaterThan(Object left, Object right)
      Greater than comparison 大于比较
    • greaterThanOrEqual

      public static boolean greaterThanOrEqual(Object left, Object right)
      Greater than or equal comparison 大于等于比较
    • and

      public static boolean and(Object left, Object right)
      Logical AND 逻辑与
    • or

      public static boolean or(Object left, Object right)
      Logical OR 逻辑或
    • not

      public static boolean not(Object operand)
      Logical NOT 逻辑非
    • negate

      public static Object negate(Object operand)
      Negate number 取负
    • matches

      public static boolean matches(Object left, Object right)
      Regex matches 正则匹配