Class AbstractSnowflakeId

java.lang.Object
me.ahoo.cosid.snowflake.AbstractSnowflakeId
All Implemented Interfaces:
IdGenerator, SnowflakeId, Statistical, StringIdGenerator
Direct Known Subclasses:
MillisecondSnowflakeId, SecondSnowflakeId

public abstract class AbstractSnowflakeId extends Object implements SnowflakeId
Abstract SnowflakeId.
  • Field Details

    • epoch

      protected final long epoch
    • timestampBit

      protected final int timestampBit
    • machineBit

      protected final int machineBit
    • sequenceBit

      protected final int sequenceBit
    • maxTimestamp

      protected final long maxTimestamp
    • maxSequence

      protected final long maxSequence
    • maxMachineId

      protected final int maxMachineId
    • machineLeft

      protected final long machineLeft
    • timestampLeft

      protected final long timestampLeft
    • machineId

      protected final long machineId
      WARN:machineLeft greater than 30 will cause overflow, so machineId should be long when calculating.
    • sequence

      protected long sequence
    • lastTimestamp

      protected long lastTimestamp
  • Constructor Details

    • AbstractSnowflakeId

      public AbstractSnowflakeId(long epoch, int timestampBit, int machineBit, int sequenceBit, int machineId, long sequenceResetThreshold)
  • Method Details

    • nextTime

      protected long nextTime()
    • getCurrentTime

      protected abstract long getCurrentTime()
      get current timestamp.
      Returns:
      current timestamp
    • generate

      public long generate()
      Description copied from interface: IdGenerator
      Generate a distributed ID as a long value.

      This method generates a unique numeric identifier that is guaranteed to be unique within the distributed system. The exact algorithm used depends on the implementation (e.g., Snowflake, Segment, CosId).

      Specified by:
      generate in interface IdGenerator
      Returns:
      A unique distributed ID as a long value
    • getEpoch

      public long getEpoch()
      Description copied from interface: SnowflakeId
      Get the epoch timestamp used as the base for time calculations.

      This is the timestamp (in milliseconds) that serves as the starting point for the timestamp portion of generated IDs. IDs generated by this instance will have timestamps relative to this epoch.

      Specified by:
      getEpoch in interface SnowflakeId
      Returns:
      The epoch timestamp in milliseconds
    • getTimestampBit

      public int getTimestampBit()
      Description copied from interface: SnowflakeId
      Get the number of bits used for the timestamp portion.

      This determines the range of time that can be represented by the timestamp portion of the ID. More bits allow for a longer time range but leave fewer bits for machine ID and sequence.

      Specified by:
      getTimestampBit in interface SnowflakeId
      Returns:
      The number of timestamp bits
    • getMachineBit

      public int getMachineBit()
      Description copied from interface: SnowflakeId
      Get the number of bits used for the machine ID portion.

      This determines how many unique machines can participate in ID generation. More bits allow for more machines but leave fewer bits for timestamp and sequence.

      Specified by:
      getMachineBit in interface SnowflakeId
      Returns:
      The number of machine ID bits
    • getSequenceBit

      public int getSequenceBit()
      Description copied from interface: SnowflakeId
      Get the number of bits used for the sequence portion.

      This determines how many IDs can be generated per time unit (e.g., per millisecond). More bits allow for higher throughput within a single time unit but leave fewer bits for timestamp and machine ID.

      Specified by:
      getSequenceBit in interface SnowflakeId
      Returns:
      The number of sequence bits
    • getMaxTimestamp

      public long getMaxTimestamp()
      Description copied from interface: SnowflakeId
      Get the maximum timestamp value that can be represented.

      This is the maximum value that can be stored in the timestamp portion of the ID, which determines how long the generator can be used before exhausting the timestamp space.

      Specified by:
      getMaxTimestamp in interface SnowflakeId
      Returns:
      The maximum timestamp value
    • getMaxMachineId

      public int getMaxMachineId()
      Description copied from interface: SnowflakeId
      Get the maximum machine ID value that can be represented.

      This is the maximum value that can be stored in the machine ID portion of the ID, which determines how many unique machines can participate in ID generation.

      Specified by:
      getMaxMachineId in interface SnowflakeId
      Returns:
      The maximum machine ID value
    • getMaxSequence

      public long getMaxSequence()
      Description copied from interface: SnowflakeId
      Get the maximum sequence value that can be represented.

      This is the maximum value that can be stored in the sequence portion of the ID, which determines how many IDs can be generated per time unit.

      Specified by:
      getMaxSequence in interface SnowflakeId
      Returns:
      The maximum sequence value
    • getLastTimestamp

      public long getLastTimestamp()
      Description copied from interface: SnowflakeId
      Get the timestamp of the last ID that was generated.

      This is used for clock synchronization and to ensure proper ordering of generated IDs. It can also help detect clock drift.

      Specified by:
      getLastTimestamp in interface SnowflakeId
      Returns:
      The timestamp of the last generated ID
    • getMachineId

      public int getMachineId()
      Description copied from interface: SnowflakeId
      Get the machine ID assigned to this generator.

      This is the unique identifier for the machine or instance that is generating IDs, which ensures global uniqueness across the distributed system.

      Specified by:
      getMachineId in interface SnowflakeId
      Returns:
      The machine ID