Interface AsGeneratorSpec<T>

Type Parameters:
T - generated type
All Superinterfaces:
GeneratorSpec<T>
All Known Subinterfaces:
BigDecimalGeneratorSpec, BigDecimalSpec, BigIntegerSpec, BooleanGeneratorSpec, BooleanSpec, ByteSpec, CharacterGeneratorSpec, CharacterSpec, DoubleSpec, EmailGeneratorSpec, EmailSpec, EnumGeneratorSpec<E>, EnumSpec<E>, FilePathSpec<T>, FileSpec, FloatSpec, HashGeneratorSpec, HashSpec, InstantGeneratorSpec, InstantSpec, IntegerSpec, LocalDateSpec, LocalDateTimeGeneratorSpec, LocalDateTimeSpec, LocalTimeGeneratorSpec, LocalTimeSpec, LongSpec, LuhnGeneratorSpec, LuhnSpec, Mod10GeneratorSpec, Mod10Spec, Mod11GeneratorSpec, Mod11Spec, MonthDayGeneratorSpec, MonthDaySpec, NumberGeneratorSpec<T>, NumberSpec<T>, NumericSequenceGeneratorSpec<T>, NumericSequenceSpec<T>, OffsetDateTimeGeneratorSpec, OffsetDateTimeSpec, OffsetTimeGeneratorSpec, OffsetTimeSpec, PathGeneratorSpec<T>, PathSpec, ShortSpec, SinGeneratorSpec, SinSpec, TemporalGeneratorSpec<T>, TemporalSpec<T>, TextPatternGeneratorSpec, TextPatternSpec, URIGeneratorSpec, URISpec, URLGeneratorSpec, URLSpec, YearMonthSpec, YearSpec, ZonedDateTimeGeneratorSpec, ZonedDateTimeSpec

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