Class UlidGenerator
java.lang.Object
cloud.opencode.base.id.ulid.UlidGenerator
- All Implemented Interfaces:
IdGenerator<String>
ULID Generator
ULID生成器
Generates Universally Unique Lexicographically Sortable Identifiers. ULID is a 128-bit identifier encoded as 26 characters using Crockford's Base32.
生成通用唯一字典序可排序标识符。ULID是128位标识符, 使用Crockford的Base32编码为26个字符。
ULID Structure | ULID结构:
01AN4Z07BY 79KA1307SR9X4MV3 |----------| |----------------| Timestamp Randomness 48bits 80bits
Features | 主要功能:
- Lexicographically sortable - 字典序可排序
- Case insensitive - 大小写不敏感
- URL safe - URL安全
- Monotonic within same millisecond - 同毫秒内单调递增
Usage Examples | 使用示例:
UlidGenerator gen = UlidGenerator.create();
String ulid = gen.generate();
// -> "01ARZ3NDEKTSV4RRFFQ69G5FAV"
// Validate
boolean valid = UlidGenerator.isValid(ulid);
// Compare
int cmp = UlidGenerator.compare(ulid1, ulid2);
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Since:
- JDK 25, opencode-base-id V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic intCompares two ULID strings 比较两个ULID字符串static UlidGeneratorcreate()Creates a ULID generator 创建ULID生成器generate()Generates the next ID 生成下一个IDgenerate(long timestamp) Generates a ULID with specific timestamp 使用指定时间戳生成ULIDbyte[]Generates ULID as byte array 生成ULID字节数组getType()Gets the generator type name 获取生成器类型名称static booleanValidates a ULID string 验证ULID字符串static UlidParser.ParsedUlidParses a ULID string 解析ULID字符串Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface IdGenerator
generateBatch
-
Method Details
-
create
-
generate
Description copied from interface:IdGeneratorGenerates the next ID 生成下一个ID- Specified by:
generatein interfaceIdGenerator<String>- Returns:
- generated ID | 生成的ID
-
generate
Generates a ULID with specific timestamp 使用指定时间戳生成ULID- Parameters:
timestamp- the timestamp in milliseconds | 时间戳(毫秒)- Returns:
- ULID string (26 characters) | ULID字符串(26字符)
- Throws:
IllegalStateException- if randomness overflows within same millisecond
-
generateBytes
public byte[] generateBytes()Generates ULID as byte array 生成ULID字节数组- Returns:
- 16-byte array | 16字节数组
-
isValid
Validates a ULID string 验证ULID字符串- Parameters:
ulid- the ULID string | ULID字符串- Returns:
- true if valid | 如果有效返回true
-
parse
Parses a ULID string 解析ULID字符串- Parameters:
ulid- the ULID string | ULID字符串- Returns:
- parsed result | 解析结果
-
compare
Compares two ULID strings 比较两个ULID字符串- Parameters:
ulid1- first ULID | 第一个ULIDulid2- second ULID | 第二个ULID- Returns:
- comparison result | 比较结果
- Throws:
NullPointerException- if either argument is null
-
getType
Description copied from interface:IdGeneratorGets the generator type name 获取生成器类型名称- Specified by:
getTypein interfaceIdGenerator<String>- Returns:
- type name | 类型名称
-