Record Class TypedKey<T>

java.lang.Object
java.lang.Record
cloud.opencode.base.rules.key.TypedKey<T>
Type Parameters:
T - the value type | 值类型
Record Components:
name - the key name | 键名称
type - the value type | 值类型

public record TypedKey<T>(String name, Class<T> type) extends Record
Typed Key - Type-Safe Key for Fact and Variable Access 类型化键 - 用于事实和变量访问的类型安全键

Provides compile-time type safety when accessing values from FactStore and RuleContext, eliminating the need for manual casting.

在从 FactStoreRuleContext 访问值时提供编译时类型安全, 消除手动转换的需要。

Usage Examples | 使用示例:

TypedKey<String> CUSTOMER_TYPE = TypedKey.of("customerType", String.class);
TypedKey<Double> ORDER_AMOUNT = TypedKey.of("orderAmount", Double.class);

// Type-safe access
context.put(CUSTOMER_TYPE, "VIP");
String type = context.get(CUSTOMER_TYPE);
Since:
JDK 25, opencode-base-rules V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    TypedKey(String name, Class<T> type)
    Canonical constructor with validation 带验证的规范构造函数
  • Method Summary

    Modifier and Type
    Method
    Description
    final boolean
    Indicates whether some other object is "equal to" this one.
    final int
    Returns a hash code value for this object.
    Returns the value of the name record component.
    static <T> TypedKey<T>
    of(String name, Class<T> type)
    Creates a typed key with the given name and type 使用给定名称和类型创建类型化键
    Returns a string representation of this record class.
    Returns the value of the type record component.

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • TypedKey

      public TypedKey(String name, Class<T> type)
      Canonical constructor with validation 带验证的规范构造函数
      Parameters:
      name - the key name | 键名称
      type - the value type | 值类型
      Throws:
      NullPointerException - if name or type is null | 如果name或type为null则抛出
  • Method Details

    • of

      public static <T> TypedKey<T> of(String name, Class<T> type)
      Creates a typed key with the given name and type 使用给定名称和类型创建类型化键
      Type Parameters:
      T - the value type | 值类型
      Parameters:
      name - the key name | 键名称
      type - the value type | 值类型
      Returns:
      the typed key | 类型化键
      Throws:
      NullPointerException - if name or type is null | 如果name或type为null则抛出
    • toString

      public 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.
    • name

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

      public Class<T> type()
      Returns the value of the type record component.
      Returns:
      the value of the type record component