Class IdWorker

java.lang.Object
org.seppiko.snowflake.IdWorker

public final class IdWorker extends Object
Seppiko Snowflake algorithm implement.
 +----------------------------------------------------------------------------------------------------------------------+
 | UNUSED(1BIT) |           TIMESTAMP(41BIT)           |  DATACENTER-ID(5BIT)  |  WORKER-ID(5BIT)  |  SERIAL-NO(12BIT)  |
 +----------------------------------------------------------------------------------------------------------------------+
 
Author:
Leonard Woo
See Also:
  • Method Details

    • getInstance

      public static IdWorker getInstance(long offsetTimestamp)
      Singleton with synchronized.
      Parameters:
      offsetTimestamp - Initialize offset timestamp (millisecond).
      Returns:
      Synchronized instance.
    • initWorker

      public void initWorker(long datacenterId, long workerId)
      Initialization worker.
      Parameters:
      datacenterId - See setDatacenterId(long).
      workerId - See setWorkerId(long).
    • setDatacenterId

      public void setDatacenterId(long datacenterId)
      Set data center id.
      Parameters:
      datacenterId - Data center number the process running on, value must is between 0 and 31.
    • setWorkerId

      public void setWorkerId(long workerId)
      Set worker id.
      Parameters:
      workerId - Machine or process number, value must is between 0 and 31.
    • getConfig

      public IdConfig getConfig()
      Get snowflake id config.
      Returns:
      Snowflake ID config instance.
    • nextId

      public long nextId()
      Generate an unique and incrementing ID.
      Returns:
      Snowflake ID.
    • getWaitCount

      public long getWaitCount()
      Returns:
      The amount of calling waitNextMillis(long) method.
    • formatId

      public String formatId(long id)
      Formatter snowflake id.
      Parameters:
      id - Snowflake id.
      Returns:
      ID formatting message. 'ISO8601 format, #sequence id, @(datacenter id, worker id)'
    • toString

      public String toString()
      Snowflake ID config string.
      Overrides:
      toString in class Object
      Returns:
      Config string.