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:
  • Constructor Details

  • Method Details

    • builder

      public static Feature.Builder builder(String key)
      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

      public boolean isEnabled(FeatureContext context)
      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

      public <T> T getMetadata(String key)
      Get metadata value 获取元数据值
      Type Parameters:
      T - the value type | 值类型
      Parameters:
      key - the metadata key | 元数据键
      Returns:
      the metadata value or null | 元数据值或null
    • getMetadata

      public <T> T getMetadata(String key, T defaultValue)
      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

      public Feature withStrategy(EnableStrategy newStrategy)
      Create a copy with updated strategy 创建具有更新策略的副本
      Parameters:
      newStrategy - the new strategy | 新策略
      Returns:
      new Feature with updated strategy | 具有更新策略的新Feature
    • withGroup

      public Feature withGroup(String newGroup)
      Create a copy with updated group 创建具有更新分组的副本
      Parameters:
      newGroup - the new group name | 新组名称
      Returns:
      new Feature with updated group | 具有更新分组的新Feature
    • withLifecycle

      public Feature withLifecycle(FeatureLifecycle newLifecycle)
      Create a copy with updated lifecycle 创建具有更新生命周期的副本
      Parameters:
      newLifecycle - the new lifecycle state | 新生命周期状态
      Returns:
      new Feature with updated lifecycle | 具有更新生命周期的新Feature
    • withExpiresAt

      public Feature withExpiresAt(Instant newExpiresAt)
      Create a copy with updated expiration time 创建具有更新过期时间的副本
      Parameters:
      newExpiresAt - the new expiration time | 新过期时间
      Returns:
      new Feature with updated expiration | 具有更新过期时间的新Feature
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      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 with Objects::equals(Object,Object); primitive components are compared with the compare method from their corresponding wrapper classes.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • key

      public String key()
      Returns the value of the key record component.
      Returns:
      the value of the key record component
    • name

      public String name()
      Returns the value of the name record component.
      Returns:
      the value of the name record component
    • description

      public String description()
      Returns the value of the description record component.
      Returns:
      the value of the description record component
    • defaultEnabled

      public boolean defaultEnabled()
      Returns the value of the defaultEnabled record component.
      Returns:
      the value of the defaultEnabled record component
    • strategy

      public EnableStrategy strategy()
      Returns the value of the strategy record component.
      Returns:
      the value of the strategy record component
    • metadata

      public Map<String,Object> metadata()
      Returns the value of the metadata record component.
      Returns:
      the value of the metadata record component
    • group

      public String group()
      Returns the value of the group record component.
      Returns:
      the value of the group record component
    • expiresAt

      public Instant expiresAt()
      Returns the value of the expiresAt record component.
      Returns:
      the value of the expiresAt record component
    • lifecycle

      public FeatureLifecycle lifecycle()
      Returns the value of the lifecycle record component.
      Returns:
      the value of the lifecycle record component
    • createdAt

      public Instant createdAt()
      Returns the value of the createdAt record component.
      Returns:
      the value of the createdAt record component
    • updatedAt

      public Instant updatedAt()
      Returns the value of the updatedAt record component.
      Returns:
      the value of the updatedAt record component