Record Class Feature
java.lang.Object
java.lang.Record
cloud.opencode.base.feature.Feature
- Record Components:
key- the unique feature key | 唯一功能键name- the display name | 显示名称description- the description | 描述defaultEnabled- default enabled state | 默认启用状态strategy- the enable strategy | 启用策略metadata- custom metadata | 自定义元数据group- the feature group name (nullable) | 功能组名称(可为null)expiresAt- expiration time (nullable, null = never expires) | 过期时间(可为null,null = 永不过期)lifecycle- the lifecycle state | 生命周期状态createdAt- creation timestamp | 创建时间戳updatedAt- update timestamp | 更新时间戳Security | 安全性:
- Thread-safe: Yes (immutable record) - 线程安全: 是(不可变记录)
- Null-safe: Partial (validates inputs) - 空值安全: 部分(验证输入)
public record Feature(String key, String name, String description, boolean defaultEnabled, EnableStrategy strategy, Map<String,Object> metadata, String group, Instant expiresAt, FeatureLifecycle lifecycle, Instant createdAt, Instant updatedAt)
extends Record
Feature Definition
功能定义
Immutable record representing a feature toggle.
表示功能开关的不可变记录。
Features | 主要功能:
- Feature key and metadata - 功能键和元数据
- Enable strategy - 启用策略
- Default value - 默认值
- Group support - 分组支持
- Expiration support - 过期支持
- Lifecycle management - 生命周期管理
- Timestamps - 时间戳
Usage Examples | 使用示例:
// Simple feature
Feature feature = Feature.builder("dark-mode")
.name("Dark Mode")
.description("Enable dark theme")
.alwaysOn()
.build();
// Percentage rollout
Feature feature = Feature.builder("new-feature")
.percentage(10)
.build();
// User whitelist
Feature feature = Feature.builder("beta-feature")
.forUsers("user1", "user2")
.build();
// Feature with group and expiration
Feature feature = Feature.builder("promo-banner")
.group("marketing")
.expiresAfter(Duration.ofDays(30))
.lifecycle(FeatureLifecycle.ACTIVE)
.build();
- Since:
- JDK 25, opencode-base-feature V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionFeature(String key, String name, String description, boolean defaultEnabled, EnableStrategy strategy, Map<String, Object> metadata, String group, Instant expiresAt, FeatureLifecycle lifecycle, Instant createdAt, Instant updatedAt) Compact constructor with validation and defensive copy 带验证和防御性复制的紧凑构造函数 -
Method Summary
Modifier and TypeMethodDescriptionstatic Feature.BuilderCreate a builder for feature 创建功能构建器Returns the value of thecreatedAtrecord component.booleanReturns the value of thedefaultEnabledrecord component.Returns the value of thedescriptionrecord component.final booleanIndicates whether some other object is "equal to" this one.Returns the value of theexpiresAtrecord component.<T> TgetMetadata(String key) Get metadata value 获取元数据值<T> TgetMetadata(String key, T defaultValue) Get metadata value with default 获取元数据值,带默认值group()Returns the value of thegrouprecord component.final inthashCode()Returns a hash code value for this object.booleanCheck if this feature is enabled with empty context 使用空上下文检查此功能是否启用booleanisEnabled(FeatureContext context) Check if this feature is enabled with context 使用上下文检查此功能是否启用booleanCheck if this feature has expired 检查此功能是否已过期booleanisUsable()Check if this feature is usable based on its lifecycle state 根据生命周期状态检查此功能是否可用key()Returns the value of thekeyrecord component.Returns the value of thelifecyclerecord component.metadata()Returns the value of themetadatarecord component.name()Returns the value of thenamerecord component.strategy()Returns the value of thestrategyrecord component.final StringtoString()Returns a string representation of this record class.Returns the value of theupdatedAtrecord component.withExpiresAt(Instant newExpiresAt) Create a copy with updated expiration time 创建具有更新过期时间的副本Create a copy with updated group 创建具有更新分组的副本withLifecycle(FeatureLifecycle newLifecycle) Create a copy with updated lifecycle 创建具有更新生命周期的副本withStrategy(EnableStrategy newStrategy) Create a copy with updated strategy 创建具有更新策略的副本
-
Constructor Details
-
Feature
public Feature(String key, String name, String description, boolean defaultEnabled, EnableStrategy strategy, Map<String, Object> metadata, String group, Instant expiresAt, FeatureLifecycle lifecycle, Instant createdAt, Instant updatedAt) Compact constructor with validation and defensive copy 带验证和防御性复制的紧凑构造函数
-
-
Method Details
-
builder
Create a builder for feature 创建功能构建器- Parameters:
key- the feature key | 功能键- Returns:
- new builder | 新的构建器
-
isEnabled
public boolean isEnabled()Check if this feature is enabled with empty context 使用空上下文检查此功能是否启用- Returns:
- true if enabled | 如果启用返回true
-
isEnabled
Check if this feature is enabled with context 使用上下文检查此功能是否启用- Parameters:
context- the evaluation context | 评估上下文- Returns:
- true if enabled | 如果启用返回true
-
isExpired
public boolean isExpired()Check if this feature has expired 检查此功能是否已过期- Returns:
- true if expired | 如果已过期返回true
-
isUsable
public boolean isUsable()Check if this feature is usable based on its lifecycle state 根据生命周期状态检查此功能是否可用- Returns:
- true if usable | 如果可用返回true
-
getMetadata
Get metadata value 获取元数据值- Type Parameters:
T- the value type | 值类型- Parameters:
key- the metadata key | 元数据键- Returns:
- the metadata value or null | 元数据值或null
-
getMetadata
Get metadata value with default 获取元数据值,带默认值- Type Parameters:
T- the value type | 值类型- Parameters:
key- the metadata key | 元数据键defaultValue- the default value | 默认值- Returns:
- the metadata value or default | 元数据值或默认值
-
withStrategy
Create a copy with updated strategy 创建具有更新策略的副本- Parameters:
newStrategy- the new strategy | 新策略- Returns:
- new Feature with updated strategy | 具有更新策略的新Feature
-
withGroup
-
withLifecycle
Create a copy with updated lifecycle 创建具有更新生命周期的副本- Parameters:
newLifecycle- the new lifecycle state | 新生命周期状态- Returns:
- new Feature with updated lifecycle | 具有更新生命周期的新Feature
-
withExpiresAt
-
toString
-
hashCode
-
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared withObjects::equals(Object,Object); primitive components are compared with thecomparemethod from their corresponding wrapper classes. -
key
-
name
-
description
Returns the value of thedescriptionrecord component.- Returns:
- the value of the
descriptionrecord component
-
defaultEnabled
public boolean defaultEnabled()Returns the value of thedefaultEnabledrecord component.- Returns:
- the value of the
defaultEnabledrecord component
-
strategy
-
metadata
-
group
-
expiresAt
-
lifecycle
-
createdAt
-
updatedAt
-