net.jqwik.engine.properties.arbitraries.randomized.CollectGenerator Maven / Gradle / Ivy
package net.jqwik.engine.properties.arbitraries.randomized;
import java.util.*;
import java.util.function.*;
import net.jqwik.api.*;
import net.jqwik.engine.properties.shrinking.*;
import org.jspecify.annotations.*;
public class CollectGenerator implements RandomGenerator> {
private final RandomGenerator elementGenerator;
private final Predicate super List extends T>> until;
public CollectGenerator(RandomGenerator elementGenerator, Predicate super List extends T>> until) {
this.elementGenerator = elementGenerator;
this.until = until;
}
@Override
public Shrinkable> next(Random random) {
List base = new ArrayList<>();
List> shrinkables = new ArrayList<>();
for (int i = 0; i < 10000; i++) {
if (until.test(base)) {
return new CollectShrinkable<>(shrinkables, until);
}
Shrinkable shrinkable = elementGenerator.next(random);
base.add(shrinkable.value());
shrinkables.add(shrinkable);
}
String message = String.format("Generated list not fulfilled condition after maximum of %s elements", 10000);
throw new JqwikException(message);
}
}