Class OpenUuid
java.lang.Object
cloud.opencode.base.id.uuid.OpenUuid
UUID Utility Class
UUID工具类
Provides utility methods for UUID generation, conversion, and validation.
提供UUID生成、转换和验证的工具方法。
Features | 主要功能:
- UUID generation (v4, v7) - UUID生成(v4, v7)
- String conversion - 字符串转换
- Byte array conversion - 字节数组转换
- Validation - 验证
Usage Examples | 使用示例:
// Generate UUIDs
UUID v4 = OpenUuid.randomUuid();
UUID v7 = OpenUuid.timeOrderedUuid();
// Convert to/from simple string
String simple = OpenUuid.toSimpleString(uuid);
UUID parsed = OpenUuid.fromSimpleString(simple);
// Convert to/from bytes
byte[] bytes = OpenUuid.toBytes(uuid);
UUID fromBytes = OpenUuid.fromBytes(bytes);
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 longextractTimestamp(UUID uuid) Extracts timestamp from UUID v7 从UUID v7提取时间戳static UUIDfromBytes(byte[] bytes) Parses byte array to UUID 将字节数组解析为UUIDstatic UUIDfromSimpleString(String simple) Parses a simple string to UUID 将简化字符串解析为UUIDstatic intgetVariant(UUID uuid) Gets the UUID variant 获取UUID变体static intgetVersion(UUID uuid) Gets the UUID version 获取UUID版本static booleanValidates a UUID string 验证UUID字符串static booleanisValidSimple(String simple) Validates a simple UUID string 验证简化UUID字符串static UUIDnameBasedMd5Uuid(UUID namespace, String name) Generates a deterministic name-based UUID (v3, MD5) for the given namespace and name.static UUIDGenerates a random UUID (v4) 生成随机UUID(v4)static UUIDGenerates a time-ordered UUID (v7) 生成时间有序UUID(v7)static byte[]Converts UUID to byte array 将UUID转换为字节数组static StringtoSimpleString(UUID uuid) Converts UUID to simple string (no hyphens) 将UUID转换为简化字符串(无连字符)
-
Method Details
-
randomUuid
-
timeOrderedUuid
Generates a time-ordered UUID (v7) 生成时间有序UUID(v7)- Returns:
- UUID | UUID
-
nameBasedMd5Uuid
Generates a deterministic name-based UUID (v3, MD5) for the given namespace and name. The same inputs always produce the same UUID. 为给定命名空间和名称生成确定性基于名称的UUID(v3,MD5)。 相同的输入始终产生相同的UUID。- Parameters:
namespace- the namespace UUID (e.g.UuidV3Generator.NAMESPACE_DNS) | 命名空间UUIDname- the name | 名称- Returns:
- UUID v3 | UUID v3
-
toSimpleString
-
fromSimpleString
Parses a simple string to UUID 将简化字符串解析为UUID- Parameters:
simple- the simple string | 简化字符串- Returns:
- UUID | UUID
- Throws:
IllegalArgumentException- if format is invalid | 如果格式无效
-
toBytes
Converts UUID to byte array 将UUID转换为字节数组- Parameters:
uuid- the UUID | UUID- Returns:
- 16-byte array | 16字节数组
-
fromBytes
Parses byte array to UUID 将字节数组解析为UUID- Parameters:
bytes- 16-byte array | 16字节数组- Returns:
- UUID | UUID
- Throws:
IllegalArgumentException- if byte array is invalid | 如果字节数组无效
-
getVersion
Gets the UUID version 获取UUID版本- Parameters:
uuid- the UUID | UUID- Returns:
- version number | 版本号
-
getVariant
Gets the UUID variant 获取UUID变体- Parameters:
uuid- the UUID | UUID- Returns:
- variant number | 变体号
-
isValid
Validates a UUID string 验证UUID字符串- Parameters:
uuidStr- the UUID string | UUID字符串- Returns:
- true if valid | 如果有效返回true
-
isValidSimple
Validates a simple UUID string 验证简化UUID字符串- Parameters:
simple- the simple UUID string | 简化UUID字符串- Returns:
- true if valid | 如果有效返回true
-
extractTimestamp
Extracts timestamp from UUID v7 从UUID v7提取时间戳- Parameters:
uuid- the UUID | UUID- Returns:
- timestamp in milliseconds, or -1 if not v7 | 时间戳(毫秒),如果不是v7返回-1
-