public final class Ulid extends Object implements Serializable, Comparable<Ulid>
ULID is 128-bit value that has two components:
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.
| 限定符和类型 | 字段和说明 |
|---|---|
static int |
RANDOM_BYTES
Number of bytes of the random component of a ULID.
|
static int |
RANDOM_CHARS
Number of characters of the random component of a ULID.
|
static int |
TIME_BYTES
Number of bytes of the time component of a ULID.
|
static int |
TIME_CHARS
Number of characters of the time component of a ULID.
|
static int |
ULID_BYTES
Number of bytes of a ULID.
|
static int |
ULID_CHARS
Number of characters of a ULID.
|
| 构造器和说明 |
|---|
Ulid(long time,
byte[] random)
Creates a new ULID.
|
Ulid(long mostSignificantBits,
long leastSignificantBits)
Creates a new ULID.
|
Ulid(Ulid ulid)
Creates a new ULID.
|
| 限定符和类型 | 方法和说明 |
|---|---|
int |
compareTo(Ulid that)
Compares two ULIDs as unsigned 128-bit integers.
|
boolean |
equals(Object other)
Checks if some other ULID is equal to this one.
|
static Ulid |
from(byte[] bytes)
Converts a byte array into a ULID.
|
static Ulid |
from(String string)
Converts a canonical string into a ULID.
|
static Ulid |
from(UUID uuid)
Converts a UUID into a ULID.
|
java.time.Instant |
getInstant()
Returns the instant of creation.
|
static java.time.Instant |
getInstant(String string)
Returns the instant of creation.
|
long |
getLeastSignificantBits()
Returns the least significant bits as a number.
|
long |
getMostSignificantBits()
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.
|
long |
getTime()
Returns the time component as a number.
|
static long |
getTime(String string)
Returns the time component as a number.
|
int |
hashCode()
Returns a hash code value for the ULID.
|
Ulid |
increment()
Returns a new ULID by incrementing the random component of the current ULID.
|
static boolean |
isValid(String string)
Checks if the input string is valid.
|
byte[] |
toBytes()
Convert the ULID into a byte array.
|
String |
toLowerCase()
Converts the ULID into a canonical string in lower case.
|
Ulid |
toRfc4122()
Converts the ULID into into another ULID that is compatible with UUIDv4.
|
String |
toString()
Converts the ULID into a canonical string in upper case.
|
UUID |
toUuid()
Convert the ULID into a UUID.
|
public static final int ULID_CHARS
public static final int TIME_CHARS
public static final int RANDOM_CHARS
public static final int ULID_BYTES
public static final int TIME_BYTES
public static final int RANDOM_BYTES
public Ulid(Ulid ulid)
Useful to make copies of ULIDs.
ulid - a ULIDpublic Ulid(long mostSignificantBits,
long leastSignificantBits)
If you want to make a copy of a UUID, use from(UUID)
instead.
mostSignificantBits - the first 8 bytes as a long valueleastSignificantBits - the last 8 bytes as a long valuepublic Ulid(long time,
byte[] random)
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.
time - the the number of milliseconds since 1970-01-01random - an array of 10 bytesIllegalArgumentException - if time is negative or larger than 2^48-1IllegalArgumentException - if random is null or its length is not 10public static Ulid from(byte[] bytes)
bytes - an array of 16 bytesIllegalArgumentException - if bytes are null or its length is not 16public static Ulid from(String string)
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.
string - a canonical stringIllegalArgumentException - if the input string is invalidpublic static java.time.Instant getInstant(String string)
The instant of creation is extracted from the time component.
string - a canonical stringInstant of creationIllegalArgumentException - if the input string is invalidpublic static long getTime(String string)
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).
string - a canonical stringIllegalArgumentException - if the input string is invalidpublic static byte[] getRandom(String string)
The random component is an array of 10 bytes (80 bits).
string - a canonical stringIllegalArgumentException - if the input string is invalidpublic static boolean isValid(String string)
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.
string - a canonical stringpublic UUID toUuid()
A ULID has 128-bit compatibility with a UUID.
If you need a RFC-4122 UUIDv4 do this: Ulid.toRfc4122().toUuid().
public byte[] toBytes()
public String toString()
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
of Ulid#toString()#toLowerCase().
toString 在类中 Objectpublic String toLowerCase()
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().
public Ulid toRfc4122()
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.
public java.time.Instant getInstant()
The instant of creation is extracted from the time component.
Instant of creationpublic long getTime()
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).
public byte[] getRandom()
The random component is an array of 10 bytes (80 bits).
public long getMostSignificantBits()
public long getLeastSignificantBits()
public Ulid increment()
Since the random component contains 80 bits:
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.
public boolean equals(Object other)
public int compareTo(Ulid that)
The first of two ULIDs is greater than the second if the most significant byte in which they differ is greater for the first UUID.
compareTo 在接口中 Comparable<Ulid>that - a ULID to be compared withthis is less than, equal to, or greater than
thatCopyright © 2024. All rights reserved.