Package discord4j.common.util
Class Snowflake
- java.lang.Object
-
- discord4j.common.util.Snowflake
-
- All Implemented Interfaces:
Comparable<Snowflake>
public final class Snowflake extends Object implements 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:
- Snowflake IDs
-
-
Field Summary
Fields Modifier and Type Field Description static long
DISCORD_EPOCH
The UNIX time that represents Discord's epoch (January 1, 2015).
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description BigInteger
asBigInteger()
Gets 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
asLong(String id)
Constructs aSnowflake
represented as along
utilizing an unsigned ID.String
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
compareTo(Snowflake other)
Compares this snowflake to the specified snowflake.boolean
equals(Object obj)
Indicates whether some other object is "equal to" thisSnowflake
.Instant
getTimestamp()
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
of(String id)
Constructs aSnowflake
utilizing an unsigned ID.static Snowflake
of(BigInteger id)
Constructs aSnowflake
utilizing a BigInteger representing an unsigned ID.static Snowflake
of(Instant timestamp)
Constructs aSnowflake
utilizing a timestamp.String
toString()
Gets the String representation of thisSnowflake
.
-
-
-
Field Detail
-
DISCORD_EPOCH
public static final long DISCORD_EPOCH
The UNIX time that represents Discord's epoch (January 1, 2015).- See Also:
- Constant Field Values
-
-
Method Detail
-
of
public static Snowflake of(long id)
Constructs aSnowflake
utilizing an unsigned ID.- Parameters:
id
- The unsigned ID to construct aSnowflake
.- Returns:
- A constructed
Snowflake
with the unsigned ID.
-
of
public static Snowflake of(String id)
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
public static Snowflake of(Instant timestamp)
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
public static Snowflake of(BigInteger id)
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
public static Snowflake of(discord4j.discordjson.Id id)
Constructs aSnowflake
utilizing a discord-jsonId
.- Parameters:
id
- The ID to construct aSnowflake
- Returns:
- A constructed
Snowflake
with the given ID
-
asLong
public static long asLong(String id)
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
public static String asString(long id)
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
public String asString()
Gets the unsigned ID of thisSnowflake
as an object String.- Returns:
- The unsigned ID of this
Snowflake
as an object String.
-
getTimestamp
public Instant getTimestamp()
Gets the timestamp of thisSnowflake
.- Returns:
- The timestamp of this
Snowflake
.
-
asBigInteger
public BigInteger asBigInteger()
Gets the unsigned ID of thisSnowflake
as a BigInteger.- Returns:
- The unsigned ID of this
Snowflake
as a BigInteger.
-
compareTo
public int compareTo(Snowflake other)
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
public boolean equals(@Nullable Object obj)
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
public String 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.- Overrides:
toString
in classObject
- Returns:
- The String representation of this
Snowflake
. - See Also:
asString()
-
-