Class ExpressionStrategy
java.lang.Object
cloud.opencode.base.feature.strategy.ExpressionStrategy
- All Implemented Interfaces:
EnableStrategy
Expression-based Enable Strategy with Optional Expression Module Delegation
支持可选表达式模块委托的基于表达式的启用策略
Evaluates feature enablement using dynamic expressions. If the Expression module (opencode-base-expression) is available, it delegates to OpenExpression for powerful expression evaluation. Otherwise, falls back to simple constant evaluation.
使用动态表达式评估功能启用状态。 如果表达式模块可用,则委托给 OpenExpression 进行强大的表达式求值。 否则降级到简单的常量评估。
Usage Examples | 使用示例:
// Simple comparison
EnableStrategy strategy = ExpressionStrategy.of("age >= 18");
// Complex boolean expression
EnableStrategy strategy = ExpressionStrategy.of(
"userType == 'premium' || (userType == 'standard' && age >= 21)"
);
// With context attributes
FeatureContext ctx = FeatureContext.builder()
.attribute("userType", "premium")
.attribute("age", 25)
.build();
boolean enabled = strategy.isEnabled(feature, ctx);
Available Variables | 可用变量:
userId- User ID from context | 来自上下文的用户IDtenantId- Tenant ID from context | 来自上下文的租户IDfeatureKey- Feature key | 功能键defaultEnabled- Default enabled state | 默认启用状态- All context attributes | 所有上下文属性
Features | 主要功能:
- Expression-based feature evaluation with configurable operators - 基于表达式的功能评估和可配置运算符
- Context attribute matching for dynamic feature decisions - 上下文属性匹配实现动态功能决策
Security | 安全性:
- Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
- Null-safe: Partial (validates inputs) - 空值安全: 部分(验证输入)
- Since:
- JDK 25, opencode-base-feature V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionGets the expression string.booleanGets the fallback value.booleanisEnabled(Feature feature, FeatureContext context) Determine if the feature is enabled for the given context 确定功能对于给定上下文是否启用static booleanChecks if the Expression module is available.static booleanisValidExpression(String expression) Checks if an expression is valid syntax.static ExpressionStrategyCreates an expression strategy.static ExpressionStrategyCreates an expression strategy with a fallback value.toString()
-
Method Details
-
isExpressionModuleAvailable
public static boolean isExpressionModuleAvailable()Checks if the Expression module is available. 检查表达式模块是否可用- Returns:
- true if Expression module is available | 如果表达式模块可用返回 true
-
of
Creates an expression strategy. 创建表达式策略- Parameters:
expression- the expression to evaluate | 要求值的表达式- Returns:
- the strategy | 策略
-
of
Creates an expression strategy with a fallback value. 创建带有回退值的表达式策略- Parameters:
expression- the expression to evaluate | 要求值的表达式fallbackValue- the value to return if expression evaluation fails | 如果表达式求值失败时返回的值- Returns:
- the strategy | 策略
-
isValidExpression
Checks if an expression is valid syntax. 检查表达式语法是否有效- Parameters:
expression- the expression to check | 要检查的表达式- Returns:
- true if expression is valid syntax | 如果表达式语法有效返回 true
-
isEnabled
Description copied from interface:EnableStrategyDetermine if the feature is enabled for the given context 确定功能对于给定上下文是否启用- Specified by:
isEnabledin interfaceEnableStrategy- Parameters:
feature- the feature being evaluated | 正在评估的功能context- the evaluation context | 评估上下文- Returns:
- true if enabled | 如果启用返回true
-
getExpression
-
getFallbackValue
public boolean getFallbackValue()Gets the fallback value. 获取回退值- Returns:
- the fallback value | 回退值
-
toString
-