Package me.ahoo.cosid

Interface IdGenerator

All Superinterfaces:
Statistical, StringIdGenerator
All Known Subinterfaces:
CosIdGenerator, IdGeneratorDecorator, SegmentId, SnowflakeFriendlyId, SnowflakeId
All Known Implementing Classes:
AbstractSnowflakeId, AtomicLongGenerator, ClockSyncCosIdGenerator, ClockSyncSnowflakeId, DefaultSegmentId, DefaultSnowflakeFriendlyId, FriendlyCosIdGenerator, LazyIdGenerator, MillisecondSnowflakeId, MockIdGenerator, Radix36CosIdGenerator, Radix62CosIdGenerator, RadixCosIdGenerator, SecondSnowflakeId, SegmentChainId, StringIdGeneratorDecorator, StringSegmentId, StringSnowflakeId, UncertaintyIdGenerator, UuidGenerator

@ThreadSafe public interface IdGenerator extends StringIdGenerator, Statistical
Id Generator.

This is the core interface for generating distributed IDs in the CosId library. It provides two primary methods for ID generation:

The interface extends StringIdGenerator to provide string ID generation capabilities and Statistical to provide statistical information about the generator's state.

Implementations of this interface are expected to be thread-safe and can be used concurrently across multiple threads.

  • Method Summary

    Modifier and Type
    Method
    Description
    long
    Generate a distributed ID as a long value.
    default String
    Generate a distributed ID as a string value.
    default IdConverter
    Get the ID converter used to convert long type IDs to String.
    Get statistical information about this ID generator.
  • Method Details

    • idConverter

      @Nonnull default IdConverter idConverter()
      Get the ID converter used to convert long type IDs to String.

      By default, this returns Radix62IdConverter.PAD_START which converts long IDs to radix-62 string representations with padding to ensure consistent string lengths.

      Returns:
      ID converter for transforming numeric IDs to string format
    • generate

      long generate()
      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).

      Returns:
      A unique distributed ID as a long value
    • generateAsString

      @Nonnull default String generateAsString()
      Generate a distributed ID as a string value.

      This method generates a unique string identifier by first generating a numeric ID via generate() and then converting it to a string using the configured idConverter().

      Specified by:
      generateAsString in interface StringIdGenerator
      Returns:
      A unique distributed ID as a string value
    • stat

      default IdGeneratorStat stat()
      Get statistical information about this ID generator.

      This method provides insights into the generator's current state, including implementation details and converter statistics. This is useful for monitoring and debugging purposes.

      Specified by:
      stat in interface Statistical
      Returns:
      Statistical information about this ID generator