Package com.github.f4b6a3.ulid.creator
Class UlidBasedGuidCreator
- java.lang.Object
-
- com.github.f4b6a3.ulid.creator.UlidBasedGuidCreator
-
public class UlidBasedGuidCreator extends Object
Factory that creates lexicographically sortable GUIDs, based on the ULID specification - Universally Unique Lexicographically Sortable Identifier. ULID specification: https://github.com/ulid/spec
-
-
Field Summary
Fields Modifier and Type Field Description protected static longHALF_RANDOM_COMPONENTprotected static longINCREMENT_MAXprotected static StringOVERRUN_MESSAGEprotected longpreviousTimestampprotected Randomrandomprotected longrandomLsbprotected longrandomLsbMaxprotected longrandomMsbprotected longrandomMsbMaxprotected TimestampStrategytimestampStrategy
-
Constructor Summary
Constructors Constructor Description UlidBasedGuidCreator()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description UUIDcreate()Return a GUID based on the ULID specification.StringcreateString()Returns a ULID string.protected longextractRandomLsb(UUID uuid)For unit testsprotected longextractRandomMsb(UUID uuid)For unit testsprotected longgetTimestamp()Return the current timestamp and resets or increments the random part.protected voidincrement()Increment the random part of the GUID.protected voidreset()Reset the random part of the GUID.protected longtruncate(long value)Truncate long to half random component.<T extends UlidBasedGuidCreator>
TwithFastRandomGenerator()Replaces the default random generator with a faster one.<T extends UlidBasedGuidCreator>
TwithRandomGenerator(Random random)Replace the default random generator, in a fluent way, to another that extendsRandom.<T extends UlidBasedGuidCreator>
TwithTimestampStrategy(TimestampStrategy timestampStrategy)Used for changing the timestamp strategy.
-
-
-
Field Detail
-
randomMsb
protected long randomMsb
-
randomLsb
protected long randomLsb
-
randomLsbMax
protected long randomLsbMax
-
randomMsbMax
protected long randomMsbMax
-
HALF_RANDOM_COMPONENT
protected static final long HALF_RANDOM_COMPONENT
- See Also:
- Constant Field Values
-
INCREMENT_MAX
protected static final long INCREMENT_MAX
- See Also:
- Constant Field Values
-
previousTimestamp
protected long previousTimestamp
-
random
protected Random random
-
OVERRUN_MESSAGE
protected static final String OVERRUN_MESSAGE
- See Also:
- Constant Field Values
-
timestampStrategy
protected TimestampStrategy timestampStrategy
-
-
Method Detail
-
create
public UUID create()
Return a GUID based on the ULID specification. A ULID has two parts: 1. A part of 48 bits that represent the amount of milliseconds since Unix Epoch, 1 January 1970. 2. A part of 80 bits that has a random value generated a secure random generator. The random part is reset to a new value every time the millisecond part changes. If more than one GUID is generated within the same millisecond, the random part is incremented by one. The maximum GUIDs that can be generated per millisecond is 2^80. The random part is generated by a secure random number generator:SecureRandom. ### Specification of Universally Unique Lexicographically Sortable ID #### Components ##### Timestamp It is a 48 bit integer. UNIX-time in milliseconds. Won't run out of space 'til the year 10889 AD. ##### Randomness It is a 80 bits integer. Cryptographically secure source of randomness, if possible. #### Sorting The left-most character must be sorted first, and the right-most character sorted last (lexical order). The default ASCII character set must be used. Within the same millisecond, sort order is not guaranteed. #### Monotonicity When generating a ULID within the same millisecond, we can provide some guarantees regarding sort order. Namely, if the same millisecond is detected, the random component is incremented by 1 bit in the least significant bit position (with carrying). If, in the extremely unlikely event that, you manage to generate more than 2^80 ULIDs within the same millisecond, or cause the random component to overflow with less, the generation will fail.- Returns:
UUIDa GUID value- Throws:
UlidCreatorException- an overrun exception if too many requests are made within the same millisecond.
-
createString
public String createString()
Returns a ULID string. The returning string is encoded to Crockford's base32.- Returns:
- a ULID string
-
getTimestamp
protected long getTimestamp()
Return the current timestamp and resets or increments the random part.- Returns:
- timestamp
-
reset
protected void reset()
Reset the random part of the GUID.
-
increment
protected void increment()
Increment the random part of the GUID. An exception is thrown when more than 2^80 increment operations are made, although it's extremely unlikely to occur.- Throws:
UlidCreatorException- if an overrun happens.
-
withTimestampStrategy
public <T extends UlidBasedGuidCreator> T withTimestampStrategy(TimestampStrategy timestampStrategy)
Used for changing the timestamp strategy.- Parameters:
timestampStrategy- a timestamp strategy- Returns:
UlidBasedGuidCreator
-
withRandomGenerator
public <T extends UlidBasedGuidCreator> T withRandomGenerator(Random random)
Replace the default random generator, in a fluent way, to another that extendsRandom. The default random generator isSecureRandom. For other faster pseudo-random generators, seeXorshiftRandomand its variations. SeeRandom.- Parameters:
random- a random generator- Returns:
UlidBasedGuidCreator
-
withFastRandomGenerator
public <T extends UlidBasedGuidCreator> T withFastRandomGenerator()
Replaces the default random generator with a faster one. The host fingerprint is used to generate a seed for the random number generator. SeeXorshift128PlusRandomandFingerprintUtil.getFingerprint()- Returns:
UlidBasedGuidCreator
-
truncate
protected long truncate(long value)
Truncate long to half random component.- Parameters:
value- a value to be truncated.- Returns:
- truncated value
-
extractRandomLsb
protected long extractRandomLsb(UUID uuid)
For unit tests
-
extractRandomMsb
protected long extractRandomMsb(UUID uuid)
For unit tests
-
-