Class GeneratorRegistry

java.lang.Object
mocka.generator.factory.GeneratorRegistry
Direct Known Subclasses:
GeneratorFactory

public class GeneratorRegistry extends Object
The GeneratorRegistry is central static registry that manages all Generator instances within the mocka-core library.

It provides static methods to register, retrieve, and manage generators identified by unique keys. The registry is backed by a thread-safe ConcurrentHashMap, ensuring safe concurrent access.

By default, several common generators (e.g. EmailGenerator, NameGenerator, CountryGenerator) are registered in the static initializer.

  • Field Details

  • Constructor Details

    • GeneratorRegistry

      public GeneratorRegistry()
  • Method Details

    • putGenerator

      public static <T> void putGenerator(T generator)
      Registers the given Generator into the Registry.

      if Generator implements RegistrableGenerator, the Generator put as RegistrableGenerator. if Generator implements AbstractGenerator, the Generator put as Generator extends AbstractGenerator.

      Type Parameters:
      T - the type of value to be generated
      Parameters:
      generator - the generator to register
      Throws:
      UnsupportedOperationException - if the generator type is not supported.
    • putGenerator

      public static <T> void putGenerator(String key, String path, Class<T> type)
      Creates RegistrableGenerator by key, path, and type parameters and registers the Generator.
      Type Parameters:
      T - the type parameter of the generator
      Parameters:
      key - unique identifier of the generator
      path - file path used as the source for the generator
      type - the type of data the generator produces
      Throws:
      GeneratorException - if a generator with the same key already exists
    • getGenerator

      public static <T> Generator<T> getGenerator(String key)
      Retrieves a registered Generator by its unique key.
      Type Parameters:
      T - the type parameter of the generator
      Parameters:
      key - unique identifier of the generator
      Returns:
      the Generator registered under the given key
      Throws:
      GeneratorException - if no generator is found for the given key
    • getGenerator

      public static <T> Generator<T> getGenerator(String key, String path, Class<T> type)
      Retrieves a RegistrableGenerator for the given key.

      If a generator with the specified key does not exist, a new one is created with the provided path and type, registered, and then returned.

      Type Parameters:
      T - the type parameter of the generator
      Parameters:
      key - unique identifier of the generator
      path - file path used as the source for the generator
      type - the type of data the generator produces
      Returns:
      the existing or newly created Generator
    • clearAllRegistrableGenerator

      public static void clearAllRegistrableGenerator()
      Removes all registered RegistrableGenerators from the registry. Built-in generators (e.g. EmailGenerator, NameGenerator) are not affected and remain registered.

      After calling this method, any custom RegistrableGenerator must be registered again before use.

    • clearRegistrableGenerator

      public static void clearRegistrableGenerator(String key)
    • getGeneratorNames

      public static List<String> getGeneratorNames()
      Returns the list of all keys (names) of registered generators.
      Returns:
      a list of generator keys currently registered
    • existsRegistrableGenerator

      public static Boolean existsRegistrableGenerator(String key)
      Checks whether a generator with the given key exists in the registry.
      Parameters:
      key - the unique identifier of the generator
      Returns:
      true if a generator is registered under the given key, false otherwise