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:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final long
    The UNIX time that represents Discord's epoch (January 1, 2015).
  • Method Summary

    Modifier and Type
    Method
    Description
    Gets the unsigned ID of this Snowflake as a BigInteger.
    long
    Gets the unsigned ID of this Snowflake as a primitive long.
    static long
    asLong(discord4j.discordjson.Id id)
    Constructs a Snowflake represented as a long utilizing a discord-json Id.
    static long
    Constructs a Snowflake represented as a long utilizing an unsigned ID.
    Gets the unsigned ID of this Snowflake as an object String.
    static String
    asString(long id)
    Constructs a Snowflake represented as a String utilizing an unsigned ID.
    int
    Compares this snowflake to the specified snowflake.
    boolean
    Indicates whether some other object is "equal to" this Snowflake.
    Gets the timestamp of this Snowflake.
    int
    Gets the hash code value of the ID.
    static Snowflake
    of(long id)
    Constructs a Snowflake utilizing an unsigned ID.
    static Snowflake
    of(discord4j.discordjson.Id id)
    Constructs a Snowflake utilizing a discord-json Id.
    static Snowflake
    of(String id)
    Constructs a Snowflake utilizing an unsigned ID.
    static Snowflake
    Constructs a Snowflake utilizing a BigInteger representing an unsigned ID.
    static Snowflake
    of(Instant timestamp)
    Constructs a Snowflake utilizing a timestamp.
    Gets the String representation of this Snowflake.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • DISCORD_EPOCH

      public static final long DISCORD_EPOCH
      The UNIX time that represents Discord's epoch (January 1, 2015).
      See Also:
  • Method Details

    • 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: