Class OpenFeature

java.lang.Object
cloud.opencode.base.feature.OpenFeature

public class OpenFeature extends Object
Open Feature 开放功能

Main facade class for feature toggle management.

功能开关管理的主外观类。

Features | 主要功能:

  • Feature registration - 功能注册
  • Feature evaluation - 功能评估
  • Strategy management - 策略管理
  • Listener support - 监听器支持
  • Pluggable storage - 可插拔存储
  • Group operations - 分组操作
  • Snapshot/restore - 快照/恢复

Usage Examples | 使用示例:

// Get singleton instance
OpenFeature features = OpenFeature.getInstance();

// Register a feature
Feature darkMode = Feature.builder("dark-mode")
    .name("Dark Mode")
    .defaultEnabled(false)
    .strategy(new PercentageStrategy(30))
    .build();
features.register(darkMode);

// Check if enabled
if (features.isEnabled("dark-mode")) {
    // Use dark mode
}

// Check with context
FeatureContext ctx = FeatureContext.builder()
    .userId("user123")
    .build();
if (features.isEnabled("dark-mode", ctx)) {
    // Use dark mode for this user
}

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Null-safe: Partial (validates inputs) - 空值安全: 部分(验证输入)
Since:
JDK 25, opencode-base-feature V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • getInstance

      public static OpenFeature getInstance()
      Get singleton instance 获取单例实例
      Returns:
      the OpenFeature instance | OpenFeature实例
    • create

      public static OpenFeature create(FeatureStore store)
      Create new instance with custom store (for testing or custom usage) 使用自定义存储创建新实例(用于测试或自定义用途)
      Parameters:
      store - the feature store | 功能存储
      Returns:
      new OpenFeature instance | 新的OpenFeature实例
    • resetInstance

      public static void resetInstance()
      Reset singleton instance (for testing) 重置单例实例(用于测试)
    • register

      public void register(Feature feature)
      Register a feature 注册功能
      Parameters:
      feature - the feature to register | 要注册的功能
    • registerAll

      public void registerAll(Feature... features)
      Register multiple features 注册多个功能
      Parameters:
      features - the features to register | 要注册的功能
    • isEnabled

      public boolean isEnabled(String key)
      Check if a feature is enabled 检查功能是否启用
      Parameters:
      key - the feature key | 功能键
      Returns:
      true if enabled | 如果启用返回true
    • isEnabled

      public boolean isEnabled(String key, FeatureContext context)
      Check if a feature is enabled for a context 检查功能对上下文是否启用
      Parameters:
      key - the feature key | 功能键
      context - the evaluation context | 评估上下文
      Returns:
      true if enabled | 如果启用返回true
    • isEnabledForUser

      public boolean isEnabledForUser(String key, String userId)
      Check if a feature is enabled for a specific user 检查功能对特定用户是否启用
      Parameters:
      key - the feature key | 功能键
      userId - the user id | 用户ID
      Returns:
      true if enabled | 如果启用返回true
    • ifEnabled

      public void ifEnabled(String key, Runnable action)
      Execute action if feature is enabled 如果功能启用则执行操作
      Parameters:
      key - the feature key | 功能键
      action - the action to execute | 要执行的操作
    • ifEnabled

      public void ifEnabled(String key, FeatureContext context, Runnable action)
      Execute action if feature is enabled with context 如果功能启用则执行操作(带上下文)
      Parameters:
      key - the feature key | 功能键
      context - the evaluation context | 评估上下文
      action - the action to execute | 要执行的操作
    • ifEnabled

      public <T> T ifEnabled(String key, Supplier<T> enabled, Supplier<T> disabled)
      Get value based on feature state 根据功能状态获取值
      Type Parameters:
      T - the return type | 返回类型
      Parameters:
      key - the feature key | 功能键
      enabled - supplier for enabled state | 启用状态的供应者
      disabled - supplier for disabled state | 禁用状态的供应者
      Returns:
      the result | 结果
    • ifEnabled

      public <T> T ifEnabled(String key, FeatureContext context, Supplier<T> enabled, Supplier<T> disabled)
      Get value based on feature state with context 根据功能状态获取值(带上下文)
      Type Parameters:
      T - the return type | 返回类型
      Parameters:
      key - the feature key | 功能键
      context - the evaluation context | 评估上下文
      enabled - supplier for enabled state | 启用状态的供应者
      disabled - supplier for disabled state | 禁用状态的供应者
      Returns:
      the result | 结果
    • get

      public Optional<Feature> get(String key)
      Get a feature by key 根据键获取功能
      Parameters:
      key - the feature key | 功能键
      Returns:
      optional containing feature | 包含功能的Optional
    • getOrThrow

      public Feature getOrThrow(String key)
      Get a feature or throw if not found 获取功能,如果未找到则抛出异常
      Parameters:
      key - the feature key | 功能键
      Returns:
      the feature | 功能
      Throws:
      FeatureNotFoundException - if not found | 如果未找到
    • enable

      public void enable(String key)
      Enable a feature 启用功能
      Parameters:
      key - the feature key | 功能键
    • disable

      public void disable(String key)
      Disable a feature 禁用功能
      Parameters:
      key - the feature key | 功能键
    • updateStrategy

      public void updateStrategy(String key, EnableStrategy strategy)
      Update feature strategy 更新功能策略
      Parameters:
      key - the feature key | 功能键
      strategy - the new strategy | 新策略
    • delete

      public boolean delete(String key)
      Delete a feature 删除功能
      Parameters:
      key - the feature key | 功能键
      Returns:
      true if deleted | 如果删除返回true
    • getAllKeys

      public Set<String> getAllKeys()
      Get all feature keys 获取所有功能键
      Returns:
      set of feature keys | 功能键集合
    • getAll

      public Map<String,Feature> getAll()
      Get all features 获取所有功能
      Returns:
      map of key to feature | 键到功能的映射
    • exists

      public boolean exists(String key)
      Check if a feature exists 检查功能是否存在
      Parameters:
      key - the feature key | 功能键
      Returns:
      true if exists | 如果存在返回true
    • size

      public int size()
      Get the feature count 获取功能数量
      Returns:
      number of features | 功能数量
    • clear

      public void clear()
      Clear all features 清空所有功能
    • addListener

      public void addListener(FeatureListener listener)
      Add a listener 添加监听器
      Parameters:
      listener - the listener | 监听器
    • removeListener

      public void removeListener(FeatureListener listener)
      Remove a listener 移除监听器
      Parameters:
      listener - the listener | 监听器
    • getStore

      public FeatureStore getStore()
      Get the feature store 获取功能存储
      Returns:
      the feature store | 功能存储
    • setStore

      public void setStore(FeatureStore store)
      Set the feature store 设置功能存储

      Allows switching to a different feature store at runtime. Existing features will not be migrated automatically.

      允许在运行时切换到不同的功能存储。现有功能不会自动迁移。

      Parameters:
      store - the new feature store | 新的功能存储
      Throws:
      NullPointerException - if store is null | 如果store为null
    • getByGroup

      public List<Feature> getByGroup(String group)
      Get all features in a group 获取组中的所有功能
      Parameters:
      group - the group name | 组名称
      Returns:
      list of features in the group | 组中的功能列表
    • enableGroup

      public void enableGroup(String group)
      Enable all features in a group 启用组中的所有功能
      Parameters:
      group - the group name | 组名称
    • disableGroup

      public void disableGroup(String group)
      Disable all features in a group 禁用组中的所有功能
      Parameters:
      group - the group name | 组名称
    • snapshot

      public FeatureSnapshot snapshot()
      Create a snapshot of current feature state 创建当前功能状态的快照
      Returns:
      the snapshot | 快照
    • restore

      public void restore(FeatureSnapshot snapshot)
      Restore feature state from a snapshot 从快照恢复功能状态
      Parameters:
      snapshot - the snapshot to restore | 要恢复的快照