Interface Gen<T>

Type Parameters:
T - type of values generated
All Known Implementing Classes:
AbstractStringGenerator, ArrayGenerator, ArrayListGenerator, BiFunctionGenerator, BigDecimalGenerator, BigIntegerGenerator, BinaryOperatorGenerator, BiPredicateGenerator, BitSetGenerator, BooleanGenerator, ByteGenerator, CallableGenerator, CharacterGenerator, CharsetGenerator, ClockGenerator, CollectionGenerator, ComponentizedGenerator, CompositeGenerator, Ctor, DateGenerator, DecimalGenerator, DoubleFunctionGenerator, DoubleGenerator, DurationGenerator, Encoded, EnumGenerator, ExhaustiveDomainGenerator, Fields, FloatGenerator, FunctionGenerator, FunctionGenerator, Generator, GuaranteeValuesGenerator, HashMapGenerator, HashSetGenerator, HashtableGenerator, InstantGenerator, IntegerGenerator, IntegralGenerator, IntFunctionGenerator, LambdaGenerator, LinkedHashMapGenerator, LinkedHashSetGenerator, LinkedListGenerator, ListGenerator, LocalDateGenerator, LocalDateTimeGenerator, LocaleGenerator, LocalTimeGenerator, LongFunctionGenerator, LongGenerator, MapGenerator, MarkerInterfaceGenerator, MonthDayGenerator, NullableGenerator, OffsetDateTimeGenerator, OffsetTimeGenerator, OptionalDoubleGenerator, OptionalGenerator, OptionalGenerator, OptionalIntGenerator, OptionalLongGenerator, PeriodGenerator, PredicateGenerator, PredicateGenerator, PropertiesGenerator, RFC4122.AbstractUUIDGenerator, RFC4122.NameBasedUUIDGenerator, RFC4122.Version3, RFC4122.Version4, RFC4122.Version5, SamplingDomainGenerator, SetGenerator, ShortGenerator, StackGenerator, StringGenerator, SupplierGenerator, SupplierGenerator, TimeZoneGenerator, ToDoubleBiFunctionGenerator, ToDoubleFunctionGenerator, ToIntBiFunctionGenerator, ToIntFunctionGenerator, ToLongBiFunctionGenerator, ToLongFunctionGenerator, UnaryOperatorGenerator, VectorGenerator, VoidGenerator, YearGenerator, YearMonthGenerator, ZilchGenerator, ZonedDateTimeGenerator, ZoneIdGenerator, ZoneOffsetGenerator
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 Gen<T>
Represents a strategy for generating random values.
  • Method Summary

    Modifier and Type
    Method
    Description
    default T
    Deprecated.
    For use by junit-quickcheck only -- when we migrate to Java 9 or better, this method will likely become private.
    default Gen<T>
    filter(Predicate<? super T> condition)
    Gives a generation strategy that produces a random value by having this strategy produce random values until one satisfies the given condition, then using that value as the result.
    default Gen<Optional<T>>
    filterOptional(Predicate<? super T> condition)
    Gives a generation strategy that produces a random value by having this strategy produce a random value and wrapping it in an Optional; if the value meets the given condition, the wrapping optional will be present.
    default <U> Gen<U>
    flatMap(Function<? super T,? extends Gen<? extends U>> mapper)
    Gives a generation strategy that produces a random value by having this strategy produce a random value, then applying the given function to that value, and asking the result to produce a value.
    static <U> Pair<Integer,Gen<? extends U>>
    freq(int weight, Gen<? extends U> generator)
    Helper for making a weighted generator indicator for frequency(Pair, Pair[]).
    static <U> Gen<U>
    frequency(Pair<Integer,Gen<? extends U>> first, Pair<Integer,Gen<? extends U>>... rest)
    Gives a generation strategy that produces a random value by choosing one of the given generators at random with probability in proportion to their given weights, and having it generate a value.
    Generates a value, possibly influenced by a source of randomness and metadata about the generation.
    default <U> Gen<U>
    map(Function<? super T,? extends U> mapper)
    Gives a generation strategy that produces a random value by having this strategy produce a random value, then applying the given function to that value, and returning the result.
    static <U> Gen<U>
    oneOf(Gen<? extends U> first, Gen<? extends U>... rest)
    Gives a generation strategy that produces a random value by choosing one of the given generators at random with (approximately) equal probability, and having it generate a value.
    static <U> Gen<U>
    oneOf(U first, U... rest)
    Gives a generation strategy that produces a random value by choosing one of the given values at random with (approximately) equal probability.
    static <U> Gen<U>
    pure(U constant)
    Gives a generation strategy that produces the given value, always.
    default Gen<List<T>>
    times(int times)
     
  • Method Details

    • generate

      T generate(SourceOfRandomness random, GenerationStatus status)
      Generates a value, possibly influenced by a source of randomness and metadata about the generation.
      Parameters:
      random - source of randomness to be used when generating the value
      status - an object that can be used to influence the generated value. For example, generating lists can use the size method to generate lists with a given number of elements.
      Returns:
      the generated value
    • map

      default <U> Gen<U> map(Function<? super T,? extends U> mapper)
      Gives a generation strategy that produces a random value by having this strategy produce a random value, then applying the given function to that value, and returning the result.
      Type Parameters:
      U - type of values produced by the mapped generation strategy
      Parameters:
      mapper - function that converts receiver's random values to result's values
      Returns:
      a new generation strategy
    • flatMap

      default <U> Gen<U> flatMap(Function<? super T,? extends Gen<? extends U>> mapper)
      Gives a generation strategy that produces a random value by having this strategy produce a random value, then applying the given function to that value, and asking the result to produce a value.
      Type Parameters:
      U - type of values produced by the mapped generation strategy
      Parameters:
      mapper - function that converts receiver's random values to another kind of generation strategy
      Returns:
      a new generation strategy
    • filter

      default Gen<T> filter(Predicate<? super T> condition)
      Gives a generation strategy that produces a random value by having this strategy produce random values until one satisfies the given condition, then using that value as the result.
      Parameters:
      condition - a condition the generated value is to meet
      Returns:
      a new generation strategy
    • filterOptional

      default Gen<Optional<T>> filterOptional(Predicate<? super T> condition)

      Gives a generation strategy that produces a random value by having this strategy produce a random value and wrapping it in an Optional; if the value meets the given condition, the wrapping optional will be present.

      If the value meets the condition but is null, the wrapping optional will not be considered present.

      Parameters:
      condition - a condition the generated value is to meet
      Returns:
      a new generation strategy
    • times

      default Gen<List<T>> times(int times)
    • pure

      static <U> Gen<U> pure(U constant)
      Gives a generation strategy that produces the given value, always.
      Type Parameters:
      U - type of values produced by the resulting strategy
      Parameters:
      constant - the value to be returned by the resulting strategy
      Returns:
      a new generation strategy
    • oneOf

      @SafeVarargs static <U> Gen<U> oneOf(U first, U... rest)
      Gives a generation strategy that produces a random value by choosing one of the given values at random with (approximately) equal probability.
      Type Parameters:
      U - type of values produced by the resulting strategy
      Parameters:
      first - first possible choice
      rest - the other possible choices
      Returns:
      a new generation strategy
    • oneOf

      @SafeVarargs static <U> Gen<U> oneOf(Gen<? extends U> first, Gen<? extends U>... rest)
      Gives a generation strategy that produces a random value by choosing one of the given generators at random with (approximately) equal probability, and having it generate a value.
      Type Parameters:
      U - type of values produced by the resulting strategy
      Parameters:
      first - first possible generator choice
      rest - the other possible generator choices
      Returns:
      a new generation strategy
    • frequency

      @SafeVarargs static <U> Gen<U> frequency(Pair<Integer,Gen<? extends U>> first, Pair<Integer,Gen<? extends U>>... rest)
      Gives a generation strategy that produces a random value by choosing one of the given generators at random with probability in proportion to their given weights, and having it generate a value.
      Type Parameters:
      U - type of values produced by the resulting strategy
      Parameters:
      first - first possible (weighted) generator choice
      rest - the other possible (weighted) generator choices
      Returns:
      a new generation strategy
    • freq

      static <U> Pair<Integer,Gen<? extends U>> freq(int weight, Gen<? extends U> generator)
      Helper for making a weighted generator indicator for frequency(Pair, Pair[]).
      Type Parameters:
      U - type of values produced by the given generation strategy
      Parameters:
      weight - an integer weight
      generator - a generator
      Returns:
      a weight-generator pair
    • _gen

      @Deprecated default T _gen(SourceOfRandomness random, GenerationStatus status)
      Deprecated.
      For use by junit-quickcheck only -- when we migrate to Java 9 or better, this method will likely become private.
      Parameters:
      random - source of randomness to be used when generating the value
      status - an object that can be used to influence the generated value. For example, generating lists can use the size method to generate lists with a given number of elements.
      Returns:
      the generated value