Record Class PrefixedId

java.lang.Object
java.lang.Record
cloud.opencode.base.id.prefixed.PrefixedId
Record Components:
prefix - the entity-type prefix (e.g., "usr", "order") | 实体类型前缀(如"usr"、"order")
rawId - the underlying ID string | 底层ID字符串

public record PrefixedId(String prefix, String rawId) extends Record
Prefixed ID - Type-safe ID with entity-type prefix (Stripe/TypeID style) 带前缀的ID - 带实体类型前缀的类型安全ID(Stripe/TypeID风格)

Represents an immutable prefixed identifier in the style popularized by Stripe (e.g., cus_abc123, inv_xyz456) and TypeID. The prefix encodes the entity type, making IDs self-documenting in logs, APIs, and debugging sessions.

表示不可变的带前缀标识符,风格参考Stripe(如cus_abc123inv_xyz456) 和TypeID。前缀编码实体类型,使ID在日志、API和调试中具有自描述性。

Features | 主要功能:

  • Entity-type prefix for self-documenting IDs - 实体类型前缀,ID自描述
  • Prefix validation (lowercase letters/digits/underscores) - 前缀验证(小写字母/数字/下划线)
  • Parse from string with fromString(String) - 通过fromString解析字符串
  • Works with any underlying ID format - 兼容任何底层ID格式

Usage Examples | 使用示例:

// Create a prefixed ID
PrefixedId userId = PrefixedId.of("usr", "01ARZ3NDEKTSV4RRFFQ69G5FAV");
System.out.println(userId);  // "usr_01ARZ3NDEKTSV4RRFFQ69G5FAV"

// Parse from string
PrefixedId parsed = PrefixedId.fromString("order_7ZYQP4T89A");
System.out.println(parsed.prefix());  // "order"
System.out.println(parsed.rawId());   // "7ZYQP4T89A"

// Validate
boolean valid = PrefixedId.isValid("usr_01ARZ3NDEK");  // true
boolean bad   = PrefixedId.isValid("User_123");         // false (uppercase prefix)

Prefix Rules | 前缀规则:

  • Must match [a-z][a-z0-9_]{0,30} — lowercase start, max 31 chars total
  • Must start with a lowercase letter (not digit or underscore)
  • May contain lowercase letters, digits, and underscores

Security | 安全性:

  • Thread-safe: Yes (immutable record) - 线程安全: 是(不可变记录)
  • Null-safe: No (throws on null) - 空值安全: 否(空值抛异常)
Since:
JDK 25, opencode-base-id V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    PrefixedId(String prefix, String rawId)
    Compact canonical constructor with validation 带验证的紧凑规范构造方法
  • Method Summary

    Modifier and Type
    Method
    Description
    final boolean
    Indicates whether some other object is "equal to" this one.
    static PrefixedId
    fromString(String prefixedId)
    Parses a full prefixed ID string (e.g., "usr_01ARZ3NDEK") 解析完整的带前缀ID字符串(如"usr_01ARZ3NDEK")
    Returns the full prefixed ID string (prefix + "_" + rawId) 返回完整的带前缀ID字符串(前缀 + "_" + 原始ID)
    final int
    Returns a hash code value for this object.
    static boolean
    isValid(String prefixedId)
    Validates whether a string is a valid prefixed ID 验证字符串是否是有效的带前缀ID
    static PrefixedId
    of(String prefix, String rawId)
    Creates a PrefixedId from a prefix and raw ID 从前缀和原始ID创建PrefixedId
    Returns the value of the prefix record component.
    Returns the value of the rawId record component.
    Returns the full prefixed ID string 返回完整的带前缀ID字符串

    Methods inherited from class Object

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

    • PrefixedId

      public PrefixedId(String prefix, String rawId)
      Compact canonical constructor with validation 带验证的紧凑规范构造方法
  • Method Details

    • of

      public static PrefixedId of(String prefix, String rawId)
      Creates a PrefixedId from a prefix and raw ID 从前缀和原始ID创建PrefixedId

      Examples | 示例:

      PrefixedId.of("usr", "01ARZ3NDEK")   = PrefixedId[prefix=usr, rawId=01ARZ3NDEK]
      PrefixedId.of("order_item", "abc123") = PrefixedId[prefix=order_item, rawId=abc123]
      
      Parameters:
      prefix - the entity-type prefix | 实体类型前缀
      rawId - the underlying ID | 底层ID
      Returns:
      PrefixedId instance | PrefixedId实例
      Throws:
      OpenIdGenerationException - if prefix is invalid or rawId is null/empty | 前缀无效或rawId为空时抛出
    • fromString

      public static PrefixedId fromString(String prefixedId)
      Parses a full prefixed ID string (e.g., "usr_01ARZ3NDEK") 解析完整的带前缀ID字符串(如"usr_01ARZ3NDEK")

      Examples | 示例:

      PrefixedId.fromString("usr_01ARZ3NDEK")   → prefix="usr",  rawId="01ARZ3NDEK"
      PrefixedId.fromString("order_abc_123")    → prefix="order", rawId="abc_123"
      
      Parameters:
      prefixedId - the full prefixed ID string | 完整的带前缀ID字符串
      Returns:
      parsed PrefixedId | 解析后的PrefixedId
      Throws:
      OpenIdGenerationException - if the format is invalid | 格式无效时抛出
    • isValid

      public static boolean isValid(String prefixedId)
      Validates whether a string is a valid prefixed ID 验证字符串是否是有效的带前缀ID
      Parameters:
      prefixedId - the string to validate | 要验证的字符串
      Returns:
      true if valid | 如果有效返回true
    • fullId

      public String fullId()
      Returns the full prefixed ID string (prefix + "_" + rawId) 返回完整的带前缀ID字符串(前缀 + "_" + 原始ID)
      Returns:
      full ID string | 完整ID字符串
    • toString

      public String toString()
      Returns the full prefixed ID string 返回完整的带前缀ID字符串
      Specified by:
      toString in class Record
      Returns:
      full ID string | 完整ID字符串
    • 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.
    • prefix

      public String prefix()
      Returns the value of the prefix record component.
      Returns:
      the value of the prefix record component
    • rawId

      public String rawId()
      Returns the value of the rawId record component.
      Returns:
      the value of the rawId record component