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字符串
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_abc123、inv_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
ConstructorsConstructorDescriptionPrefixedId(String prefix, String rawId) Compact canonical constructor with validation 带验证的紧凑规范构造方法 -
Method Summary
Modifier and TypeMethodDescriptionfinal booleanIndicates whether some other object is "equal to" this one.static PrefixedIdfromString(String prefixedId) Parses a full prefixed ID string (e.g., "usr_01ARZ3NDEK") 解析完整的带前缀ID字符串(如"usr_01ARZ3NDEK")fullId()Returns the full prefixed ID string (prefix + "_" + rawId) 返回完整的带前缀ID字符串(前缀 + "_" + 原始ID)final inthashCode()Returns a hash code value for this object.static booleanValidates whether a string is a valid prefixed ID 验证字符串是否是有效的带前缀IDstatic PrefixedIdCreates a PrefixedId from a prefix and raw ID 从前缀和原始ID创建PrefixedIdprefix()Returns the value of theprefixrecord component.rawId()Returns the value of therawIdrecord component.toString()Returns the full prefixed ID string 返回完整的带前缀ID字符串
-
Constructor Details
-
PrefixedId
-
-
Method Details
-
of
Creates a PrefixedId from a prefix and raw ID 从前缀和原始ID创建PrefixedIdExamples | 示例:
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
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
Validates whether a string is a valid prefixed ID 验证字符串是否是有效的带前缀ID- Parameters:
prefixedId- the string to validate | 要验证的字符串- Returns:
- true if valid | 如果有效返回true
-
fullId
Returns the full prefixed ID string (prefix + "_" + rawId) 返回完整的带前缀ID字符串(前缀 + "_" + 原始ID)- Returns:
- full ID string | 完整ID字符串
-
toString
-
hashCode
-
equals
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 withObjects::equals(Object,Object). -
prefix
-
rawId
-