Class UuidV7Generator
java.lang.Object
cloud.opencode.base.id.uuid.UuidV7Generator
- All Implemented Interfaces:
IdGenerator<UUID>
UUID v7 Generator
UUID v7生成器
Generates time-ordered UUIDs based on RFC 9562 version 7. The first 48 bits contain Unix timestamp in milliseconds, making these UUIDs naturally sortable and database-friendly.
基于RFC 9562版本7生成时间有序的UUID。前48位包含Unix毫秒时间戳, 使这些UUID天然可排序且对数据库友好。
UUID v7 Structure | UUID v7结构:
0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | unix_ts_ms (48 bits) | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | unix_ts_ms | ver | rand_a (12 bits) | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |var| rand_b (62 bits) | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | rand_b | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Features | 主要功能:
- Time-ordered for database efficiency - 时间有序,数据库高效
- Monotonic within same millisecond - 同毫秒内单调递增
- Lexicographically sortable - 字典序可排序
Usage Examples | 使用示例:
UuidV7Generator gen = UuidV7Generator.create();
UUID uuid = gen.generate();
// Extract timestamp
long timestamp = UuidV7Generator.extractTimestamp(uuid);
// Check if v7
boolean isV7 = UuidV7Generator.isV7(uuid);
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 UuidV7Generatorcreate()Creates a UUID v7 generator 创建UUID v7生成器static longextractTimestamp(UUID uuid) Extracts the timestamp from a UUID v7 从UUID v7提取时间戳generate()Generates the next ID 生成下一个IDGenerates a UUID as string 生成UUID字符串getType()Gets the generator type name 获取生成器类型名称static booleanChecks if a UUID is version 7 检查UUID是否为版本7Methods 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 UUID v7 generator 创建UUID v7生成器- Returns:
- generator | 生成器
-
generate
Description copied from interface:IdGeneratorGenerates the next ID 生成下一个ID- Specified by:
generatein interfaceIdGenerator<UUID>- Returns:
- generated ID | 生成的ID
-
generateStr
-
extractTimestamp
Extracts the timestamp from a UUID v7 从UUID v7提取时间戳- Parameters:
uuid- the UUID | UUID- Returns:
- timestamp in milliseconds | 时间戳(毫秒)
-
isV7
Checks if a UUID is version 7 检查UUID是否为版本7- Parameters:
uuid- the UUID | UUID- Returns:
- true if v7 | 如果是v7返回true
-
getType
Description copied from interface:IdGeneratorGets the generator type name 获取生成器类型名称- Specified by:
getTypein interfaceIdGenerator<UUID>- Returns:
- type name | 类型名称
-