Class GeneratorDecorator<T>

java.lang.Object
org.instancio.internal.generator.misc.GeneratorDecorator<T>
Type Parameters:
T - generated type
All Implemented Interfaces:
Generator<T>, GeneratorSpec<T>
Direct Known Subclasses:
SupplierAdapter

@InternalApi public abstract class GeneratorDecorator<T> extends Object implements Generator<T>
Decorator for generators that might be missing hints.
Since:
2.14.0
  • Constructor Details

    • GeneratorDecorator

      protected GeneratorDecorator(Generator<T> delegate)
  • Method Details

    • init

      public final void init(GeneratorContext context)
      Description copied from interface: Generator
      An optional method for generators that need to initialise state before generating values.

      Note: this method is guaranteed to be called before Generator.generate(Random), however, there is no guarantee as to the number of times it will be invoked. It is possible for init() to be called multiple times for a given generator instance.

      Specified by:
      init in interface Generator<T>
      Parameters:
      context - generator context
    • generate

      public final T generate(Random random)
      Description copied from interface: Generator
      Returns a generated value.

      If this method produces random data, the data needs to be generated using the provided Random instance. This ensures generated values are reproducible for a given seed value.

      Specified by:
      generate in interface Generator<T>
      Parameters:
      random - provider for generating random values
      Returns:
      generated value or null if value is nullable, could not be generated, or generation is delegated to the engine
    • getDelegate

      public final Generator<T> getDelegate()
    • decorate

      public static <T> Generator<T> decorate(Supplier<T> supplier)
      Type Parameters:
      T - generated type
      Parameters:
      supplier - to decorate with hints
      Returns:
      generator backed by the given supplier
    • replaceHints

      public static <T> Generator<T> replaceHints(Generator<T> generator, Hints hints)
      Decorates the generator with the specified hints. The original hints will be ignored.
      Type Parameters:
      T - generated type
      Parameters:
      generator - to decorate with new hints
      hints - replacement hints
      Returns:
      generator with substituted hints
    • decorateIfNullAfterGenerate

      public static <T> Generator<T> decorateIfNullAfterGenerate(Generator<T> generator, AfterGenerate defaultAfterGenerate)
      Decorates the generator with the defaultAfterGenerate action if the generator does not specify the action in its hints. Retains all the other hints defined by the generator.
      Type Parameters:
      T - generated type
      Parameters:
      generator - to decorate with the action
      defaultAfterGenerate - the default action
      Returns:
      generator with updated hints if no action was specified in the original hints