net.jqwik.api.configurators.SelfConfiguringArbitrary Maven / Gradle / Ivy
package net.jqwik.api.configurators;
import org.apiguardian.api.*;
import net.jqwik.api.*;
import net.jqwik.api.providers.*;
import org.jspecify.annotations.*;
import static org.apiguardian.api.API.Status.*;
/**
* An {@linkplain Arbitrary} implementation can also implement this interface if it wants
* to take over its own configuration which is usually being done by registered instances
* of {@linkplain ArbitraryConfigurator}.
*
*
* There are a few implementors within jqwik's own codebase:
*
*
* - net.jqwik.engine.properties.arbitraries.OneOfArbitrary
* - net.jqwik.engine.properties.arbitraries.FrequencyOfArbitrary
* - net.jqwik.engine.properties.arbitraries.ArrayArbitrary
*
*/
@API(status = MAINTAINED, since = "1.0")
public interface SelfConfiguringArbitrary {
/**
* If an arbitrary is self configuring use it, otherwise use default configurator
*/
@SuppressWarnings("unchecked")
@API(status = INTERNAL)
static Arbitrary configure(Arbitrary self, ArbitraryConfigurator configurator, TypeUsage targetType) {
if (self instanceof SelfConfiguringArbitrary) {
return ((SelfConfiguringArbitrary) self).configure(configurator, targetType);
} else {
return configurator.configure(self, targetType);
}
}
/**
* Do all configuration yourself or delegate to {@link ArbitraryConfigurator#configure(Arbitrary, TypeUsage)}
* of the {@code configurator} that's being handed in.
*
* @param configurator the configurator to use
* @param targetType the target type of the arbitrary
* @return the arbitrary instance or a new derived one
*/
Arbitrary configure(ArbitraryConfigurator configurator, TypeUsage targetType);
}