Package discord4j.common.util
Class Snowflake
java.lang.Object
discord4j.common.util.Snowflake
- All Implemented Interfaces:
Comparable<Snowflake>
An unsigned 64-bit ID that is guaranteed to be unique across all of Discord, except in some unique scenarios
in which child objects share their parent's ID.
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final long
The UNIX time that represents Discord's epoch (January 1, 2015). -
Method Summary
Modifier and TypeMethodDescriptionGets the unsigned ID of thisSnowflake
as a BigInteger.long
asLong()
Gets the unsigned ID of thisSnowflake
as a primitive long.static long
asLong
(discord4j.discordjson.Id id) Constructs aSnowflake
represented as along
utilizing a discord-jsonId
.static long
Constructs aSnowflake
represented as along
utilizing an unsigned ID.asString()
Gets the unsigned ID of thisSnowflake
as an object String.static String
asString
(long id) Constructs aSnowflake
represented as aString
utilizing an unsigned ID.int
Compares this snowflake to the specified snowflake.boolean
Indicates whether some other object is "equal to" thisSnowflake
.Gets the timestamp of thisSnowflake
.int
hashCode()
Gets the hash code value of theID
.static Snowflake
of
(long id) Constructs aSnowflake
utilizing an unsigned ID.static Snowflake
of
(discord4j.discordjson.Id id) Constructs aSnowflake
utilizing a discord-jsonId
.static Snowflake
Constructs aSnowflake
utilizing an unsigned ID.static Snowflake
of
(BigInteger id) Constructs aSnowflake
utilizing a BigInteger representing an unsigned ID.static Snowflake
Constructs aSnowflake
utilizing a timestamp.toString()
Gets the String representation of thisSnowflake
.
-
Field Details
-
DISCORD_EPOCH
public static final long DISCORD_EPOCHThe UNIX time that represents Discord's epoch (January 1, 2015).- See Also:
-
-
Method Details
-
of
Constructs aSnowflake
utilizing an unsigned ID.- Parameters:
id
- The unsigned ID to construct aSnowflake
.- Returns:
- A constructed
Snowflake
with the unsigned ID.
-
of
Constructs aSnowflake
utilizing an unsigned ID.- Parameters:
id
- The unsigned ID to construct aSnowflake
. Must be non-null.- Returns:
- A constructed
Snowflake
with the unsigned ID. - Throws:
NumberFormatException
- Ifid
is not an unsigned ID.
-
of
Constructs aSnowflake
utilizing a timestamp. The constructedSnowflake
is only guaranteed to contain accurate information about itstimestamp
; the other portions are undefined.- Parameters:
timestamp
- The timestamp to construct aSnowflake
. Must be non-null.- Returns:
- A constructed
Snowflake
with the timestamp.
-
of
Constructs aSnowflake
utilizing a BigInteger representing an unsigned ID.- Parameters:
id
- The BigInteger representing an unsigned ID to construct aSnowflake
. Must be non-null.- Returns:
- A constructed
Snowflake
with an unsigned ID.
-
of
Constructs aSnowflake
utilizing a discord-jsonId
.- Parameters:
id
- The ID to construct aSnowflake
- Returns:
- A constructed
Snowflake
with the given ID
-
asLong
Constructs aSnowflake
represented as along
utilizing an unsigned ID.- Parameters:
id
- The unsigned ID to construct aSnowflake
. Must be non-null.- Returns:
- A constructed
Snowflake
with the unsigned ID. - Throws:
NumberFormatException
- Ifid
is not an unsigned ID.
-
asLong
public static long asLong(discord4j.discordjson.Id id) Constructs aSnowflake
represented as along
utilizing a discord-jsonId
.- Parameters:
id
- The ID to construct aSnowflake
. Must be non-null.- Returns:
- A constructed
Snowflake
with the ID.
-
asString
Constructs aSnowflake
represented as aString
utilizing an unsigned ID.- Parameters:
id
- The unsigned ID to construct aSnowflake
. Must be non-null.- Returns:
- A constructed
Snowflake
with the unsigned ID. - Throws:
NumberFormatException
- Ifid
is not an unsigned ID.
-
asLong
public long asLong()Gets the unsigned ID of thisSnowflake
as a primitive long.- Returns:
- The unsigned ID of this
Snowflake
as a primitive long.
-
asString
Gets the unsigned ID of thisSnowflake
as an object String.- Returns:
- The unsigned ID of this
Snowflake
as an object String.
-
getTimestamp
Gets the timestamp of thisSnowflake
.- Returns:
- The timestamp of this
Snowflake
.
-
asBigInteger
Gets the unsigned ID of thisSnowflake
as a BigInteger.- Returns:
- The unsigned ID of this
Snowflake
as a BigInteger.
-
compareTo
Compares this snowflake to the specified snowflake.The comparison is based on the timestamp portion of the snowflakes.
- Specified by:
compareTo
in interfaceComparable<Snowflake>
- Parameters:
other
- The other snowflake to compare to.- Returns:
- The comparator value.
-
equals
Indicates whether some other object is "equal to" thisSnowflake
. The other object is considered equal if:- It is also a
Snowflake
and; - Both instances have equal
IDs
.
- It is also a
-
hashCode
public int hashCode()Gets the hash code value of theID
. -
toString
Gets the String representation of thisSnowflake
.The format returned by this method is unspecified and may vary between implementations; however, it is guaranteed to always be non-empty. This method is not suitable for obtaining the ID; use
asString()
instead.
-