Class UlidGenerator

java.lang.Object
cloud.opencode.base.id.ulid.UlidGenerator
All Implemented Interfaces:
IdGenerator<String>

public final class UlidGenerator extends Object implements 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 Details

    • create

      public static UlidGenerator create()
      Creates a ULID generator 创建ULID生成器
      Returns:
      generator | 生成器
    • generate

      public String generate()
      Description copied from interface: IdGenerator
      Generates the next ID 生成下一个ID
      Specified by:
      generate in interface IdGenerator<String>
      Returns:
      generated ID | 生成的ID
    • generate

      public String generate(long timestamp)
      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

      public static boolean isValid(String ulid)
      Validates a ULID string 验证ULID字符串
      Parameters:
      ulid - the ULID string | ULID字符串
      Returns:
      true if valid | 如果有效返回true
    • parse

      public static UlidParser.ParsedUlid parse(String ulid)
      Parses a ULID string 解析ULID字符串
      Parameters:
      ulid - the ULID string | ULID字符串
      Returns:
      parsed result | 解析结果
    • compare

      public static int compare(String ulid1, String ulid2)
      Compares two ULID strings 比较两个ULID字符串
      Parameters:
      ulid1 - first ULID | 第一个ULID
      ulid2 - second ULID | 第二个ULID
      Returns:
      comparison result | 比较结果
      Throws:
      NullPointerException - if either argument is null
    • getType

      public String getType()
      Description copied from interface: IdGenerator
      Gets the generator type name 获取生成器类型名称
      Specified by:
      getType in interface IdGenerator<String>
      Returns:
      type name | 类型名称