Class IncrementingUuidGenerator

java.lang.Object
io.cucumber.core.eventbus.IncrementingUuidGenerator
All Implemented Interfaces:
UuidGenerator, Supplier<UUID>

public class IncrementingUuidGenerator extends Object implements UuidGenerator
Thread-safe and collision-free UUID generator for single JVM. This is a sequence generator and each instance has its own counter. This generator is about 100 times faster than #RandomUuidGenerator.

Properties: - thread-safe - collision-free in the same classloader - almost collision-free in different classloaders / JVMs - UUIDs generated using the instances from the same classloader are sortable

UUID version 8 (custom) / variant 2

 |       40 bits      |      8 bits    |  4 bits |    12 bits    |  2 bits | 62 bits |
 | -------------------| -------------- | ------- | ------------- | ------- | ------- |
 | LSBs of epoch-time | sessionCounter | version | classloaderId | variant | counter |
 
  • Field Details

    • MAX_EPOCH_TIME

      private static final long MAX_EPOCH_TIME
      40 bits mask for the epoch-time part (MSB).
      See Also:
    • MAX_SESSION_ID

      static final long MAX_SESSION_ID
      8 bits mask for the session identifier (MSB). Package-private for testing purposes.
      See Also:
    • MAX_COUNTER_VALUE

      static final long MAX_COUNTER_VALUE
      62 bits mask for the counter value (LSB)
      See Also:
    • classloaderId

      static long classloaderId
      Classloader identifier (MSB). The identifier is a pseudo-random number on 12 bits. Since we use a random value (and cut it to 12 bits), there is a small probability that two classloaders generate the same 12 bits random number. This could lead to UUID collisions if the UUID parts (epoch-time, session counter and counter) are the same. The default `classloaderId` (random number cut to 12 bits) has a collision rate of less than 1% when using 10 classloaders (which leads to a much smaller UUID probability thanks to the other dynamic parts of the UUID like epoch-time and counters). If you use multiple classloaders and want to ensure a collision-free UUID generation, you need to provide the `classloaderId` by yourself. You can do so using the setClassloaderId(int) method. Note: there is no need to save the Random because it's static.
    • sessionCounter

      static final AtomicLong sessionCounter
      Session counter to differentiate instances created within a given classloader (MSB).
    • msb

      private long msb
      Computed UUID MSB value.
    • counter

      final AtomicLong counter
      Counter for the UUID LSB.
  • Constructor Details

    • IncrementingUuidGenerator

      public IncrementingUuidGenerator()
  • Method Details

    • setClassloaderId

      public static void setClassloaderId(int classloaderId)
      Defines a new classloaderId for the class. This only affects instances created after the first call to generateId() (the instances created before the call keep their classloaderId). This method should be called to specify a classloaderId if you are using more than one class loader, and you want to guarantee a collision-free UUID generation (instead of the default random classloaderId which produces about 1% collision rate on the classloaderId, and thus can have UUID collision if the epoch-time, session counter and counter have the same values).
      Parameters:
      classloaderId - the new classloaderId (only the least significant 12 bits are used)
      See Also:
    • initializeMsb

      private long initializeMsb()
    • generateId

      public UUID generateId()
      Generate a new UUID. Will throw an exception when out of capacity.
      Specified by:
      generateId in interface UuidGenerator
      Returns:
      a non-null UUID
      Throws:
      CucumberException - when out of capacity