Class 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).
    • 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 a Snowflake utilizing an unsigned ID.
        Parameters:
        id - The unsigned ID to construct a Snowflake.
        Returns:
        A constructed Snowflake with the unsigned ID.
      • of

        public static Snowflake of​(String id)
        Constructs a Snowflake utilizing an unsigned ID.
        Parameters:
        id - The unsigned ID to construct a Snowflake. Must be non-null.
        Returns:
        A constructed Snowflake with the unsigned ID.
        Throws:
        NumberFormatException - If id is not an unsigned ID.
      • of

        public static Snowflake of​(Instant timestamp)
        Constructs a Snowflake utilizing a timestamp. The constructed Snowflake is only guaranteed to contain accurate information about its timestamp; the other portions are undefined.
        Parameters:
        timestamp - The timestamp to construct a Snowflake. Must be non-null.
        Returns:
        A constructed Snowflake with the timestamp.
      • of

        public static Snowflake of​(BigInteger id)
        Constructs a Snowflake utilizing a BigInteger representing an unsigned ID.
        Parameters:
        id - The BigInteger representing an unsigned ID to construct a Snowflake. Must be non-null.
        Returns:
        A constructed Snowflake with an unsigned ID.
      • of

        public static Snowflake of​(discord4j.discordjson.Id id)
        Constructs a Snowflake utilizing a discord-json Id.
        Parameters:
        id - The ID to construct a Snowflake
        Returns:
        A constructed Snowflake with the given ID
      • asLong

        public static long asLong​(String id)
        Constructs a Snowflake represented as a long utilizing an unsigned ID.
        Parameters:
        id - The unsigned ID to construct a Snowflake. Must be non-null.
        Returns:
        A constructed Snowflake with the unsigned ID.
        Throws:
        NumberFormatException - If id is not an unsigned ID.
      • asLong

        public static long asLong​(discord4j.discordjson.Id id)
        Constructs a Snowflake represented as a long utilizing a discord-json Id.
        Parameters:
        id - The ID to construct a Snowflake. Must be non-null.
        Returns:
        A constructed Snowflake with the ID.
      • asString

        public static String asString​(long id)
        Constructs a Snowflake represented as a String utilizing an unsigned ID.
        Parameters:
        id - The unsigned ID to construct a Snowflake. Must be non-null.
        Returns:
        A constructed Snowflake with the unsigned ID.
        Throws:
        NumberFormatException - If id is not an unsigned ID.
      • asLong

        public long asLong()
        Gets the unsigned ID of this Snowflake as a primitive long.
        Returns:
        The unsigned ID of this Snowflake as a primitive long.
      • asString

        public String asString()
        Gets the unsigned ID of this Snowflake as an object String.
        Returns:
        The unsigned ID of this Snowflake as an object String.
      • getTimestamp

        public Instant getTimestamp()
        Gets the timestamp of this Snowflake.
        Returns:
        The timestamp of this Snowflake.
      • asBigInteger

        public BigInteger asBigInteger()
        Gets the unsigned ID of this Snowflake 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 interface Comparable<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" this Snowflake. The other object is considered equal if:
        • It is also a Snowflake and;
        • Both instances have equal IDs.
        Overrides:
        equals in class Object
        Parameters:
        obj - An object to be tested for equality.
        Returns:
        true if the other object is "equal to" this one, false otherwise.
      • hashCode

        public int hashCode()
        Gets the hash code value of the ID.
        Overrides:
        hashCode in class Object
        Returns:
        The hash code value of the ID.
      • toString

        public String toString()
        Gets the String representation of this Snowflake.

        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 class Object
        Returns:
        The String representation of this Snowflake.
        See Also:
        asString()