Class Ksuid
- All Implemented Interfaces:
Serializable,Comparable<Ksuid>
KSUID is a 160-bit value that has two parts:
- Time: a sequence of 32 bits that represent the creation minute.
- Payload: a sequence of 128 random bits generated by a secure random generator.
Instances of this class are immutable.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longNumber of seconds of 2014-05-13T16:53:20Z (14e8).static final intNumber of bytes of a KSUID.static final intNumber of characters of a KSUID.static final intNumber of bytes of the payload of a KSUID.static final intNumber of bytes of the time of a KSUID. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintCompares two KSUIDs as unsigned 160-bit integers.booleanChecks if some other KSUID is equal to this one.static Ksuidfast()Returns a fast new KSUID.static Ksuidfrom(byte[] bytes) Convert a byte array into a KSUID.static KsuidConvert a canonical string into a KSUID.Return the instant of creation.static InstantgetInstant(String string) Return the instant of creation.byte[]Return the payload.static byte[]getPayload(String string) Return the payload.longgetTime()Return the creation time.static longReturn the creation time.inthashCode()Returns a hash code value for the KSUID.Returns a new KSUID by incrementing the payload of the current KSUID.static booleanCheck if the input string is valid.byte[]toBytes()Convert the KSUID into a byte array.toString()Convert the KSUID into a canonical string.
-
Field Details
-
KSUID_CHARS
public static final int KSUID_CHARSNumber of characters of a KSUID.- See Also:
-
KSUID_BYTES
public static final int KSUID_BYTESNumber of bytes of a KSUID.- See Also:
-
TIME_BYTES
public static final int TIME_BYTESNumber of bytes of the time of a KSUID.- See Also:
-
PAYLOAD_BYTES
public static final int PAYLOAD_BYTESNumber of bytes of the payload of a KSUID.- See Also:
-
EPOCH_OFFSET
public static final long EPOCH_OFFSETNumber of seconds of 2014-05-13T16:53:20Z (14e8).- See Also:
-
-
Constructor Details
-
Ksuid
Creates a new KSUID.Useful to make copies of KSUIDs.
- Parameters:
ksuid- a KSUID
-
Ksuid
public Ksuid(long seconds, byte[] payload) Creates a new KSUID.Seconds parameter is the number of seconds since 1970-01-01 (Unix epoch). It is internally converted to KSUID time.
Payload parameter must be an array of 16 bytes.
- Parameters:
seconds- the number of seconds since 1970-01-01payload- an array of 16 bytes- Throws:
IllegalArgumentException- if payload is null or its length is not 16
-
-
Method Details
-
fast
Returns a fast new KSUID.This static method is a quick alternative to
KsuidCreator.getKsuid().It employs
SplittableRandomwhich 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
KsuidCreator.getKsuid().- Returns:
- a KSUID
- Since:
- 4.1.0
-
from
Convert a byte array into a KSUID.- Parameters:
bytes- an array of 20 bytes- Returns:
- a KSUID
- Throws:
IllegalArgumentException- if bytes are null or its length is not 20
-
from
Convert a canonical string into a KSUID.The input string must be 27 characters long and must contain only characters from base-62 alphabet.
- Parameters:
string- a canonical string- Returns:
- a KSUID
- Throws:
IllegalArgumentException- if the input string is invalid- See Also:
-
toBytes
public byte[] toBytes()Convert the KSUID into a byte array.- Returns:
- a byte array.
-
toString
-
getInstant
-
getInstant
Return the instant of creation.The instant of creation is extracted from the creation time.
- Parameters:
string- a canonical string- Returns:
- the
Instantof creation - Throws:
IllegalArgumentException- if the input string is invalid
-
getTime
public long getTime()Return the creation time.The number returned is equivalent to the number of seconds since 1970-01-01 (Unix epoch).
- Returns:
- a number of seconds
-
getTime
Return the creation time.The number returned is equivalent to the number of seconds since 1970-01-01 (Unix epoch).
- Parameters:
string- a canonical string- Returns:
- a number of seconds
- Throws:
IllegalArgumentException- if the input string is invalid
-
getPayload
public byte[] getPayload()Return the payload.The payload is an array of 16 bytes (128 bits).
- Returns:
- a byte array
-
getPayload
Return the payload.The payload is an array of 16 bytes (128 bits).
- Parameters:
string- a canonical string- Returns:
- a byte array
-
isValid
Check if the input string is valid.The input string must be 27 characters long and must contain only characters from base-62 alphabet.
- Parameters:
string- a canonical string- Returns:
- true if valid
- See Also:
-
hashCode
-
equals
-
compareTo
Compares two KSUIDs as unsigned 160-bit integers.The first of two KSUID is greater than the second if the most significant byte in which they differ is greater for the first KSUID.
- Specified by:
compareToin interfaceComparable<Ksuid>- Parameters:
that- a KSUID to be compared with- Returns:
- -1, 0 or 1 as
thisis less than, equal to, or greater thanthat
-
increment
Returns a new KSUID by incrementing the payload of the current KSUID. When an overflow occurs in the payload 128 bits, the time is simply incremented to maintain monotonicity.- Returns:
- a KSUID
-