Interface Generator<T>

Type Parameters:
T - type to generate
All Superinterfaces:
GeneratorSpec<T>
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface Generator<T> extends GeneratorSpec<T>
A class for generating values of a specific type.
Since:
1.0.1
  • Method Summary

    Modifier and Type
    Method
    Description
    generate(Random random)
    Returns a generated value.
    default Hints
    Hints provided by the generator to the engine.
    default void
    An optional method for generators that need to initialise state before generating values.
  • Method Details

    • init

      @ExperimentalApi default void init(GeneratorContext context)
      An optional method for generators that need to initialise state before generating values.

      Note: this method is guaranteed to be called before 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.

      Parameters:
      context - generator context
      Since:
      2.13.0
    • generate

      T generate(Random random)
      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.

      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
    • hints

      default Hints hints()
      Hints provided by the generator to the engine.

      The most important hint for custom generators is AfterGenerate. This hint indicates whether the object created by this generator:

      • should be populated (for example, if it has null fields)
      • can be modified using selectors

      For example, setting the hint to AfterGenerate.POPULATE_NULLS will cause Instancio to populate null fields on the object returned by this generator:

      
         @Override
         public Hints hints() {
             return Hints.afterGenerate(AfterGenerate.POPULATE_NULLS);
         }
       

      If the action is not specified, default behaviour will be based on the AfterGenerate value configured in the Settings using the key Keys.AFTER_GENERATE_HINT.

      In addition, the following hints can be provided for populating data structures:

      Returns:
      hints from this generator to the engine
      Since:
      2.0.0
      See Also: