Package org.instancio

Interface Generator<T>

Type Parameters:
T - type to generate
All Superinterfaces:
GeneratorSpec<T>
All Known Implementing Classes:
AbstractGenerator, AbstractRandomComparableNumberGeneratorSpec, AbstractRandomNumberGeneratorSpec, ArrayGenerator, AtomicIntegerGenerator, AtomicLongGenerator, BigDecimalGenerator, BigIntegerGenerator, BooleanGenerator, ByteGenerator, CharacterGenerator, CollectionGenerator, CollectionGeneratorSpecImpl, ConcurrentHashMapGenerator, ConcurrentSkipListMapGenerator, DateGenerator, DoubleGenerator, EnumGenerator, EnumSetGenerator, FloatGenerator, HashSetGenerator, InstantiatingGenerator, IntegerGenerator, LocalDateGenerator, LocalDateTimeGenerator, LongGenerator, MapGenerator, MapGeneratorSpecImpl, OneOfArrayGenerator, OneOfCollectionGenerator, ShortGenerator, StringGenerator, TextPatternGenerator, TreeMapGenerator, TreeSetGenerator, UUIDGenerator, XMLGregorianCalendarGenerator
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 generator of values of a specific type.
  • Method Summary

    Modifier and Type
    Method
    Description
    default Optional<String>
    Returns the public API method name of this generator.
    Returns a generated value.
    Returns hints, including collection sizes and whether values are nullable.
    default boolean
    If true, then this generator delegate object instantiation to another generator supplied via setDelegate(Generator).
    default void
    setDelegate(Generator<?> delegate)
    Set a delegate that will be responsible for instantiating an object on behalf of this generator.
    default boolean
    supports(Class<?> type)
    Checks whether this generator can generate given type.
    default Optional<Class<?>>
    Target class to generate.
  • Method Details

    • generate

      T generate(RandomProvider random)
      Returns a generated value.

      If the generated value is random, it needs to be generated using the given RandomProvider instance. This ensures the data is generated with the same seed value and allows random data to be reproduced by specifying the seed value.

      Parameters:
      random - provider for random values
      Returns:
      generated value
    • isDelegating

      default boolean isDelegating()
      If true, then this generator delegate object instantiation to another generator supplied via setDelegate(Generator).
      Returns:
      true if this is a delegating generator
    • setDelegate

      default void setDelegate(Generator<?> delegate)
      Set a delegate that will be responsible for instantiating an object on behalf of this generator.
      Parameters:
      delegate - that will create the target object
    • apiMethodName

      default Optional<String> apiMethodName()
      Returns the public API method name of this generator. Will return an empty result if this instance is a lambda.
      Returns:
      spec name, if defined
    • targetClass

      default Optional<Class<?>> targetClass()
      Target class to generate.

      If Optional.empty() is returned, it will default to the field type for fields, and element type for collection and array elements.

      If the type is an interface, such as Set, will generate a default implementation class such as HashSet.

      Returns:
      target class
    • supports

      default boolean supports(Class<?> type)
      Checks whether this generator can generate given type.
      Parameters:
      type - to check
      Returns:
      true if generator can
    • getHints

      default GeneratedHints getHints()
      Returns hints, including collection sizes and whether values are nullable.
      Returns:
      generated hints