Class UuidUtil

java.lang.Object
com.helixframework.id.uuid.UuidUtil

public final class UuidUtil extends Object
Utility class for working with UUIDs providing methods for generation, conversion, and validation of different UUID versions.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    A utility class for Version 4 UUID generation and manipulation.
    static final class 
     
    static final class 
    A utility class for Version 7 UUID generation and manipulation according to the IETF UUID draft specification.
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    compare(UUID uuid1, UUID uuid2)
    Compares two UUIDs lexicographically by comparing the most significant bits first, then the least significant bits.
    static UUID
    Creates UUID from URL-safe Base64 string.
    static UUID
    fromBytes(byte[] bytes)
    Creates UUID from byte array.
    static boolean
    isNil(String value)
    Checks if the UUID string represents a nil UUID.
    static boolean
    isNil(UUID uuid)
    Checks if the UUID is nil (all bits set to zero).
    static boolean
    Checks if UUID conforms to RFC 4122 specification.
    static boolean
    isV1(UUID uuid)
    Checks if UUID is version 1 (time-based).
    static boolean
    isV3(UUID uuid)
    Checks if UUID is version 3 (name-based using MD5 hashing).
    static boolean
    isV4(UUID uuid)
    Checks if UUID is version 4 (random or pseudo-random).
    static boolean
    isV5(UUID uuid)
    Checks if UUID is version 5 (name-based using SHA-1 hashing).
    static boolean
    isV6(UUID uuid)
    Checks if UUID is version 6 (reordered time-based).
    static boolean
    isV7(UUID uuid)
    Checks if UUID is version 7 (Unix Epoch time-based).
    static UUID
    nil()
    Creates a nil UUID (all bits set to zero).
    static String
    Generates a random UUID string.
    static UUID
    random(long seed)
    Creates a random UUID using the same value for both most and least significant bits.
    static UUID
    random(long seed1, long seed2)
    Creates a random UUID using separate values for most and least significant bits.
    static UUID
    random(UUID seed)
    Creates a new UUID by copying the bit values from an existing UUID.
    static String
    Converts UUID to URL-safe Base64 string.
    static byte[]
    toBytes(UUID uuid)
    Converts UUID to a byte array.
    static String
    toString(UUID uuid)
    Converts UUID to string representation.
    static UUID
    Attempts to parse a UUID string.
    v4()
    Creates a new V4 UUID builder instance.
    v5()
    Creates a new V5 UUID builder instance.
    v7()
    Creates a new V7 UUID builder instance.
    static int
    variant(UUID uuid)
    Gets the variant number of UUID.
    static int
    version(UUID uuid)
    Gets the version number of UUID.

    Methods inherited from class java.lang.Object

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

    • random

      public static String random()
      Generates a random UUID string.
      Returns:
      random UUID as string
    • random

      public static UUID random(long seed)
      Creates a random UUID using the same value for both most and least significant bits.
      Parameters:
      seed - value to use for both most and least significant bits
      Returns:
      randomly generated UUID
    • random

      public static UUID random(UUID seed)
      Creates a new UUID by copying the bit values from an existing UUID.
      Parameters:
      seed - UUID to copy bit values from
      Returns:
      new UUID with the same bit values as seed
    • random

      public static UUID random(long seed1, long seed2)
      Creates a random UUID using separate values for most and least significant bits.
      Parameters:
      seed1 - value to use for most significant bits
      seed2 - value to use for least significant bits
      Returns:
      randomly generated UUID
    • nil

      public static UUID nil()
      Creates a nil UUID (all bits set to zero).
      Returns:
      nil UUID
    • isNil

      public static boolean isNil(UUID uuid)
      Checks if the UUID is nil (all bits set to zero).
      Parameters:
      uuid - UUID to check
      Returns:
      true if UUID is nil, false otherwise
    • isNil

      public static boolean isNil(String value)
      Checks if the UUID string represents a nil UUID.
      Parameters:
      value - UUID string to check
      Returns:
      true if the string represents a nil UUID, false otherwise
    • tryParse

      public static UUID tryParse(String value)
      Attempts to parse a UUID string.
      Parameters:
      value - string to parse
      Returns:
      UUID if parsing successful, null otherwise
    • toBytes

      public static byte[] toBytes(UUID uuid)
      Converts UUID to a byte array. The UUID is converted by putting its most significant bits followed by the least significant bits into a 16-byte array using ByteBuffer.
      Parameters:
      uuid - UUID to convert
      Returns:
      16-byte array representation of UUID
      Throws:
      IllegalArgumentException - if uuid is null
    • fromBytes

      public static UUID fromBytes(byte[] bytes)
      Creates UUID from byte array.
      Parameters:
      bytes - byte array to convert
      Returns:
      UUID created from bytes
      Throws:
      IllegalArgumentException - if byte array is null or not 16 bytes long
    • toBase64Url

      public static String toBase64Url(UUID uuid)
      Converts UUID to URL-safe Base64 string.
      Parameters:
      uuid - UUID to convert
      Returns:
      Base64 URL-encoded string, or null if input UUID is null
    • fromBase64Url

      public static UUID fromBase64Url(String value)
      Creates UUID from URL-safe Base64 string.
      Parameters:
      value - Base64 URL-encoded string
      Returns:
      UUID created from Base64 string, or null if the input string is null
      Throws:
      IllegalArgumentException - if the decoded bytes are not a valid UUID (not 16 bytes)
    • compare

      public static int compare(UUID uuid1, UUID uuid2)
      Compares two UUIDs lexicographically by comparing the most significant bits first, then the least significant bits.
      Parameters:
      uuid1 - first UUID to compare
      uuid2 - second UUID to compare
      Returns:
      negative value if uuid1 less than uuid2, positive if uuid1 greater than uuid2, 0 if equal
      Throws:
      IllegalArgumentException - if either UUID is null
    • toString

      public static String toString(UUID uuid)
      Converts UUID to string representation.
      Parameters:
      uuid - UUID to convert
      Returns:
      string representation of UUID, or null if input UUID is null
    • version

      public static int version(UUID uuid)
      Gets the version number of UUID.
      Parameters:
      uuid - UUID to check
      Returns:
      version number of UUID (1-7), or -1 if uuid is null
    • variant

      public static int variant(UUID uuid)
      Gets the variant number of UUID.
      Parameters:
      uuid - UUID to check
      Returns:
      variant number of UUID (0-3), or -1 if uuid is null
    • isRfc4122

      public static boolean isRfc4122(UUID uuid)
      Checks if UUID conforms to RFC 4122 specification.
      Parameters:
      uuid - UUID to check
      Returns:
      true if UUID is RFC 4122 compliant, false otherwise
    • isV1

      public static boolean isV1(UUID uuid)
      Checks if UUID is version 1 (time-based).
      Parameters:
      uuid - UUID to check
      Returns:
      true if UUID is version 1, false otherwise
    • isV3

      public static boolean isV3(UUID uuid)
      Checks if UUID is version 3 (name-based using MD5 hashing).
      Parameters:
      uuid - UUID to check
      Returns:
      true if UUID is version 3, false otherwise
    • isV4

      public static boolean isV4(UUID uuid)
      Checks if UUID is version 4 (random or pseudo-random).
      Parameters:
      uuid - UUID to check
      Returns:
      true if UUID is version 4, false otherwise
    • isV5

      public static boolean isV5(UUID uuid)
      Checks if UUID is version 5 (name-based using SHA-1 hashing).
      Parameters:
      uuid - UUID to check
      Returns:
      true if UUID is version 5, false otherwise
    • isV6

      public static boolean isV6(UUID uuid)
      Checks if UUID is version 6 (reordered time-based).
      Parameters:
      uuid - UUID to check
      Returns:
      true if UUID is version 6, false otherwise
    • isV7

      public static boolean isV7(UUID uuid)
      Checks if UUID is version 7 (Unix Epoch time-based).
      Parameters:
      uuid - UUID to check
      Returns:
      true if UUID is version 7, false otherwise
    • v4

      public static UuidUtil.V4 v4()
      Creates a new V4 UUID builder instance.
      Returns:
      V4 UUID builder instance
    • v5

      public static UuidUtil.V5 v5()
      Creates a new V5 UUID builder instance.
      Returns:
      V5 UUID builder instance
    • v7

      public static UuidUtil.V7 v7()
      Creates a new V7 UUID builder instance.
      Returns:
      V7 UUID builder instance