Record Class FeatureContext

java.lang.Object
java.lang.Record
cloud.opencode.base.feature.FeatureContext
Record Components:
userId - the user ID | 用户ID
tenantId - the tenant ID | 租户ID
attributes - custom attributes map | 自定义属性映射

Security | 安全性:

  • Thread-safe: Yes (immutable record) - 线程安全: 是(不可变记录)
  • Null-safe: Partial (validates inputs) - 空值安全: 部分(验证输入)

public record FeatureContext(String userId, String tenantId, Map<String,Object> attributes) extends Record
Feature Context 功能上下文

Immutable context for feature evaluation.

用于功能评估的不可变上下文。

Features | 主要功能:

  • User context - 用户上下文
  • Tenant context - 租户上下文
  • Custom attributes - 自定义属性

Usage Examples | 使用示例:

// Empty context
FeatureContext context = FeatureContext.empty();

// User context
FeatureContext context = FeatureContext.ofUser("user-123");

// Full context with builder
FeatureContext context = FeatureContext.builder()
    .userId("user-123")
    .tenantId("tenant-456")
    .attribute("role", "admin")
    .build();
Since:
JDK 25, opencode-base-feature V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • FeatureContext

      public FeatureContext(String userId, String tenantId, Map<String,Object> attributes)
      Creates an instance of a FeatureContext record class.
      Parameters:
      userId - the value for the userId record component
      tenantId - the value for the tenantId record component
      attributes - the value for the attributes record component
  • Method Details

    • empty

      public static FeatureContext empty()
      Create an empty context 创建空上下文
      Returns:
      empty context | 空上下文
    • ofUser

      public static FeatureContext ofUser(String userId)
      Create a context with user ID only 仅使用用户ID创建上下文
      Parameters:
      userId - the user ID | 用户ID
      Returns:
      context with user | 带用户的上下文
    • ofTenant

      public static FeatureContext ofTenant(String tenantId)
      Create a context with tenant ID only 仅使用租户ID创建上下文
      Parameters:
      tenantId - the tenant ID | 租户ID
      Returns:
      context with tenant | 带租户的上下文
    • of

      public static FeatureContext of(String userId, String tenantId)
      Create a context with user and tenant 使用用户和租户创建上下文
      Parameters:
      userId - the user ID | 用户ID
      tenantId - the tenant ID | 租户ID
      Returns:
      context | 上下文
    • builder

      public static FeatureContext.Builder builder()
      Create a new builder 创建新的构建器
      Returns:
      new builder | 新的构建器
    • getAttribute

      public <T> T getAttribute(String key)
      Get an attribute value 获取属性值
      Type Parameters:
      T - the value type | 值类型
      Parameters:
      key - the attribute key | 属性键
      Returns:
      the attribute value or null | 属性值或null
    • getAttribute

      public <T> T getAttribute(String key, T defaultValue)
      Get an attribute value with default 获取属性值,带默认值
      Type Parameters:
      T - the value type | 值类型
      Parameters:
      key - the attribute key | 属性键
      defaultValue - the default value | 默认值
      Returns:
      the attribute value or default | 属性值或默认值
    • hasUserId

      public boolean hasUserId()
      Check if context has user ID 检查上下文是否有用户ID
      Returns:
      true if has user ID | 如果有用户ID返回true
    • hasTenantId

      public boolean hasTenantId()
      Check if context has tenant ID 检查上下文是否有租户ID
      Returns:
      true if has tenant ID | 如果有租户ID返回true
    • 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. All components in this record class are compared with Objects::equals(Object,Object).
      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.
    • userId

      public String userId()
      Returns the value of the userId record component.
      Returns:
      the value of the userId record component
    • tenantId

      public String tenantId()
      Returns the value of the tenantId record component.
      Returns:
      the value of the tenantId record component
    • attributes

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