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
FieldsModifier and TypeFieldDescriptionstatic final longThe UNIX time that represents Discord's epoch (January 1, 2015). -
Method Summary
Modifier and TypeMethodDescriptionGets the unsigned ID of thisSnowflakeas a BigInteger.longasLong()Gets the unsigned ID of thisSnowflakeas a primitive long.static longasLong(discord4j.discordjson.Id id) Constructs aSnowflakerepresented as alongutilizing a discord-jsonId.static longConstructs aSnowflakerepresented as alongutilizing an unsigned ID.asString()Gets the unsigned ID of thisSnowflakeas an object String.static StringasString(long id) Constructs aSnowflakerepresented as aStringutilizing an unsigned ID.intCompares this snowflake to the specified snowflake.booleanIndicates whether some other object is "equal to" thisSnowflake.Gets the timestamp of thisSnowflake.inthashCode()Gets the hash code value of theID.static Snowflakeof(long id) Constructs aSnowflakeutilizing an unsigned ID.static Snowflakeof(discord4j.discordjson.Id id) Constructs aSnowflakeutilizing a discord-jsonId.static SnowflakeConstructs aSnowflakeutilizing an unsigned ID.static Snowflakeof(BigInteger id) Constructs aSnowflakeutilizing a BigInteger representing an unsigned ID.static SnowflakeConstructs aSnowflakeutilizing 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 aSnowflakeutilizing an unsigned ID.- Parameters:
id- The unsigned ID to construct aSnowflake.- Returns:
- A constructed
Snowflakewith the unsigned ID.
-
of
Constructs aSnowflakeutilizing an unsigned ID.- Parameters:
id- The unsigned ID to construct aSnowflake. Must be non-null.- Returns:
- A constructed
Snowflakewith the unsigned ID. - Throws:
NumberFormatException- Ifidis not an unsigned ID.
-
of
Constructs aSnowflakeutilizing a timestamp. The constructedSnowflakeis 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
Snowflakewith the timestamp.
-
of
Constructs aSnowflakeutilizing a BigInteger representing an unsigned ID.- Parameters:
id- The BigInteger representing an unsigned ID to construct aSnowflake. Must be non-null.- Returns:
- A constructed
Snowflakewith an unsigned ID.
-
of
Constructs aSnowflakeutilizing a discord-jsonId.- Parameters:
id- The ID to construct aSnowflake- Returns:
- A constructed
Snowflakewith the given ID
-
asLong
Constructs aSnowflakerepresented as alongutilizing an unsigned ID.- Parameters:
id- The unsigned ID to construct aSnowflake. Must be non-null.- Returns:
- A constructed
Snowflakewith the unsigned ID. - Throws:
NumberFormatException- Ifidis not an unsigned ID.
-
asLong
public static long asLong(discord4j.discordjson.Id id) Constructs aSnowflakerepresented as alongutilizing a discord-jsonId.- Parameters:
id- The ID to construct aSnowflake. Must be non-null.- Returns:
- A constructed
Snowflakewith the ID.
-
asString
Constructs aSnowflakerepresented as aStringutilizing an unsigned ID.- Parameters:
id- The unsigned ID to construct aSnowflake. Must be non-null.- Returns:
- A constructed
Snowflakewith the unsigned ID. - Throws:
NumberFormatException- Ifidis not an unsigned ID.
-
asLong
public long asLong()Gets the unsigned ID of thisSnowflakeas a primitive long.- Returns:
- The unsigned ID of this
Snowflakeas a primitive long.
-
asString
Gets the unsigned ID of thisSnowflakeas an object String.- Returns:
- The unsigned ID of this
Snowflakeas an object String.
-
getTimestamp
Gets the timestamp of thisSnowflake.- Returns:
- The timestamp of this
Snowflake.
-
asBigInteger
Gets the unsigned ID of thisSnowflakeas a BigInteger.- Returns:
- The unsigned ID of this
Snowflakeas a BigInteger.
-
compareTo
Compares this snowflake to the specified snowflake.The comparison is based on the timestamp portion of the snowflakes.
- Specified by:
compareToin 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
Snowflakeand; - 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.
-