Class OpenUuid

java.lang.Object
cloud.opencode.base.id.uuid.OpenUuid

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

    • randomUuid

      public static UUID randomUuid()
      Generates a random UUID (v4) 生成随机UUID(v4)
      Returns:
      UUID | UUID
    • timeOrderedUuid

      public static UUID timeOrderedUuid()
      Generates a time-ordered UUID (v7) 生成时间有序UUID(v7)
      Returns:
      UUID | UUID
    • nameBasedMd5Uuid

      public static UUID nameBasedMd5Uuid(UUID namespace, String name)
      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) | 命名空间UUID
      name - the name | 名称
      Returns:
      UUID v3 | UUID v3
    • toSimpleString

      public static String toSimpleString(UUID uuid)
      Converts UUID to simple string (no hyphens) 将UUID转换为简化字符串(无连字符)
      Parameters:
      uuid - the UUID | UUID
      Returns:
      simple string (32 chars) | 简化字符串(32字符)
    • fromSimpleString

      public static UUID fromSimpleString(String simple)
      Parses a simple string to UUID 将简化字符串解析为UUID
      Parameters:
      simple - the simple string | 简化字符串
      Returns:
      UUID | UUID
      Throws:
      IllegalArgumentException - if format is invalid | 如果格式无效
    • toBytes

      public static byte[] toBytes(UUID uuid)
      Converts UUID to byte array 将UUID转换为字节数组
      Parameters:
      uuid - the UUID | UUID
      Returns:
      16-byte array | 16字节数组
    • fromBytes

      public static UUID fromBytes(byte[] bytes)
      Parses byte array to UUID 将字节数组解析为UUID
      Parameters:
      bytes - 16-byte array | 16字节数组
      Returns:
      UUID | UUID
      Throws:
      IllegalArgumentException - if byte array is invalid | 如果字节数组无效
    • getVersion

      public static int getVersion(UUID uuid)
      Gets the UUID version 获取UUID版本
      Parameters:
      uuid - the UUID | UUID
      Returns:
      version number | 版本号
    • getVariant

      public static int getVariant(UUID uuid)
      Gets the UUID variant 获取UUID变体
      Parameters:
      uuid - the UUID | UUID
      Returns:
      variant number | 变体号
    • isValid

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

      public static boolean isValidSimple(String simple)
      Validates a simple UUID string 验证简化UUID字符串
      Parameters:
      simple - the simple UUID string | 简化UUID字符串
      Returns:
      true if valid | 如果有效返回true
    • extractTimestamp

      public static long extractTimestamp(UUID uuid)
      Extracts timestamp from UUID v7 从UUID v7提取时间戳
      Parameters:
      uuid - the UUID | UUID
      Returns:
      timestamp in milliseconds, or -1 if not v7 | 时间戳(毫秒),如果不是v7返回-1