Class TimeBasedEpochGenerator


public class TimeBasedEpochGenerator extends NoArgGenerator
Implementation of UUID generator that uses time/location based generation method field from the Unix Epoch timestamp source - the number of milliseconds seconds since midnight 1 Jan 1970 UTC, leap seconds excluded. This is usually referred to as "Version 7".

As all JUG provided implementations, this generator is fully thread-safe. Additionally it can also be made externally synchronized with other instances (even ones running on other JVMs); to do this, use FileBasedTimestampSynchronizer (or equivalent).

Since:
4.1
  • Field Details

    • _random

      protected final Random _random
      Random number generator that this generator uses.
    • _clock

      protected final UUIDClock _clock
      Underlying UUIDClock used for accessing current time, to use for generation.
      Since:
      4.3
  • Constructor Details

    • TimeBasedEpochGenerator

      public TimeBasedEpochGenerator(Random rnd)
      Parameters:
      rnd - Random number generator to use for generating UUIDs; if null, shared default generator is used. Note that it is strongly recommend to use a good (pseudo) random number generator; for example, JDK's SecureRandom.
    • TimeBasedEpochGenerator

      public TimeBasedEpochGenerator(Random rnd, UUIDClock clock)
      Parameters:
      rnd - Random number generator to use for generating UUIDs; if null, shared default generator is used. Note that it is strongly recommend to use a good (pseudo) random number generator; for example, JDK's SecureRandom.
      clock - clock Object used for accessing current time to use for generation
  • Method Details

    • getType

      public UUIDType getType()
      Description copied from class: UUIDGenerator
      Accessor for determining type of UUIDs (version) that this generator instance will produce.
      Specified by:
      getType in class UUIDGenerator
    • generate

      public UUID generate()
      Description copied from class: NoArgGenerator
      Method for generating a UUID.
      Specified by:
      generate in class NoArgGenerator
      Returns:
      Newly generated UUID
    • construct

      public UUID construct(long rawTimestamp)
      Method that will construct actual UUID instance for given unix epoch timestamp: called by generate() but may alternatively be called directly to construct an instance with known timestamp. NOTE: calling this method directly produces somewhat distinct UUIDs as "entropy" value is still generated as necessary to avoid producing same UUID even if same timestamp is being passed.
      Parameters:
      rawTimestamp - unix epoch millis
      Returns:
      unix epoch time based UUID
      Since:
      4.3