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 |
 
  • 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:
      • classloaderId
    • 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