Class TimestampIdGenerator
java.lang.Object
cloud.opencode.base.id.simple.TimestampIdGenerator
- All Implemented Interfaces:
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 Summary
Modifier and TypeMethodDescriptionstatic TimestampIdGeneratorcreate()Creates a generator with default random length (6) 创建使用默认随机长度(6)的生成器static TimestampIdGeneratorcreate(int randomLength) Creates a generator with specific random length 使用指定随机长度创建生成器generate()Generates the next ID 生成下一个IDGenerates an ID with prefix 生成带前缀的IDgetType()Gets the generator type name 获取生成器类型名称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 generator with default random length (6) 创建使用默认随机长度(6)的生成器- Returns:
- generator | 生成器
-
create
Creates a generator with specific random length 使用指定随机长度创建生成器- Parameters:
randomLength- the random digits length | 随机数字长度- Returns:
- generator | 生成器
-
generate
Description copied from interface:IdGeneratorGenerates the next ID 生成下一个ID- Specified by:
generatein interfaceIdGenerator<String>- Returns:
- generated ID | 生成的ID
-
generate
-
getType
Description copied from interface:IdGeneratorGets the generator type name 获取生成器类型名称- Specified by:
getTypein interfaceIdGenerator<String>- Returns:
- type name | 类型名称
-