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 | 自定义元数据
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, Instant createdAt, Instant updatedAt) extends Record
Feature Definition 功能定义

Immutable record representing a feature toggle.

表示功能开关的不可变记录。

Features | 主要功能:

  • Feature key and metadata - 功能键和元数据
  • Enable strategy - 启用策略
  • Default value - 默认值
  • 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();
Since:
JDK 25, opencode-base-feature V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • Feature

      public Feature(String key, String name, String description, boolean defaultEnabled, EnableStrategy strategy, Map<String,Object> metadata, Instant createdAt, Instant updatedAt)
      Creates an instance of a Feature record class.
      Parameters:
      key - the value for the key record component
      name - the value for the name record component
      description - the value for the description record component
      defaultEnabled - the value for the defaultEnabled record component
      strategy - the value for the strategy record component
      metadata - the value for the metadata record component
      createdAt - the value for the createdAt record component
      updatedAt - the value for the updatedAt record component
  • 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
    • 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
    • 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
    • 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