net.jqwik.engine.properties.arbitraries.OneOfArbitrary Maven / Gradle / Ivy
package net.jqwik.engine.properties.arbitraries;
import java.util.*;
import java.util.function.*;
import net.jqwik.api.*;
import net.jqwik.api.configurators.*;
import net.jqwik.api.providers.*;
import net.jqwik.engine.properties.arbitraries.exhaustive.*;
import net.jqwik.engine.properties.arbitraries.randomized.*;
public class OneOfArbitrary implements Arbitrary, SelfConfiguringArbitrary {
private final List> all = new ArrayList<>();
public OneOfArbitrary(List> all) {this.all.addAll(all);}
@Override
public RandomGenerator generator(int genSize) {
return RandomGenerators.choose(all).flatMap(Function.identity(), genSize);
}
@Override
public Optional> exhaustive() {
return ExhaustiveGenerators.choose(all).flatMap(generator -> ExhaustiveGenerators.flatMap(generator, Function.identity()));
}
@Override
public Arbitrary configure(ArbitraryConfigurator configurator, TypeUsage targetType) {
all.replaceAll(a -> {
if (a instanceof SelfConfiguringArbitrary) {
// TODO: This condition exists 3 times
//noinspection unchecked
return ((SelfConfiguringArbitrary) a).configure(configurator, targetType);
} else {
return configurator.configure(a, targetType);
}
});
return this;
}
protected List> arbitraries() {
return all;
}
protected void addArbitrary(Arbitrary arbitrary) {
all.add(arbitrary);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy