Interface AsGeneratorSpec<T>

Type Parameters:
T - generated type
All Superinterfaces:
GeneratorSpec<T>
All Known Subinterfaces:
BigDecimalAsGeneratorSpec, BooleanAsGeneratorSpec, CharacterAsGeneratorSpec, EmailAsGeneratorSpec, EnumAsGeneratorSpec<E>, HashAsGeneratorSpec, InstantGeneratorAsSpec, LocalDateTimeGeneratorAsSpec, LocalTimeGeneratorAsSpec, LuhnAsGeneratorSpec, Mod10AsGeneratorSpec, Mod11AsGeneratorSpec, MonthDayGeneratorSpec, NumberAsGeneratorSpec<T>, NumericSequenceAsGeneratorSpec<T>, OffsetDateTimeGeneratorAsSpec, OffsetTimeGeneratorAsSpec, PathAsGeneratorSpec<T>, SinAsGeneratorSpec, TemporalAsGeneratorSpec<T>, TextPatternAsGeneratorSpec, URIAsGeneratorSpec, URLAsGeneratorSpec, ZonedDateTimeGeneratorAsSpec

public interface AsGeneratorSpec<T> extends GeneratorSpec<T>
Allows mapping the result of a generator to another type.
Since:
2.11.0
  • Method Summary

    Modifier and Type
    Method
    Description
    default <R> GeneratorSpec<R>
    as(Function<T,R> mappingFunction)
    Converts the generated value to another type using the specified mapping function.
    Converts the generated value to a String.
  • Method Details

    • asString

      default GeneratorSpec<String> asString()
      Converts the generated value to a String.
      Returns:
      generator spec with the result mapped to a string using Object.toString()
      Since:
      2.11.0
    • as

      default <R> GeneratorSpec<R> as(Function<T,R> mappingFunction)
      Converts the generated value to another type using the specified mapping function.

      Example:

      
         record LogEntry(String msg, long timestamp) {}
      
         LogEntry result = Instancio.of(LogEntry.class)
           .generate(field(Log::timestamp), gen -> gen.temporal().instant().past().as(Instant::toEpochMilli))
           .create();
       
      Type Parameters:
      R - resulting type after applying the function
      Parameters:
      mappingFunction - for mapping generated value to another type
      Returns:
      generator spec with mapped result
      Since:
      2.11.0