net.jqwik.api.Arbitrary Maven / Gradle / Ivy
package net.jqwik.api;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
import com.google.errorprone.annotations.*;
import org.apiguardian.api.*;
import org.jspecify.annotations.*;
import net.jqwik.api.arbitraries.*;
import static org.apiguardian.api.API.Status.*;
/**
* The main interface for representing objects that can be generated and shrunk.
*
* @param The type of generated objects. Primitive objects (e.g. int, boolean etc.) are represented by their boxed
* type (e.g. Integer, Boolean).
*/
@API(status = STABLE, since = "1.0")
@CheckReturnValue
public interface Arbitrary {
@API(status = INTERNAL)
abstract class ArbitraryFacade {
private static final ArbitraryFacade implementation;
static {
implementation = FacadeLoader.load(ArbitraryFacade.class);
}
public abstract ListArbitrary list(Arbitrary elementArbitrary);
public abstract SetArbitrary set(Arbitrary elementArbitrary);
public abstract StreamArbitrary stream(Arbitrary elementArbitrary);
public abstract IteratorArbitrary iterator(Arbitrary elementArbitrary);
public abstract ArrayArbitrary array(Arbitrary elementArbitrary, Class arrayClass);
public abstract Stream sampleStream(Arbitrary arbitrary);
public abstract Arbitrary<@Nullable T> injectNull(Arbitrary self, double nullProbability);
public abstract Arbitrary filter(Arbitrary self, Predicate super T> filterPredicate, int maxMisses);
public abstract Arbitrary map(Arbitrary self, Function super T, ? extends U> mapper);
public abstract Arbitrary flatMap(Arbitrary self, Function super T, ? extends Arbitrary> mapper);
public abstract Arbitrary ignoreExceptions(Arbitrary self, int maxThrows, Class extends Throwable>[] exceptionTypes);
public abstract Arbitrary dontShrink(Arbitrary self);
public abstract Arbitrary configureEdgeCases(Arbitrary self, Consumer super EdgeCases.Config> configurator);
public abstract Arbitrary withoutEdgeCases(Arbitrary self);
public abstract RandomGenerator memoizedGenerator(Arbitrary self, int genSize, boolean withEdgeCases);
public abstract Arbitrary fixGenSize(Arbitrary self, int genSize);
public abstract Arbitrary> collect(Arbitrary self, Predicate super List extends T>> until);
}
/**
* Create the random generator for an arbitrary.
*
*
* Starting with version 1.4.0 the returned generator should no longer
* include edge cases explicitly since those will be injected in {@linkplain #generator(int, boolean)}
*
*
* @param genSize a very unspecific configuration parameter that can be used
* to influence the configuration and behaviour of a random generator
* if and only if the generator wants to be influenced.
* Many generators are independent of genSize.
*
* The default value of {@code genSize} is the number of tries configured
* for a property. Use {@linkplain Arbitrary#fixGenSize(int)} to fix
* the parameter for a given arbitrary.
* @return a new random generator instance
*/
RandomGenerator generator(int genSize);
/**
* Create the random generator for an arbitrary with or without edge cases.
*
* Never override this method. Override {@linkplain #generator(int)} instead.
*
* @param genSize See {@linkplain #generator(int)} about meaning of this parameter
* @param withEdgeCases True if edge cases should be injected into the stream of generated values
* @return a new random generator instance
*/
@API(status = INTERNAL, since = "1.4.0")
default RandomGenerator generator(int genSize, boolean withEdgeCases) {
return ArbitraryFacade.implementation.memoizedGenerator(this, genSize, withEdgeCases);
}
/**
* Create the random generator for an arbitrary where the embedded generators,
* if there are any, also generate edge cases.
*
*
* Override only if there are any embedded arbitraries / generators,
* e.g. a container using an element generator
*
*
* @param genSize See {@linkplain #generator(int)} about meaning of this parameter
* @return a new random generator instance
*/
@API(status = INTERNAL, since = "1.4.0")
default RandomGenerator generatorWithEmbeddedEdgeCases(int genSize) {
return generator(genSize);
}
/**
* @return The same instance but with type Arbitrary<Object>
*/
@SuppressWarnings("unchecked")
@API(status = INTERNAL)
default Arbitrary