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 longDISCORD_EPOCHThe 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 BigIntegerasBigInteger()Gets 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 longasLong(String id)Constructs aSnowflakerepresented as alongutilizing an unsigned ID.StringasString()Gets the unsigned ID of thisSnowflakeas an object String.static StringasString(long id)Constructs aSnowflakerepresented as aStringutilizing an unsigned ID.intcompareTo(Snowflake other)Compares this snowflake to the specified snowflake.booleanequals(Object obj)Indicates whether some other object is "equal to" thisSnowflake.InstantgetTimestamp()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 Snowflakeof(String id)Constructs aSnowflakeutilizing an unsigned ID.static Snowflakeof(BigInteger id)Constructs aSnowflakeutilizing a BigInteger representing an unsigned ID.static Snowflakeof(Instant timestamp)Constructs aSnowflakeutilizing a timestamp.StringtoString()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 aSnowflakeutilizing an unsigned ID.- Parameters:
id- The unsigned ID to construct aSnowflake.- Returns:
- A constructed
Snowflakewith the unsigned ID.
-
of
public static Snowflake of(String id)
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
public static Snowflake of(Instant timestamp)
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
public static Snowflake of(BigInteger id)
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
public static Snowflake of(discord4j.discordjson.Id id)
Constructs aSnowflakeutilizing a discord-jsonId.- Parameters:
id- The ID to construct aSnowflake- Returns:
- A constructed
Snowflakewith the given ID
-
asLong
public static long asLong(String id)
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
public static String asString(long id)
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
public String asString()
Gets the unsigned ID of thisSnowflakeas an object String.- Returns:
- The unsigned ID of this
Snowflakeas 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 thisSnowflakeas a BigInteger.- Returns:
- The unsigned ID of this
Snowflakeas 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:
compareToin 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
Snowflakeand; - 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:
toStringin classObject- Returns:
- The String representation of this
Snowflake. - See Also:
asString()
-
-