Package io.cucumber.core.eventbus
Class IncrementingUuidGenerator
java.lang.Object
io.cucumber.core.eventbus.IncrementingUuidGenerator
- All Implemented Interfaces:
UuidGenerator,Supplier<UUID>
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 Summary
FieldsModifier and TypeFieldDescription(package private) static longClassloader identifier (MSB).(package private) final AtomicLongCounter for the UUID LSB.(package private) static final long62 bits mask for the counter value (LSB)private static final long40 bits mask for the epoch-time part (MSB).(package private) static final long8 bits mask for the session identifier (MSB).private longComputed UUID MSB value.(package private) static final AtomicLongSession counter to differentiate instances created within a given classloader (MSB). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionGenerate a new UUID.private longstatic voidsetClassloaderId(int classloaderId) Defines a new classloaderId for the class.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.cucumber.core.eventbus.UuidGenerator
get
-
Field Details
-
MAX_EPOCH_TIME
private static final long MAX_EPOCH_TIME40 bits mask for the epoch-time part (MSB).- See Also:
-
MAX_SESSION_ID
static final long MAX_SESSION_ID8 bits mask for the session identifier (MSB). Package-private for testing purposes.- See Also:
-
MAX_COUNTER_VALUE
static final long MAX_COUNTER_VALUE62 bits mask for the counter value (LSB)- See Also:
-
classloaderId
static long classloaderIdClassloader 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 thesetClassloaderId(int)method. Note: there is no need to save the Random because it's static. -
sessionCounter
Session counter to differentiate instances created within a given classloader (MSB). -
msb
private long msbComputed UUID MSB value. -
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 togenerateId()(the instances created before the call keep their classloaderId). This method should be called to specify aclassloaderIdif 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
Generate a new UUID. Will throw an exception when out of capacity.- Specified by:
generateIdin interfaceUuidGenerator- Returns:
- a non-null UUID
- Throws:
CucumberException- when out of capacity
-