Class TimeOrderedEpochFactory


  • public final class TimeOrderedEpochFactory
    extends AbstCombFactory
    Concrete factory for creating Unix epoch time-ordered unique identifiers (UUIDv7).

    UUIDv7 is a new UUID version proposed by Peabody and Davis. It is similar to Prefix COMB GUID and ULID.

    This factory creates 3 types:

    • Type 1 (default): this type is divided in 3 components, namely time, counter and random. The counter component is incremented by 1 when the time repeats. The random component is always randomized.
    • Type 2 (plus 1): this type is divided in 2 components, namely time and monotonic random. The monotonic random component is incremented by 1 when the time repeats. This type of UUID is like a Monotonic ULID. It can be much faster than the other types.
    • Type 3 (plus n): this type is also divided in 2 components, namely time and monotonic random. The monotonic random component is incremented by a random positive integer between 1 and 2^32. This type of UUID is also like a Monotonic ULID, but with a random increment instead of 1.

    If the underlying runtime provides enough clock precision, the microseconds are also injected in the UUID, specifically in the rand_a field, which is the name RFC 9562 gives to the 12 bits right after the milliseconds field, from left to right. Otherwise, these 12 bits are randomly generated.

    In JDK 11, we get 1 microsecond precision. However, in JDK 8, the maximum precision we can get is 1 millisecond. On Windows, it is even worse because the default precision is 15.625ms, due to the system clock's refresh rate of 64Hz.

    Since:
    5.0.0
    See Also:
    PrefixCombFactory, ULID Specification, New UUID formats, Revise Universally Unique Identifier Definitions (uuidrev)
    • Constructor Detail

      • TimeOrderedEpochFactory

        public TimeOrderedEpochFactory()
        Default constructor.
      • TimeOrderedEpochFactory

        public TimeOrderedEpochFactory​(Clock clock)
        Constructor with a clock.
        Parameters:
        clock - a clock
      • TimeOrderedEpochFactory

        public TimeOrderedEpochFactory​(Random random)
        Constructor with a random.
        Parameters:
        random - a random
      • TimeOrderedEpochFactory

        public TimeOrderedEpochFactory​(Random random,
                                       Clock clock)
        Constructor with a random and a clock.
        Parameters:
        random - a random
        clock - a clock
      • TimeOrderedEpochFactory

        public TimeOrderedEpochFactory​(LongSupplier randomFunction)
        Constructor with a function which return random numbers.
        Parameters:
        randomFunction - a function
      • TimeOrderedEpochFactory

        public TimeOrderedEpochFactory​(LongSupplier randomFunction,
                                       Clock clock)
        Constructor with a function which a function which return random numbers and a clock.
        Parameters:
        randomFunction - a function
        clock - a clock
    • Method Detail

      • create

        public UUID create()
        Returns a time-ordered unique identifier (UUIDv7).
        Specified by:
        create in class UuidFactory
        Returns:
        a UUIDv7
      • create

        public UUID create​(UuidFactory.Parameters parameters)
        Returns a time-ordered unique identifier (UUIDv7) for a given instant.

        The random component is generated with each method invocation.

        Overrides:
        create in class AbstRandomBasedFactory
        Parameters:
        instant - a given instant
        Returns:
        a UUIDv7