net.jqwik.api.state.Transformation Maven / Gradle / Ivy
The newest version!
package net.jqwik.api.state;
import java.util.function.*;
import org.apiguardian.api.*;
import net.jqwik.api.*;
import org.jspecify.annotations.*;
import static org.apiguardian.api.API.Status.*;
/**
* A transformation provides an arbitrary of {@linkplain Transformer transformers}
* for values of type {@code T} in the context of {@linkplain Chain chains}.
* The provided arbitrary of transformers can depend on the previous state,
* which can be retrieved using the first {@linkplain Supplier supplier} argument of the function.
* A transformation can also be restricted by a precondition,
* which must hold for the transformation to be applicable.
*
* @param The type of state to be transformed in a chain
* @see Chain
* @see Transformer
*/
@FunctionalInterface
@API(status = EXPERIMENTAL, since = "1.7.0")
public interface Transformation extends Function, Arbitrary>> {
Predicate> NO_PRECONDITION = ignore -> false;
class Builder {
final private Predicate precondition;
private Builder(Predicate precondition) {
this.precondition = precondition;
}
public Transformation provide(Arbitrary> arbitrary) {
return new Transformation() {
@Override
public Predicate precondition() {
return precondition;
}
@Override
public Arbitrary> apply(Supplier ignore) {
return arbitrary;
}
};
}
public Transformation provide(Function>> arbitraryCreator) {
return new Transformation() {
@Override
public Predicate precondition() {
return precondition;
}
@Override
public Arbitrary> apply(Supplier supplier) {
return arbitraryCreator.apply(supplier.get());
}
};
}
}
/**
* Create a TransformerProvider with a precondition
*/
static Builder when(Predicate precondition) {
return new Builder(precondition);
}
/**
* Override this method if the applicability of the provided transformers depends on the previous state
*
* @return a predicate with input {@code T}
*/
@SuppressWarnings("unchecked")
default Predicate precondition() {
return (Predicate) NO_PRECONDITION;
}
}