Interface IdGeneratorProvider

All Known Implementing Classes:
DefaultIdGeneratorProvider

@ThreadSafe public interface IdGeneratorProvider
Container for managing named StringIdGenerator instances.

A provider holds both IdGenerators (long ids) and generators of string ids only, such as CosIdGenerator. Use get(String, Class) or getRequired(String, Class) to look one up by the type you need, for example getRequired(name, IdGenerator.class) to call generate(). The shared generator is always an IdGenerator.

This interface provides a registry for ID generators that can be accessed by name throughout an application. It supports both named generators and a special shared generator that can be used as a default.

The provider acts as a centralized repository for ID generators, enabling:

  • Dependency injection of named generators
  • Centralized configuration and management
  • Easy switching between different generator implementations
  • Sharing of a common generator across multiple components

Common use cases include:

  • Managing different ID generators for different entity types
  • Providing a default generator for components that don't specify one
  • Enabling runtime configuration of ID generation strategies

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

  • Field Details

    • SHARE

      static final String SHARE
      The key used for the shared (default) ID generator.

      This constant defines the name used to access the shared ID generator, which serves as a default generator for components that don't require a specific named generator.

      See Also:
  • Method Details

    • getShare

      IdGenerator getShare()
      Get the shared (default) ID generator.

      This method returns the shared ID generator that can be used as a default when no specific named generator is required.

      Returns:
      The shared ID generator
    • setShare

      void setShare(IdGenerator idGenerator)
      Set the shared (default) ID generator.

      This method updates the shared ID generator that will be returned by getShare().

      Parameters:
      idGenerator - The ID generator to set as shared
    • removeShare

      IdGenerator removeShare()
      Remove the shared (default) ID generator.

      This method removes the current shared ID generator and returns it, leaving no shared generator configured.

      Returns:
      The previous shared ID generator, or null if none was set
    • get

      Get an ID generator by its name.

      This method returns an optional containing the ID generator with the specified name, or an empty optional if no generator with that name exists.

      Parameters:
      name - The name of the ID generator to retrieve
      Returns:
      An optional containing the ID generator, or empty if not found
    • get

      default <T extends StringIdGenerator> Optional<T> get(String name, Class<T> type)
      Get an ID generator by its name and type.
      Type Parameters:
      T - The generator type
      Parameters:
      name - The name of the ID generator to retrieve
      type - The type the generator must have, such as IdGenerator.class
      Returns:
      An optional containing the ID generator, or empty if not found
      Throws:
      IllegalArgumentException - if the generator with that name is not of the given type
    • getRequired

      default StringIdGenerator getRequired(String name)
      Get an ID generator by its name, throwing an exception if not found.

      This method returns the ID generator with the specified name, or throws an IllegalArgumentException if no generator with that name exists.

      Parameters:
      name - The name of the ID generator to retrieve
      Returns:
      The ID generator with the specified name
      Throws:
      IllegalArgumentException - if no generator with the specified name exists
    • getRequired

      default <T extends StringIdGenerator> T getRequired(String name, Class<T> type)
      Get an ID generator by its name and type, throwing an exception if not found.
      Type Parameters:
      T - The generator type
      Parameters:
      name - The name of the ID generator to retrieve
      type - The type the generator must have, such as IdGenerator.class
      Returns:
      The ID generator with the specified name
      Throws:
      IllegalArgumentException - if no generator with the specified name exists, or it is not of the given type
    • set

      void set(String name, StringIdGenerator idGenerator)
      Set an ID generator with the specified name.

      This method registers an ID generator with the specified name, replacing any existing generator with the same name.

      Parameters:
      name - The name to register the generator under
      idGenerator - The ID generator to register
      Throws:
      IllegalArgumentException - if the name is SHARE and the generator is not an IdGenerator
    • remove

      StringIdGenerator remove(String name)
      Remove an ID generator by its name.

      This method removes the ID generator with the specified name and returns the removed generator, or null if no generator with that name existed.

      Parameters:
      name - The name of the ID generator to remove
      Returns:
      The removed ID generator, or null if not found
    • clear

      void clear()
      Clear all registered ID generators.

      This method removes all registered ID generators, including the shared generator, leaving the provider empty.

    • entries

      Get all registered ID generator entries.

      This method returns a set of map entries representing all registered ID generators, including their names and instances.

      Returns:
      A set of entries for all registered ID generators
    • getAll

      Get all registered ID generators.

      This method returns a collection of all registered ID generator instances, without their associated names.

      Returns:
      A collection of all registered ID generators