Class OperatorEvaluator
java.lang.Object
cloud.opencode.base.expression.eval.OperatorEvaluator
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 Summary
Modifier and TypeMethodDescriptionstatic ObjectAdd two values 加法static booleanLogical AND 逻辑与static ObjectDivide two values 除法static booleanEquals comparison 相等比较static ObjectevaluateBinary(String operator, Object left, Object right) Evaluate binary operator 求值二元运算符static ObjectevaluateUnary(String operator, Object operand) Evaluate unary operator 求值一元运算符static booleangreaterThan(Object left, Object right) Greater than comparison 大于比较static booleangreaterThanOrEqual(Object left, Object right) Greater than or equal comparison 大于等于比较static booleanLess than comparison 小于比较static booleanlessThanOrEqual(Object left, Object right) Less than or equal comparison 小于等于比较static booleanRegex matches 正则匹配static ObjectModulo two values 取模static ObjectMultiply two values 乘法static ObjectNegate number 取负static booleanLogical NOT 逻辑非static booleanNot equals comparison 不等比较static booleanLogical OR 逻辑或static ObjectPower operation 幂运算static ObjectSubtract two values 减法
-
Method Details
-
evaluateBinary
-
evaluateUnary
-
add
-
subtract
-
multiply
-
divide
-
modulo
-
power
-
equals
-
notEquals
-
lessThan
-
lessThanOrEqual
-
greaterThan
-
greaterThanOrEqual
-
and
-
or
-
not
Logical NOT 逻辑非 -
negate
-
matches
-