Class LeasedSnowflakeId

java.lang.Object
me.ahoo.cosid.snowflake.LeasedSnowflakeId
All Implemented Interfaces:
Decorator<IdGenerator>, IdGenerator, IdGeneratorDecorator, SnowflakeId, Statistical, StringIdGenerator

public class LeasedSnowflakeId extends Object implements IdGeneratorDecorator, SnowflakeId
SnowflakeId that refuses to generate ids once the lease of its machine id is lost or expired.

Fail fast: a duplicate id is worse than a failed request. See MachineIdLease.

  • Constructor Details

  • Method Details

    • getActual

      public @NonNull SnowflakeId getActual()
      Description copied from interface: IdGeneratorDecorator
      Get the actual (wrapped) ID generator that this decorator is enhancing.

      This method returns the underlying ID generator that this decorator is wrapping. All ID generation requests are typically delegated to this actual generator, with the decorator adding its additional functionality.

      Specified by:
      getActual in interface Decorator<IdGenerator>
      Specified by:
      getActual in interface IdGeneratorDecorator
      Returns:
      The actual ID generator being decorated
    • getLease

      public MachineIdLease getLease()
    • generate

      public long generate() throws MachineIdLostException
      Description copied from interface: IdGeneratorDecorator
      Generate a distributed ID by delegating to the actual generator.

      This default implementation delegates the ID generation to the actual wrapped generator, ensuring that decorators can focus on adding functionality rather than reimplementing basic generation.

      Specified by:
      generate in interface IdGenerator
      Specified by:
      generate in interface IdGeneratorDecorator
      Returns:
      A unique distributed ID as a long value
      Throws:
      MachineIdLostException
    • stat

      public IdGeneratorStat stat()
      Description copied from interface: IdGeneratorDecorator
      Get statistical information about this decorated ID generator.

      This method provides combined statistical information from both the decorator itself and the actual generator it wraps, as well as the ID converter being used. This gives a complete picture of the generator's state.

      Specified by:
      stat in interface IdGenerator
      Specified by:
      stat in interface IdGeneratorDecorator
      Specified by:
      stat in interface SnowflakeId
      Specified by:
      stat in interface Statistical
      Specified by:
      stat in interface StringIdGenerator
      Returns:
      Statistical information about this decorated ID generator
    • layout

      public SnowflakeLayout layout()
      Description copied from interface: SnowflakeId
      The bit layout of the ids.
      Specified by:
      layout in interface SnowflakeId
      Returns:
      the layout
    • 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
    • getLastTimestampAsMilliseconds

      public long getLastTimestampAsMilliseconds()
      Description copied from interface: SnowflakeId
      Get the last generated timestamp in milliseconds for clock synchronization.
      Specified by:
      getLastTimestampAsMilliseconds in interface SnowflakeId
      Returns:
      The last generated timestamp in milliseconds
    • 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