Class Uuid7

java.lang.Object
org.apache.juneau.commons.utils.Uuid7

public final class Uuid7 extends Object
Generator for RFC 9562 §5.7 version-7 UUIDs.

A v7 UUID is time-ordered: a 48-bit big-endian Unix-milliseconds timestamp in the most-significant bits, followed by the RFC 9562 version (7) and variant nibbles and SecureRandom-filled random bits. No JDK API produces v7 natively as of the Java 17 baseline this repo targets.

The SecureRandom used to fill the random bits is cached per thread (ThreadLocal) so minting is not a fresh SecureRandom construction (and potential entropy syscall) on the request path.

See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static UUID
    Generates a new version-7 UUID using the current wall-clock time.
    static UUID
    create(Clock clock)
    Generates a new version-7 UUID whose timestamp is read from the specified clock.
    static String
    Generates a new version-7 UUID and returns its canonical string form.
    static long
    Extracts the 48-bit Unix-milliseconds timestamp encoded in a v7 UUID's most-significant bits.

    Methods inherited from class java.lang.Object

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

    • create

      public static UUID create()
      Generates a new version-7 UUID using the current wall-clock time.
      Returns:
      A new v7 UUID. Never null.
    • create

      public static UUID create(Clock clock)
      Generates a new version-7 UUID whose timestamp is read from the specified clock.

      Lets tests pin the encoded timestamp with Clock.fixed(Instant, ZoneId). The random bits are still SecureRandom-filled, so two UUIDs minted against the same fixed clock differ.

      Parameters:
      clock - The clock supplying the Unix-milliseconds timestamp. Must not be null.
      Returns:
      A new v7 UUID. Never null.
    • createString

      public static String createString()
      Generates a new version-7 UUID and returns its canonical string form.
      Returns:
      The string form of a new v7 UUID. Never null.
    • timestamp

      public static long timestamp(UUID uuid)
      Extracts the 48-bit Unix-milliseconds timestamp encoded in a v7 UUID's most-significant bits.
      Parameters:
      uuid - The UUID. Must not be null.
      Returns:
      The encoded Unix-milliseconds timestamp.