Class TimestampIdGenerator

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

public final class TimestampIdGenerator extends Object implements IdGenerator<String>
Timestamp-based ID Generator 基于时间戳的ID生成器

Generates IDs in format: yyyyMMddHHmmssSSS + random digits. Suitable for order numbers, transaction IDs, etc.

生成格式为:yyyyMMddHHmmssSSS + 随机数字的ID。 适用于订单号、交易ID等场景。

Format | 格式:

20240101123456789001234
|       |    |   | |
| date  |time|ms |random

Features | 主要功能:

  • Time-ordered - 时间有序
  • Human-readable timestamp - 人类可读时间戳
  • Configurable random length - 可配置随机数长度
  • Optional prefix support - 可选前缀支持

Usage Examples | 使用示例:

TimestampIdGenerator gen = TimestampIdGenerator.create();
String id = gen.generate();
// -> "202401011234567890001234"

// With prefix
String orderId = gen.generate("ORD");
// -> "ORD202401011234567890001234"

// Custom random length
TimestampIdGenerator gen2 = TimestampIdGenerator.create(6);
String shortId = gen2.generate();
// -> "20240101123456789123456"

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 TimestampIdGenerator create()
      Creates a generator with default random length (6) 创建使用默认随机长度(6)的生成器
      Returns:
      generator | 生成器
    • create

      public static TimestampIdGenerator create(int randomLength)
      Creates a generator with specific random length 使用指定随机长度创建生成器
      Parameters:
      randomLength - the random digits length | 随机数字长度
      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(String prefix)
      Generates an ID with prefix 生成带前缀的ID
      Parameters:
      prefix - the prefix | 前缀
      Returns:
      ID string | ID字符串
    • getType

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