Package com.github.f4b6a3.ulid.creator
Class UlidSpecCreator
- java.lang.Object
-
- com.github.f4b6a3.ulid.creator.UlidSpecCreator
-
public class UlidSpecCreator 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 longrandom1protected longrandom2protected longrandomMax1protected longrandomMax2protected RandomStrategyrandomStrategyprotected TimestampStrategytimestampStrategy
-
Constructor Summary
Constructors Constructor Description UlidSpecCreator()
-
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 longextractRandom1(UUID uuid)For unit testsprotected longextractRandom2(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.<T extends UlidSpecCreator>
TwithRandomGenerator(Random random)Replaces the default random strategy with another that uses the inputRandominstance.<T extends UlidSpecCreator>
TwithRandomStrategy(RandomStrategy randomStrategy)Replaces the default random strategy with another.<T extends UlidSpecCreator>
TwithTimestampStrategy(TimestampStrategy timestampStrategy)Used for changing the timestamp strategy.
-
-
-
Field Detail
-
random1
protected long random1
-
random2
protected long random2
-
randomMax1
protected long randomMax1
-
randomMax2
protected long randomMax2
-
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
-
OVERRUN_MESSAGE
protected static final String OVERRUN_MESSAGE
- See Also:
- Constant Field Values
-
timestampStrategy
protected TimestampStrategy timestampStrategy
-
randomStrategy
protected RandomStrategy randomStrategy
-
-
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
-
createString
public String createString()
Returns a ULID string. The returning string is encoded to Crockford's base32. The random component is generated by a secure random number generator:SecureRandom.- 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.
-
withTimestampStrategy
public <T extends UlidSpecCreator> T withTimestampStrategy(TimestampStrategy timestampStrategy)
Used for changing the timestamp strategy.- Parameters:
timestampStrategy- a timestamp strategy- Returns:
UlidSpecCreator
-
withRandomStrategy
public <T extends UlidSpecCreator> T withRandomStrategy(RandomStrategy randomStrategy)
Replaces the default random strategy with another. The default random strategy usesSecureRandom. SeeRandom.- Type Parameters:
T- the type parameter- Parameters:
random- a random generator- Returns:
AbstractRandomBasedUuidCreator
-
withRandomGenerator
public <T extends UlidSpecCreator> T withRandomGenerator(Random random)
Replaces the default random strategy with another that uses the inputRandominstance. It replaces the internalDefaultRandomStrategywithOtherRandomStrategy.- Parameters:
random- a random generator- Returns:
UlidSpecCreator
-
extractRandom1
protected long extractRandom1(UUID uuid)
For unit tests
-
extractRandom2
protected long extractRandom2(UUID uuid)
For unit tests
-
-