Class VariantRouter<T>

java.lang.Object
cloud.opencode.base.feature.proxy.VariantRouter<T>
Type Parameters:
T - the variant type | 变体类型

Security | 安全性:

  • Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
  • Null-safe: Partial (validates inputs) - 空值安全: 部分(验证输入)

public final class VariantRouter<T> extends Object
Variant Router 变体路由器

Routes method calls to appropriate variants for A/B testing.

将方法调用路由到适当的A/B测试变体。

Features | 主要功能:

  • Multiple variant registration - 多变体注册
  • Consistent user routing - 一致性用户路由
  • Percentage-based traffic split - 基于百分比的流量分配
  • Context-aware selection - 上下文感知选择

Usage Examples | 使用示例:

// Define variant implementations
CheckoutService variantA = new CheckoutServiceV1();
CheckoutService variantB = new CheckoutServiceV2();

// Create router with percentage-based traffic split
VariantRouter<CheckoutService> router = VariantRouter.<CheckoutService>builder("checkout-flow")
    .variant("A", variantA, 50)  // 50% traffic
    .variant("B", variantB, 50)  // 50% traffic
    .build();

// Route based on user context
FeatureContext ctx = FeatureContext.ofUser("user123");
CheckoutService service = router.route(ctx);
service.checkout(order);

// Or use direct invocation
router.execute(ctx, s -> s.checkout(order));
Since:
JDK 25, opencode-base-feature V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • builder

      public static <T> VariantRouter.Builder<T> builder(String featureKey)
      Create a builder for the router 创建路由器构建器
      Type Parameters:
      T - the variant type | 变体类型
      Parameters:
      featureKey - the feature key for the A/B test | A/B测试的功能键
      Returns:
      new builder | 新的构建器
    • fromAnnotations

      public static <T> VariantRouter<VariantRouter.MethodVariant<T>> fromAnnotations(String featureKey, T target)
      Create router from annotated methods 从注解方法创建路由器
      Type Parameters:
      T - the target type | 目标类型
      Parameters:
      featureKey - the feature key | 功能键
      target - the target object containing @FeatureVariant methods | 包含@FeatureVariant方法的目标对象
      Returns:
      variant router | 变体路由器
    • route

      public T route(FeatureContext context)
      Route to a variant based on context 根据上下文路由到变体
      Parameters:
      context - the feature context | 功能上下文
      Returns:
      the selected variant | 选择的变体
      Throws:
      VariantRouter.NoVariantException - if no variants are registered | 如果没有注册变体
    • route

      public T route()
      Route with empty context 使用空上下文路由
      Returns:
      the selected variant | 选择的变体
    • routeForUser

      public T routeForUser(String userId)
      Route for a specific user 为特定用户路由
      Parameters:
      userId - the user ID | 用户ID
      Returns:
      the selected variant | 选择的变体
    • execute

      public <R> R execute(FeatureContext context, Function<T,R> action)
      Execute action on selected variant 在选择的变体上执行操作
      Type Parameters:
      R - the return type | 返回类型
      Parameters:
      context - the feature context | 功能上下文
      action - the action to execute | 要执行的操作
      Returns:
      the result | 结果
    • executeVoid

      public void executeVoid(FeatureContext context, Consumer<T> action)
      Execute void action on selected variant 在选择的变体上执行无返回值操作
      Parameters:
      context - the feature context | 功能上下文
      action - the action to execute | 要执行的操作
    • getVariant

      public Optional<T> getVariant(String variantId)
      Get a specific variant by ID 根据ID获取特定变体
      Parameters:
      variantId - the variant identifier | 变体标识符
      Returns:
      optional containing the variant | 包含变体的Optional
    • getSelectedVariantId

      public String getSelectedVariantId(FeatureContext context)
      Get the variant that would be selected for a context 获取将为上下文选择的变体ID
      Parameters:
      context - the feature context | 功能上下文
      Returns:
      the variant identifier | 变体标识符
    • getVariantIds

      public Set<String> getVariantIds()
      Get all registered variant IDs 获取所有注册的变体ID
      Returns:
      set of variant IDs | 变体ID集合
    • getFeatureKey

      public String getFeatureKey()
      Get the feature key 获取功能键
      Returns:
      the feature key | 功能键