Class LazyIdGenerator

java.lang.Object
me.ahoo.cosid.provider.LazyIdGenerator
All Implemented Interfaces:
Decorator<IdGenerator>, IdGenerator, IdGeneratorDecorator, Statistical, StringIdGenerator

public final class LazyIdGenerator extends Object implements IdGeneratorDecorator
Lazy loading IdGenerator.

Delays the lookup of an ID generator from the provider until first access. This is useful when the generator might not be immediately available at startup.

  • Constructor Details

    • LazyIdGenerator

      public LazyIdGenerator(String generatorName)
      Creates a lazy generator with default provider.
      Parameters:
      generatorName - the name of the generator to lookup
    • LazyIdGenerator

      public LazyIdGenerator(String generatorName, IdGeneratorProvider idGeneratorProvider)
      Creates a lazy generator with custom provider.
      Parameters:
      generatorName - the name of the generator to lookup
      idGeneratorProvider - the provider to use for lookup
  • Method Details

    • getGeneratorName

      public String getGeneratorName()
      Gets the generator name.
      Returns:
      the generator name
    • tryGet

      public IdGenerator tryGet(boolean required)
      Attempts to get the generator, optionally throwing if not found.
      Parameters:
      required - if true, throws NotFoundIdGeneratorException if not found
      Returns:
      the generator or null if not required and not found
      Throws:
      NotFoundIdGeneratorException - if required and not found
    • asSnowflakeId

      public SnowflakeId asSnowflakeId(boolean required)
      Gets this generator as a SnowflakeId.
      Parameters:
      required - if true, throws if not a SnowflakeId
      Returns:
      the SnowflakeId or null
      Throws:
      CosIdException - if not a SnowflakeId when required
    • asFriendlyId

      public SnowflakeFriendlyId asFriendlyId(boolean required)
      Gets this generator as a SnowflakeFriendlyId.
      Parameters:
      required - if true, throws if not a SnowflakeFriendlyId
      Returns:
      the SnowflakeFriendlyId or null
      Throws:
      CosIdException - if not a SnowflakeFriendlyId when required
    • asSegmentId

      public SegmentId asSegmentId(boolean required)
      Gets this generator as a SegmentId.
      Parameters:
      required - if true, throws if not a SegmentId
      Returns:
      the SegmentId or null
      Throws:
      CosIdException - if not a SegmentId when required
    • getActual

      public @NonNull IdGenerator 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
    • idConverter

      public @NonNull IdConverter idConverter()
      Description copied from interface: IdGenerator
      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.

      Specified by:
      idConverter in interface IdGenerator
      Returns:
      ID converter for transforming numeric IDs to string format