Class SnowflakeGenerator

java.lang.Object
tfagaming.projects.minecraft.homestead.snowflake.SnowflakeGenerator

public class SnowflakeGenerator extends Object
Snowflake ID Generator for Homestead models.
  • Constructor Details

    • SnowflakeGenerator

      public SnowflakeGenerator(long workerId)
      Creates a generator with a specific worker ID.
      Parameters:
      workerId - Unique ID for this machine/worker, ranging from 0 to 1023
      Throws:
      IllegalArgumentException - if workerId is out of range
    • SnowflakeGenerator

      public SnowflakeGenerator()
      Creates a generator with a random worker ID.
  • Method Details

    • extractTimestamp

      public static long extractTimestamp(long snowflakeId)
      Extracts the timestamp (as epoch millis) from a Snowflake ID.
    • extractWorkerId

      public static long extractWorkerId(long snowflakeId)
      Extracts the worker ID from a Snowflake ID.
    • extractSequence

      public static long extractSequence(long snowflakeId)
      Extracts the sequence number from a Snowflake ID.
    • toInstant

      public static Instant toInstant(long snowflakeId)
      Converts a Snowflake ID to an Instant.
    • nextId

      public long nextId()
      Generates the next unique Snowflake ID.
      Returns:
      A unique 64-bit positive long ID
      Throws:
      IllegalStateException - if the clock moves backwards
    • nextIdString

      public String nextIdString()
      Generates the next unique Snowflake ID.
      Returns:
      A unique and unsigned 64-bit positive long ID as string
      Throws:
      IllegalStateException - if the clock moves backwards
    • getWorkerId

      public long getWorkerId()
      Returns the worker ID of this generator instance.