Class ExpressionStrategy

java.lang.Object
cloud.opencode.base.feature.strategy.ExpressionStrategy
All Implemented Interfaces:
EnableStrategy

public final class ExpressionStrategy extends Object implements 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 | 来自上下文的用户ID
  • tenantId - Tenant ID from context | 来自上下文的租户ID
  • featureKey - 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 Details

    • isExpressionModuleAvailable

      public static boolean isExpressionModuleAvailable()
      Checks if the Expression module is available. 检查表达式模块是否可用
      Returns:
      true if Expression module is available | 如果表达式模块可用返回 true
    • of

      public static ExpressionStrategy of(String expression)
      Creates an expression strategy. 创建表达式策略
      Parameters:
      expression - the expression to evaluate | 要求值的表达式
      Returns:
      the strategy | 策略
    • of

      public static ExpressionStrategy of(String expression, boolean fallbackValue)
      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

      public static boolean isValidExpression(String expression)
      Checks if an expression is valid syntax. 检查表达式语法是否有效
      Parameters:
      expression - the expression to check | 要检查的表达式
      Returns:
      true if expression is valid syntax | 如果表达式语法有效返回 true
    • isEnabled

      public boolean isEnabled(Feature feature, FeatureContext context)
      Description copied from interface: EnableStrategy
      Determine if the feature is enabled for the given context 确定功能对于给定上下文是否启用
      Specified by:
      isEnabled in interface EnableStrategy
      Parameters:
      feature - the feature being evaluated | 正在评估的功能
      context - the evaluation context | 评估上下文
      Returns:
      true if enabled | 如果启用返回true
    • getExpression

      public String getExpression()
      Gets the expression string. 获取表达式字符串
      Returns:
      the expression | 表达式
    • getFallbackValue

      public boolean getFallbackValue()
      Gets the fallback value. 获取回退值
      Returns:
      the fallback value | 回退值
    • toString

      public String toString()
      Overrides:
      toString in class Object