Class IdConverter

java.lang.Object
cloud.opencode.base.id.IdConverter

public final class IdConverter extends Object
ID Format Converter Utility ID格式转换工具

Provides utilities for converting IDs between different formats. Useful for generating short URLs, API paths, and format interoperability.

提供ID在不同格式之间转换的工具。 适用于生成短链接、API路径和格式互操作。

Supported Conversions | 支持的转换:

  • Long ID ↔ Base62/Base36 string
  • ULID ↔ UUID interconversion
  • UUID ↔ Base62 string
  • Snowflake ID ↔ Base62 string

Usage Examples | 使用示例:

// Long to Base62
String shortId = IdConverter.toBase62(snowflakeId);
long id = IdConverter.fromBase62(shortId);

// ULID to UUID
UUID uuid = IdConverter.ulidToUuid(ulid);
String ulid = IdConverter.uuidToUlid(uuid);

Features | 主要功能:

  • ID format conversion between different representations - 不同表示形式之间的ID格式转换
  • Base62/Base36 encoding and decoding - Base62/Base36编码和解码
  • Numeric to string ID conversion - 数值到字符串ID转换

Security | 安全性:

  • Thread-safe: No - 线程安全: 否
  • Null-safe: Yes (validates inputs) - 空值安全: 是(验证输入)

Performance | 性能特性:

  • Time complexity: O(log₆₂ n) for toBase62/fromBase62/toBase36/fromBase36 where n=numeric value; O(1) for ulidToUuid/uuidToUlid (fixed 16-byte/26-char structures); O(b) for encodeBytesToBase62/decodeBase62ToBytes where b=byte count (22 iterations × 16 bytes each) - 时间复杂度: toBase62/fromBase62/toBase36/fromBase36 为 O(log₆₂ n);ulidToUuid/uuidToUlid 为 O(1)(固定结构);encodeBytesToBase62/decodeBase62ToBytes 为 O(b)
  • Space complexity: O(1) - output strings are of bounded fixed length - 空间复杂度: O(1) - 输出字符串长度有界固定
Since:
JDK 25, opencode-base-id V1.1.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static long
    Converts a Base62 string back to Snowflake ID 将Base62字符串转换回雪花ID
    static UUID
    Converts a Base62 string back to UUID 将Base62字符串转换回UUID
    static long
    Converts a Base36 string back to long ID 将Base36字符串转换回长整型ID
    static long
    Converts a Base58 string back to long ID 将Base58字符串转换回长整型ID
    static long
    Converts a Base62 string back to long ID 将Base62字符串转换回长整型ID
    static boolean
    Validates a Base36 string 验证Base36字符串
    static boolean
    Validates whether a string is valid Base58 验证字符串是否是有效的Base58
    static boolean
    Validates a Base62 string 验证Base62字符串
    static String
    snowflakeToBase62(long snowflakeId)
    Converts a Snowflake ID to Base62 string (shorter representation) 将雪花ID转换为Base62字符串(更短的表示)
    static String
    toBase36(long id)
    Converts a long ID to Base36 string (case-insensitive) 将长整型ID转换为Base36字符串(大小写不敏感)
    static String
    toBase58(long id)
    Converts a long ID to Base58 string (Bitcoin-style alphabet, no ambiguous characters) 将长整型ID转换为Base58字符串(比特币风格字母表,无歧义字符)
    static String
    toBase62(long id)
    Converts a long ID to Base62 string 将长整型ID转换为Base62字符串
    static UUID
    Converts a ULID string to UUID 将ULID字符串转换为UUID
    static String
    Converts a UUID to Base62 string (compact representation) 将UUID转换为Base62字符串(紧凑表示)
    static String
    Converts a UUID to ULID string 将UUID转换为ULID字符串

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • toBase62

      public static String toBase62(long id)
      Converts a long ID to Base62 string 将长整型ID转换为Base62字符串
      Parameters:
      id - the ID | ID
      Returns:
      Base62 string | Base62字符串
    • fromBase62

      public static long fromBase62(String base62)
      Converts a Base62 string back to long ID 将Base62字符串转换回长整型ID
      Parameters:
      base62 - the Base62 string | Base62字符串
      Returns:
      the ID | ID
      Throws:
      IllegalArgumentException - if the string is invalid
    • toBase36

      public static String toBase36(long id)
      Converts a long ID to Base36 string (case-insensitive) 将长整型ID转换为Base36字符串(大小写不敏感)
      Parameters:
      id - the ID | ID
      Returns:
      Base36 string | Base36字符串
    • fromBase36

      public static long fromBase36(String base36)
      Converts a Base36 string back to long ID 将Base36字符串转换回长整型ID
      Parameters:
      base36 - the Base36 string | Base36字符串
      Returns:
      the ID | ID
      Throws:
      NumberFormatException - if the string is invalid
    • ulidToUuid

      public static UUID ulidToUuid(String ulid)
      Converts a ULID string to UUID 将ULID字符串转换为UUID

      Both ULID and UUID are 128-bit, so the conversion is lossless.

      ULID和UUID都是128位,因此转换是无损的。

      Parameters:
      ulid - the ULID string (26 characters) | ULID字符串(26字符)
      Returns:
      UUID | UUID
      Throws:
      IllegalArgumentException - if the ULID is invalid
    • uuidToUlid

      public static String uuidToUlid(UUID uuid)
      Converts a UUID to ULID string 将UUID转换为ULID字符串
      Parameters:
      uuid - the UUID | UUID
      Returns:
      ULID string (26 characters) | ULID字符串(26字符)
    • snowflakeToBase62

      public static String snowflakeToBase62(long snowflakeId)
      Converts a Snowflake ID to Base62 string (shorter representation) 将雪花ID转换为Base62字符串(更短的表示)
      Parameters:
      snowflakeId - the Snowflake ID | 雪花ID
      Returns:
      Base62 string (typically 10-11 characters) | Base62字符串(通常10-11字符)
    • base62ToSnowflake

      public static long base62ToSnowflake(String base62)
      Converts a Base62 string back to Snowflake ID 将Base62字符串转换回雪花ID
      Parameters:
      base62 - the Base62 string | Base62字符串
      Returns:
      Snowflake ID | 雪花ID
    • uuidToBase62

      public static String uuidToBase62(UUID uuid)
      Converts a UUID to Base62 string (compact representation) 将UUID转换为Base62字符串(紧凑表示)
      Parameters:
      uuid - the UUID | UUID
      Returns:
      Base62 string (22 characters) | Base62字符串(22字符)
    • base62ToUuid

      public static UUID base62ToUuid(String base62)
      Converts a Base62 string back to UUID 将Base62字符串转换回UUID
      Parameters:
      base62 - the Base62 string (22 characters) | Base62字符串(22字符)
      Returns:
      UUID | UUID
    • isValidBase62

      public static boolean isValidBase62(String str)
      Validates a Base62 string 验证Base62字符串
      Parameters:
      str - the string to validate | 要验证的字符串
      Returns:
      true if valid | 如果有效返回true
    • isValidBase36

      public static boolean isValidBase36(String str)
      Validates a Base36 string 验证Base36字符串
      Parameters:
      str - the string to validate | 要验证的字符串
      Returns:
      true if valid | 如果有效返回true
    • toBase58

      public static String toBase58(long id)
      Converts a long ID to Base58 string (Bitcoin-style alphabet, no ambiguous characters) 将长整型ID转换为Base58字符串(比特币风格字母表,无歧义字符)

      Base58 avoids visually ambiguous characters (0, O, I, l) making it suitable for human-readable short IDs in URLs and QR codes.

      Base58避免视觉上模糊的字符(0、O、I、l),适合在URL和QR码中使用人类可读的短ID。

      Examples | 示例:

      toBase58(0L)          = "1"  // zero maps to '1' (Bitcoin convention)
      toBase58(57L)         = "z"
      toBase58(58L)         = "21"
      toBase58(1000000000L) = "2QGPK"
      

      Performance | 性能:

      Time: O(log₅₈ n), Space: O(1)

      时间: O(log₅₈ n), 空间: O(1)

      Parameters:
      id - the ID (treated as unsigned long) | ID(视为无符号长整型)
      Returns:
      Base58 encoded string | Base58编码字符串
    • fromBase58

      public static long fromBase58(String base58)
      Converts a Base58 string back to long ID 将Base58字符串转换回长整型ID

      Examples | 示例:

      fromBase58("1")       = 0L
      fromBase58("2QGPK")   = 1000000000L
      fromBase58("21")      = 58L
      

      Performance | 性能:

      Time: O(n) where n=string length, Space: O(1)

      Parameters:
      base58 - the Base58 encoded string | Base58编码字符串
      Returns:
      the decoded ID | 解码的ID
      Throws:
      IllegalArgumentException - if the string is null, empty, or contains invalid characters | 字符串为null、空或含无效字符时抛出
    • isValidBase58

      public static boolean isValidBase58(String str)
      Validates whether a string is valid Base58 验证字符串是否是有效的Base58
      Parameters:
      str - the string to validate | 要验证的字符串
      Returns:
      true if the string is a valid Base58 value | 如果是有效Base58字符串则返回true