Class UlidGenerator
java.lang.Object
cloud.opencode.base.id.ulid.UlidGenerator
- All Implemented Interfaces:
IdGenerator<String>
ULID Generator - Universally Unique Lexicographically Sortable Identifier
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 (configurable) - 同毫秒内单调递增(可配置)
- Non-monotonic mode via
UlidConfig- 通过UlidConfig配置非单调模式
Usage Examples | 使用示例:
// Monotonic (default)
UlidGenerator gen = UlidGenerator.create();
String ulid = gen.generate();
// -> "01ARZ3NDEKTSV4RRFFQ69G5FAV"
// Non-monotonic
UlidGenerator gen2 = UlidGenerator.create(UlidConfig.nonMonotonic());
String ulid2 = gen2.generate();
// Validate
boolean valid = UlidGenerator.isValid(ulid);
// Compare
int cmp = UlidGenerator.compare(ulid1, ulid2);
Security | 安全性:
- Thread-safe: Yes (synchronized) - 线程安全: 是(同步)
- Entropy: SecureRandom - 熵源: SecureRandom
- 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 default monotonic ULID generator (singleton) 创建默认单调ULID生成器(单例)static UlidGeneratorcreate(UlidConfig config) Creates a ULID generator with explicit configuration 使用显式配置创建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 获取生成器类型名称booleanReturns whether this generator uses monotonic mode 返回此生成器是否使用单调模式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
Creates a default monotonic ULID generator (singleton) 创建默认单调ULID生成器(单例)- Returns:
- monotonic generator | 单调生成器
-
create
Creates a ULID generator with explicit configuration 使用显式配置创建ULID生成器Examples | 示例:
UlidGenerator.create(UlidConfig.defaultConfig()) // monotonic singleton UlidGenerator.create(UlidConfig.nonMonotonic()) // new non-monotonic instance
- Parameters:
config- the ULID configuration | ULID配置- Returns:
- generator | 生成器
-
isMonotonic
public boolean isMonotonic()Returns whether this generator uses monotonic mode 返回此生成器是否使用单调模式- Returns:
- true if monotonic | 如果是单调模式返回true
-
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字符)
-
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 | 类型名称
-