Package com.ultikits.ultitools.utils
Class Ulid
java.lang.Object
com.ultikits.ultitools.utils.Ulid
ULID generation and validation for UltiCloud protocol v2 envelope ids (D-11, D-21): 26 Crockford base32
characters (
0123456789ABCDEFGHJKMNPQRSTVWXYZ), a 48-bit millisecond timestamp followed by 80 random bits,
as in the ULID specification (https://github.com/ulid/spec).
Monotonic per generator. Within one millisecond -- or when the clock steps backwards -- a generator keeps the last timestamp and increments the 80-bit random part by one, so ids from one generator are strictly increasing in both time order and string order. If the random part would overflow, the generator waits for the clock to pass the last timestamp and draws fresh randomness.
Thread safety. next() draws from one shared generator whose nextValue() is
synchronized: envelope ids are drawn on the main thread (outbox enqueue) and on the socket and scheduler
threads (outgoing envelopes), and every id must be unique across all of them.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final char[]private final LongSupplierprivate static final int[]private static final longprivate longThe timestamp of the last id, or-1before the first.(package private) static final intThe length of a ULID string.private static final longprivate final Randomprivate longThe top 16 of the 80 random bits of the last id.private longThe low 64 of the 80 random bits of the last id.private static final Ulidprivate static final int -
Constructor Summary
ConstructorsConstructorDescriptionUlid(LongSupplier clock, Random random) A generator with its own clock and randomness; tests use this to freeze time. -
Method Summary
Modifier and TypeMethodDescriptionprivate static longcheckedTime(long time) private voidprivate static Stringencode(long time, long high, long low) private boolean(package private) static booleanWhethervalueis a ULID: 26 Crockford base32 characters, case-insensitive (the specification's decoding rule), withoutI,L,OorU, and within the 128-bit range (the first character is at most7).(package private) static Stringnext()(package private) String(package private) static longtimestampOf(String value)
-
Field Details
-
LENGTH
static final int LENGTHThe length of a ULID string.- See Also:
-
ALPHABET
private static final char[] ALPHABET -
TIME_CHARS
private static final int TIME_CHARS- See Also:
-
MAX_TIME
private static final long MAX_TIME- See Also:
-
HIGH_MASK
private static final long HIGH_MASK- See Also:
-
DECODE
private static final int[] DECODE -
SHARED
-
clock
-
random
-
lastTime
private long lastTimeThe timestamp of the last id, or-1before the first. Guarded bythis. -
randomHigh
private long randomHighThe top 16 of the 80 random bits of the last id. Guarded bythis. -
randomLow
private long randomLowThe low 64 of the 80 random bits of the last id. Guarded bythis.
-
-
Constructor Details
-
Ulid
Ulid(LongSupplier clock, Random random) A generator with its own clock and randomness; tests use this to freeze time. Production usesnext().- Parameters:
clock- milliseconds since the Unix epochrandom- the source of the 80 random bits
-
-
Method Details
-
next
- Returns:
- a new ULID from the shared, synchronised generator
-
nextValue
String nextValue()- Returns:
- the next ULID of this generator, strictly greater than every earlier one it produced
- Throws:
IllegalStateException- if the clock is outside the 48-bit ULID time range
-
checkedTime
private static long checkedTime(long time) -
drawRandom
private void drawRandom() -
incrementRandom
private boolean incrementRandom()- Returns:
falsewhen the 80-bit value wrapped to zero
-
encode
-
isValid
Whethervalueis a ULID: 26 Crockford base32 characters, case-insensitive (the specification's decoding rule), withoutI,L,OorU, and within the 128-bit range (the first character is at most7).- Parameters:
value- the candidate, may benull- Returns:
trueif it is a ULID
-
timestampOf
- Parameters:
value- a ULID- Returns:
- its timestamp in milliseconds since the Unix epoch
- Throws:
IllegalArgumentException- ifvalueis not a ULID
-