Class Generators

java.lang.Object
dev.orne.test.rnd.Generators

@API(status=STABLE, since="0.1") public final class Generators extends Object
Main entry point for random value generation system. Allows generation of random values with dynamic generators addition.

Registers generators declared in /META-INF/services/dev.orne.test.rnd.Generator SPI files in the class path.

Further registered generator adjustment can be performed with getRegisteredGenerators(), getGenerator(Class), register(Generator...), register(Collection), remove(Generator...), remove(Collection) and reset().

Since:
0.1
Version:
1.1, 2023-11
Author:
(w) Iker Hernaez
  • Field Details

    • COMPARATOR

      public static final Comparator<Generator> COMPARATOR
      The generator comparator by priority.
  • Method Details

    • supports

      public static boolean supports(@NotNull @NotNull Class<?> type)
      Checks if values of the specified type can be generated by any registered generator.
      Parameters:
      type - The type to check.
      Returns:
      If values of the specified type can be generated.
    • defaultValue

      public static <T> T defaultValue(@NotNull @NotNull Class<T> type)
      Returns the default value for the specified type.
      Type Parameters:
      T - The requested value type.
      Parameters:
      type - The requested value type.
      Returns:
      The default value for the specified type.
      Throws:
      GeneratorNotFoundException - If no generator supports the requested value type.
    • defaultValue

      @API(status=EXPERIMENTAL, since="0.1") public static <T> T defaultValue(@NotNull @NotNull Class<T> type, @NotNull @NotNull Object... params)
      Returns the default value for the specified type.
      Type Parameters:
      T - The requested value type.
      Parameters:
      type - The requested value type.
      params - The generation parameter sources.
      Returns:
      The default value for the specified type.
      Throws:
      GeneratorNotFoundException - If no generator supports the requested value type.
      GeneratorNotParameterizableException - If the generator registered for the requested value type is not parameterizable.
    • nullableDefaultValue

      public static <T> T nullableDefaultValue(@NotNull @NotNull Class<T> type)
      Returns the default value for the specified type allowing null values.

      This method should return null except for native types.

      Type Parameters:
      T - The requested value type.
      Parameters:
      type - The requested value type.
      Returns:
      The nullable default value for the specified type.
      Throws:
      GeneratorNotFoundException - If no generator supports the requested value type.
    • nullableDefaultValue

      @API(status=EXPERIMENTAL, since="0.1") public static <T> T nullableDefaultValue(@NotNull @NotNull Class<T> type, @NotNull @NotNull Object... params)
      Returns the default value for the specified type allowing null values.

      This method should return null except for native types.

      Type Parameters:
      T - The requested value type.
      Parameters:
      type - The requested value type.
      params - The generation parameter sources.
      Returns:
      The nullable default value for the specified type.
      Throws:
      GeneratorNotFoundException - If no generator supports the requested value type.
      GeneratorNotParameterizableException - If the generator registered for the requested value type is not parameterizable.
    • randomValue

      public static <T> T randomValue(@NotNull @NotNull Class<T> type)
      Returns a random value of the specified type.
      Type Parameters:
      T - The requested value type.
      Parameters:
      type - The requested value type.
      Returns:
      A random value for the specified type.
      Throws:
      GeneratorNotFoundException - If no generator supports the requested value type.
    • randomValue

      @API(status=EXPERIMENTAL, since="0.1") public static <T> T randomValue(@NotNull @NotNull Class<T> type, @NotNull @NotNull Object... params)
      Returns a random value of the specified type.
      Type Parameters:
      T - The requested value type.
      Parameters:
      type - The requested value type.
      params - The generation parameter sources.
      Returns:
      A random value for the specified type.
      Throws:
      GeneratorNotFoundException - If no generator supports the requested value type.
      GeneratorNotParameterizableException - If the generator registered for the requested value type is not parameterizable.
    • nullableRandomValue

      public static <T> T nullableRandomValue(@NotNull @NotNull Class<T> type)
      Returns a random value of the specified type allowing null values.

      The returned value has a probability of be null except for native types. If not null behaves as randomValue().

      Type Parameters:
      T - The requested value type.
      Parameters:
      type - The requested value type.
      Returns:
      A random nullable value for the specified type.
      Throws:
      GeneratorNotFoundException - If no generator supports the requested value type.
      See Also:
    • nullableRandomValue

      @API(status=EXPERIMENTAL, since="0.1") public static <T> T nullableRandomValue(@NotNull @NotNull Class<T> type, @NotNull @NotNull Object... params)
      Returns a random value of the specified type allowing null values.

      The returned value has a probability of be null except for native types. If not null behaves as randomValue().

      Type Parameters:
      T - The requested value type.
      Parameters:
      type - The requested value type.
      params - The generation parameter sources.
      Returns:
      A random nullable value for the specified type.
      Throws:
      GeneratorNotFoundException - If no generator supports the requested value type.
      GeneratorNotParameterizableException - If the generator registered for the requested value type is not parameterizable.
      See Also:
    • getGenerator

      public static Generator getGenerator(@NotNull @NotNull Class<?> type)
      Returns the generator to use for the specified value type.
      Parameters:
      type - The value type to generate
      Returns:
      The generator to use. Returns null is no one is suitable.
    • getParameterizableGenerator

      public static ParameterizableGenerator getParameterizableGenerator(@NotNull @NotNull Class<?> type)
      Returns the parameterizable generator to use for the specified value type. If no generator supports the requested value type or the generator is not parameterizable returns null.
      Parameters:
      type - The value type to generate
      Returns:
      The generator to use. Returns null is no one is suitable.
    • forField

      @API(status=EXPERIMENTAL, since="0.2") @NotNull public static <T> @NotNull PropertyTypeGenerator<T> forField(@NotNull @NotNull Field field)
      Returns a targeted generator for the specified field.

      If a parameterizable generator has been registered for the specified type extracts the generation parameters from the field declaration.

      Type Parameters:
      T - The type of the generated values
      Parameters:
      field - The bean field
      Returns:
      A generator for the type of the specified field
      Since:
      0.2
    • forField

      @API(status=EXPERIMENTAL, since="0.2") @NotNull public static <T> @NotNull PropertyTypeGenerator<T> forField(@NotNull @NotNull Class<?> beanType, @NotNull @NotNull Field field)
      Returns a targeted generator for the specified field.

      If a parameterizable generator has been registered for the specified type extracts the generation parameters from the field declaration.

      Type Parameters:
      T - The type of the generated values
      Parameters:
      beanType - The bean type
      field - The bean field
      Returns:
      A generator for the type of the specified field
      Since:
      0.2
    • forProperty

      @API(status=EXPERIMENTAL, since="0.1") @NotNull public static <T> @NotNull PropertyTypeGenerator<T> forProperty(@NotNull @NotNull Class<?> beanType, @NotNull @NotNull String property)
      Returns a targeted generator for the specified bean property.

      If a parameterizable generator has been registered for the specified type extracts the generation parameters from the property declaration.

      Type Parameters:
      T - The type of the generated values
      Parameters:
      beanType - The bean type
      property - The property of the bean
      Returns:
      A generator for the type of the specified property
    • forParameter

      @API(status=EXPERIMENTAL, since="0.2") @NotNull public static <T> @NotNull ParameterTypeGenerator<T> forParameter(@NotNull @NotNull Parameter parameter)
      Returns a targeted generator for the specified parameter.

      If a parameterizable generator has been registered for the specified type extracts the generation parameters from the parameter declaration.

      Type Parameters:
      T - The type of the generated values
      Parameters:
      parameter - The parameter
      Returns:
      A generator for the type of the specified parameter
      Since:
      0.2
    • forParameter

      @API(status=EXPERIMENTAL, since="0.1") @NotNull public static <T> @NotNull MethodParameterTypeGenerator<T> forParameter(@NotNull @NotNull Method method, @NotNull @javax.validation.constraints.NotNull int parameterIndex)
      Returns a targeted generator for the specified method parameter.

      If a parameterizable generator has been registered for the specified type extracts the generation parameters from the method declaration.

      Type Parameters:
      T - The type of the generated values
      Parameters:
      method - The method
      parameterIndex - The parameter index
      Returns:
      A generator for the type of the specified method parameter
    • forParameter

      @API(status=EXPERIMENTAL, since="0.1") @NotNull public static <T> @NotNull MethodParameterTypeGenerator<T> forParameter(@NotNull @NotNull Class<?> cls, @NotNull @NotNull String method, @NotNull @NotNull Class<?>[] parameterTypes, @NotNull @javax.validation.constraints.NotNull int parameterIndex)
      Returns a targeted generator for the specified method parameter.

      If a parameterizable generator has been registered for the specified type extracts the generation parameters from the method declaration.

      Type Parameters:
      T - The type of the generated values
      Parameters:
      cls - The method's class
      method - The method name
      parameterTypes - The method parameter types
      parameterIndex - The parameter index
      Returns:
      A generator for the type of the specified method parameter
    • forReturnType

      @API(status=EXPERIMENTAL, since="0.1") @NotNull public static <T> @NotNull MethodReturnTypeGenerator<T> forReturnType(@NotNull @NotNull Method method)
      Returns a targeted generator for the specified method return type.

      If a parameterizable generator has been registered for the specified type extracts the generation parameters from the method declaration.

      Type Parameters:
      T - The type of the generated values
      Parameters:
      method - The method
      Returns:
      A generator for the type of the specified method return type
    • forReturnType

      @API(status=EXPERIMENTAL, since="0.1") @NotNull public static <T> @NotNull MethodReturnTypeGenerator<T> forReturnType(@NotNull @NotNull Class<?> cls, @NotNull @NotNull String method, @NotNull @NotNull Class<?>[] parameterTypes)
      Returns a targeted generator for the specified method return type.

      If a parameterizable generator has been registered for the specified type extracts the generation parameters from the method declaration.

      Type Parameters:
      T - The type of the generated values
      Parameters:
      cls - The method's class
      parameterTypes - The method parameter types
      method - The method name
      Returns:
      A generator for the type of the specified method return type
    • forParameter

      @API(status=EXPERIMENTAL, since="0.1") @NotNull public static <T> @NotNull ConstructorParameterTypeGenerator<T> forParameter(@NotNull @NotNull Constructor<?> constructor, @NotNull @javax.validation.constraints.NotNull int parameterIndex)
      Returns a targeted generator for the specified constructor parameter.

      If a parameterizable generator has been registered for the specified type extracts the generation parameters from the constructor declaration.

      Type Parameters:
      T - The type of the generated values
      Parameters:
      constructor - The constructor
      parameterIndex - The parameter index
      Returns:
      A generator for the type of the specified constructor parameter
    • forParameter

      @API(status=EXPERIMENTAL, since="0.1") @NotNull public static <T> @NotNull ConstructorParameterTypeGenerator<T> forParameter(@NotNull @NotNull Class<?> cls, @NotNull @NotNull Class<?>[] parameterTypes, @NotNull @javax.validation.constraints.NotNull int parameterIndex)
      Returns a targeted generator for the specified constructor parameter.

      If a parameterizable generator has been registered for the specified type extracts the generation parameters from the constructor declaration.

      Type Parameters:
      T - The type of the generated values
      Parameters:
      cls - The class of the constructor
      parameterTypes - The constructor parameter types
      parameterIndex - The parameter index
      Returns:
      A generator for the type of the specified constructor parameter
    • getRegisteredGenerators

      @NotNull public static @NotNull List<Generator> getRegisteredGenerators()
      Returns an unmodifiable list with the registered generators.
      Returns:
      The registered generators
    • register

      public static void register(@NotNull @NotNull Generator... generators)
      Adds the specified generators to the registered generators.
      Parameters:
      generators - The generators to register
    • register

      public static void register(@NotNull @NotNull Collection<Generator> generators)
      Adds the specified generators to the registered generators.
      Parameters:
      generators - The generators to register
    • remove

      public static void remove(@NotNull @NotNull Generator... generators)
      Removes the specified generators from the registered generators.
      Parameters:
      generators - The generators to remove
    • remove

      public static void remove(@NotNull @NotNull Collection<Generator> generators)
      Removes the specified generators from the registered generators.
      Parameters:
      generators - The generators to remove
    • reset

      public static void reset()
      Resets the loaded and cached generators. Next call will regenerate the generator list (including SPI generators) and restart generator caching.