Class OpenFeature
java.lang.Object
cloud.opencode.base.feature.OpenFeature
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 Summary
Modifier and TypeMethodDescriptionvoidaddListener(FeatureListener listener) Add a listener 添加监听器voidclear()Clear all features 清空所有功能static OpenFeaturecreate(FeatureStore store) Create new instance with custom store (for testing or custom usage) 使用自定义存储创建新实例(用于测试或自定义用途)booleanDelete a feature 删除功能voidDisable a feature 禁用功能voiddisableGroup(String group) Disable all features in a group 禁用组中的所有功能voidEnable a feature 启用功能voidenableGroup(String group) Enable all features in a group 启用组中的所有功能booleanCheck if a feature exists 检查功能是否存在Get a feature by key 根据键获取功能getAll()Get all features 获取所有功能Get all feature keys 获取所有功能键getByGroup(String group) Get all features in a group 获取组中的所有功能static OpenFeatureGet singleton instance 获取单例实例getOrThrow(String key) Get a feature or throw if not found 获取功能,如果未找到则抛出异常getStore()Get the feature store 获取功能存储voidifEnabled(String key, FeatureContext context, Runnable action) Execute action if feature is enabled with context 如果功能启用则执行操作(带上下文)<T> TifEnabled(String key, FeatureContext context, Supplier<T> enabled, Supplier<T> disabled) Get value based on feature state with context 根据功能状态获取值(带上下文)voidExecute action if feature is enabled 如果功能启用则执行操作<T> TGet value based on feature state 根据功能状态获取值booleanCheck if a feature is enabled 检查功能是否启用booleanisEnabled(String key, FeatureContext context) Check if a feature is enabled for a context 检查功能对上下文是否启用booleanisEnabledForUser(String key, String userId) Check if a feature is enabled for a specific user 检查功能对特定用户是否启用voidRegister a feature 注册功能voidregisterAll(Feature... features) Register multiple features 注册多个功能voidremoveListener(FeatureListener listener) Remove a listener 移除监听器static voidReset singleton instance (for testing) 重置单例实例(用于测试)voidrestore(FeatureSnapshot snapshot) Restore feature state from a snapshot 从快照恢复功能状态voidsetStore(FeatureStore store) Set the feature store 设置功能存储intsize()Get the feature count 获取功能数量snapshot()Create a snapshot of current feature state 创建当前功能状态的快照voidupdateStrategy(String key, EnableStrategy strategy) Update feature strategy 更新功能策略
-
Method Details
-
getInstance
Get singleton instance 获取单例实例- Returns:
- the OpenFeature instance | OpenFeature实例
-
create
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
Register a feature 注册功能- Parameters:
feature- the feature to register | 要注册的功能
-
registerAll
Register multiple features 注册多个功能- Parameters:
features- the features to register | 要注册的功能
-
isEnabled
Check if a feature is enabled 检查功能是否启用- Parameters:
key- the feature key | 功能键- Returns:
- true if enabled | 如果启用返回true
-
isEnabled
Check if a feature is enabled for a context 检查功能对上下文是否启用- Parameters:
key- the feature key | 功能键context- the evaluation context | 评估上下文- Returns:
- true if enabled | 如果启用返回true
-
isEnabledForUser
-
ifEnabled
-
ifEnabled
Execute action if feature is enabled with context 如果功能启用则执行操作(带上下文)- Parameters:
key- the feature key | 功能键context- the evaluation context | 评估上下文action- the action to execute | 要执行的操作
-
ifEnabled
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
-
getOrThrow
Get a feature or throw if not found 获取功能,如果未找到则抛出异常- Parameters:
key- the feature key | 功能键- Returns:
- the feature | 功能
- Throws:
FeatureNotFoundException- if not found | 如果未找到
-
enable
-
disable
-
updateStrategy
Update feature strategy 更新功能策略- Parameters:
key- the feature key | 功能键strategy- the new strategy | 新策略
-
delete
Delete a feature 删除功能- Parameters:
key- the feature key | 功能键- Returns:
- true if deleted | 如果删除返回true
-
getAllKeys
-
getAll
-
exists
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
Add a listener 添加监听器- Parameters:
listener- the listener | 监听器
-
removeListener
Remove a listener 移除监听器- Parameters:
listener- the listener | 监听器
-
getStore
-
setStore
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
-
enableGroup
Enable all features in a group 启用组中的所有功能- Parameters:
group- the group name | 组名称
-
disableGroup
Disable all features in a group 禁用组中的所有功能- Parameters:
group- the group name | 组名称
-
snapshot
Create a snapshot of current feature state 创建当前功能状态的快照- Returns:
- the snapshot | 快照
-
restore
Restore feature state from a snapshot 从快照恢复功能状态- Parameters:
snapshot- the snapshot to restore | 要恢复的快照
-