- java.lang.Object
-
- com.github.f4b6a3.ulid.Ulid
-
- All Implemented Interfaces:
Serializable,Comparable<Ulid>
public final class Ulid extends Object implements Serializable, Comparable<Ulid>
A class that represents ULIDs.ULID is a 128-bit value that has two components:
- Time component: a number of milliseconds since 1970-01-01 (Unix epoch).
- Random component: a sequence of 80 random bits generated by a secure random generator.
ULID has 128-bit compatibility with
UUID. Like a UUID, a ULID can also be stored as a 16-byte array.Instances of this class are immutable.
- See Also:
- ULID Specification, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static UlidMAXA special ULID that has all 128 bits set to ONE.static UlidMINA special ULID that has all 128 bits set to ZERO.static intRANDOM_BYTESNumber of bytes of the random component of a ULID.static intRANDOM_CHARSNumber of characters of the random component of a ULID.static intTIME_BYTESNumber of bytes of the time component of a ULID.static intTIME_CHARSNumber of characters of the time component of a ULID.static intULID_BYTESNumber of bytes of a ULID.static intULID_CHARSNumber of characters of a ULID.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intcompareTo(Ulid that)Compares two ULIDs as unsigned 128-bit integers.booleanequals(Object other)Checks if some other ULID is equal to this one.static Ulidfast()Returns a fast new ULID.static Ulidfrom(byte[] bytes)Converts a byte array into a ULID.static Ulidfrom(String string)Converts a canonical string into a ULID.static Ulidfrom(UUID uuid)Converts a UUID into a ULID.InstantgetInstant()Returns the instant of creation.static InstantgetInstant(String string)Returns the instant of creation.longgetLeastSignificantBits()Returns the least significant bits as a number.longgetMostSignificantBits()Returns the most significant bits as a number.byte[]getRandom()Returns the random component as a byte array.static byte[]getRandom(String string)Returns the random component as a byte array.longgetTime()Returns the time component as a number.static longgetTime(String string)Returns the time component as a number.inthashCode()Returns a hash code value for the ULID.Ulidincrement()Returns a new ULID by incrementing the random component of the current ULID.static booleanisValid(String string)Checks if the input string is valid.static Ulidmax(long time)Returns the maximum ULID for a given time.static Ulidmin(long time)Returns the minimum ULID for a given time.byte[]toBytes()Convert the ULID into a byte array.StringtoLowerCase()Converts the ULID into a canonical string in lower case.UlidtoRfc4122()Converts the ULID into into another ULID that is compatible with UUIDv4.StringtoString()Converts the ULID into a canonical string in upper case.UUIDtoUuid()Convert the ULID into a UUID.
-
-
-
Field Detail
-
ULID_CHARS
public static final int ULID_CHARS
Number of characters of a ULID.- See Also:
- Constant Field Values
-
TIME_CHARS
public static final int TIME_CHARS
Number of characters of the time component of a ULID.- See Also:
- Constant Field Values
-
RANDOM_CHARS
public static final int RANDOM_CHARS
Number of characters of the random component of a ULID.- See Also:
- Constant Field Values
-
ULID_BYTES
public static final int ULID_BYTES
Number of bytes of a ULID.- See Also:
- Constant Field Values
-
TIME_BYTES
public static final int TIME_BYTES
Number of bytes of the time component of a ULID.- See Also:
- Constant Field Values
-
RANDOM_BYTES
public static final int RANDOM_BYTES
Number of bytes of the random component of a ULID.- See Also:
- Constant Field Values
-
MIN
public static final Ulid MIN
A special ULID that has all 128 bits set to ZERO.
-
MAX
public static final Ulid MAX
A special ULID that has all 128 bits set to ONE.
-
-
Constructor Detail
-
Ulid
public Ulid(Ulid ulid)
Creates a new ULID.Useful to make copies of ULIDs.
- Parameters:
ulid- a ULID
-
Ulid
public Ulid(long mostSignificantBits, long leastSignificantBits)Creates a new ULID.If you want to make a copy of a
UUID, usefrom(UUID)instead.- Parameters:
mostSignificantBits- the first 8 bytes as a long valueleastSignificantBits- the last 8 bytes as a long value
-
Ulid
public Ulid(long time, byte[] random)Creates a new ULID.Time parameter is the number of milliseconds since 1970-01-01 (Unix epoch). It must be a positive number not larger than 2^48-1.
Random parameter must be an array of 10 bytes.
- Parameters:
time- the the number of milliseconds since 1970-01-01random- an array of 10 bytes- Throws:
IllegalArgumentException- if time is negative or larger than 2^48-1IllegalArgumentException- if random is null or its length is not 10
-
-
Method Detail
-
fast
public static Ulid fast()
Returns a fast new ULID.This static method is a quick alternative to
UlidCreator.getUlid().It employs
ThreadLocalRandomwhich works very well, although not cryptographically strong. It can be useful, for example, for logging.Security-sensitive applications that require a cryptographically secure pseudo-random generator should use
UlidCreator.getUlid().- Returns:
- a ULID
- Since:
- 5.1.0
-
min
public static Ulid min(long time)
Returns the minimum ULID for a given time.The 48 bits of the time component are filled with the given time and the 80 bits of the random component are all set to ZERO.
For example, the minimum ULID for 2022-02-22 22:22:22.222 is `
new Ulid(0x017f2387460e0000L, 0x0000000000000000L)`, where `0x017f2387460e` is the timestamp in hexadecimal.It can be useful to find all records before or after a specific timestamp in a table without a `
created_at` field.- Parameters:
time- the the number of milliseconds since 1970-01-01- Returns:
- a ULID
- Since:
- 5.2.0
-
max
public static Ulid max(long time)
Returns the maximum ULID for a given time.The 48 bits of the time component are filled with the given time and the 80 bits or the random component are all set to ONE.
For example, the maximum ULID for 2022-02-22 22:22:22.222 is `
new Ulid(0x017f2387460effffL, 0xffffffffffffffffL)`, where `0x017f2387460e` is the timestamp in hexadecimal.It can be useful to find all records before or after a specific timestamp in a table without a `
created_at` field.- Parameters:
time- the the number of milliseconds since 1970-01-01- Returns:
- a ULID
- Since:
- 5.2.0
-
from
public static Ulid from(UUID uuid)
Converts a UUID into a ULID.- Parameters:
uuid- a UUID- Returns:
- a ULID
-
from
public static Ulid from(byte[] bytes)
Converts a byte array into a ULID.- Parameters:
bytes- an array of 16 bytes- Returns:
- a ULID
- Throws:
IllegalArgumentException- if bytes are null or its length is not 16
-
from
public static Ulid from(String string)
Converts a canonical string into a ULID.The input string must be 26 characters long and must contain only characters from Crockford's base 32 alphabet.
The first character of the input string must be between 0 and 7.
- Parameters:
string- a canonical string- Returns:
- a ULID
- Throws:
IllegalArgumentException- if the input string is invalid- See Also:
- Crockford's Base 32
-
toUuid
public UUID toUuid()
Convert the ULID into a UUID.A ULID has 128-bit compatibility with a
UUID.If you need a RFC-4122 UUIDv4 do this:
Ulid.toRfc4122().toUuid().- Returns:
- a UUID.
-
toBytes
public byte[] toBytes()
Convert the ULID into a byte array.- Returns:
- an byte array.
-
toString
public String toString()
Converts the ULID into a canonical string in upper case.The output string is 26 characters long and contains only characters from Crockford's Base 32 alphabet.
For lower case string, use the shorthand
Ulid#toLowerCase(), instead ofUlid#toString()#toLowerCase().- Overrides:
toStringin classObject- Returns:
- a ULID string
- See Also:
- Crockford's Base 32
-
toLowerCase
public String toLowerCase()
Converts the ULID into a canonical string in lower case.The output string is 26 characters long and contains only characters from Crockford's Base 32 alphabet.
It is a shorthand at least twice as fast as
Ulid.toString().toLowerCase().- Returns:
- a string
- See Also:
- Crockford's Base 32
-
toRfc4122
public Ulid toRfc4122()
Converts the ULID into into another ULID that is compatible with UUIDv4.The bytes of the returned ULID are compliant with the RFC-4122 version 4.
If you need a RFC-4122 UUIDv4 do this:
Ulid.toRfc4122().toUuid().Note: If you use this method, you can not get the original ULID, since it changes 6 bits of it to generate a UUIDv4.
- Returns:
- a ULID
- See Also:
- RFC-4122
-
getInstant
public Instant getInstant()
Returns the instant of creation.The instant of creation is extracted from the time component.
- Returns:
- the
Instantof creation
-
getInstant
public static Instant getInstant(String string)
Returns the instant of creation.The instant of creation is extracted from the time component.
- Parameters:
string- a canonical string- Returns:
- the
Instantof creation - Throws:
IllegalArgumentException- if the input string is invalid
-
getTime
public long getTime()
Returns the time component as a number.The time component is a number between 0 and 2^48-1. It is equivalent to the count of milliseconds since 1970-01-01 (Unix epoch).
- Returns:
- a number of milliseconds
-
getTime
public static long getTime(String string)
Returns the time component as a number.The time component is a number between 0 and 2^48-1. It is equivalent to the count of milliseconds since 1970-01-01 (Unix epoch).
- Parameters:
string- a canonical string- Returns:
- a number of milliseconds
- Throws:
IllegalArgumentException- if the input string is invalid
-
getRandom
public byte[] getRandom()
Returns the random component as a byte array.The random component is an array of 10 bytes (80 bits).
- Returns:
- a byte array
-
getRandom
public static byte[] getRandom(String string)
Returns the random component as a byte array.The random component is an array of 10 bytes (80 bits).
- Parameters:
string- a canonical string- Returns:
- a byte array
- Throws:
IllegalArgumentException- if the input string is invalid
-
getMostSignificantBits
public long getMostSignificantBits()
Returns the most significant bits as a number.- Returns:
- a number.
-
getLeastSignificantBits
public long getLeastSignificantBits()
Returns the least significant bits as a number.- Returns:
- a number.
-
increment
public Ulid increment()
Returns a new ULID by incrementing the random component of the current ULID.Since the random component contains 80 bits:
- (1) This method can generate up to 1208925819614629174706176 (2^80) ULIDs per millisecond;
- (2) This method can generate monotonic increasing ULIDs 99.999999999999992% ((2^80 - 10^9) / (2^80)) of the time within a single millisecond interval, considering an unrealistic rate of 1,000,000,000 ULIDs per millisecond.
Due to (1) and (2), it does not throw the error message recommended by the specification. When an overflow occurs in the random 80 bits, the time component is simply incremented to maintain monotonicity.
- Returns:
- a ULID
-
isValid
public static boolean isValid(String string)
Checks if the input string is valid.The input string must be 26 characters long and must contain only characters from Crockford's base 32 alphabet.
The first character of the input string must be between 0 and 7.
- Parameters:
string- a canonical string- Returns:
- true if the input string is valid
- See Also:
- Crockford's Base 32
-
hashCode
public int hashCode()
Returns a hash code value for the ULID.
-
equals
public boolean equals(Object other)
Checks if some other ULID is equal to this one.
-
compareTo
public int compareTo(Ulid that)
Compares two ULIDs as unsigned 128-bit integers.The first of two ULIDs is greater than the second if the most significant byte in which they differ is greater for the first ULID.
- Specified by:
compareToin interfaceComparable<Ulid>- Parameters:
that- a ULID to be compared with- Returns:
- -1, 0 or 1 as
thisis less than, equal to, or greater thanthat
-
-