net.jqwik.engine.properties.shrinking.ShrinkableSet Maven / Gradle / Ivy
The newest version!
package net.jqwik.engine.properties.shrinking;
import java.util.*;
import java.util.stream.*;
import org.jspecify.annotations.*;
import net.jqwik.api.*;
import net.jqwik.engine.properties.*;
public class ShrinkableSet extends ShrinkableContainer, E> {
public ShrinkableSet(
Collection extends Shrinkable> elements,
int minSize, int maxSize,
Collection extends FeatureExtractor> uniquenessExtractors,
@Nullable Arbitrary elementArbitrary
) {
this(new ArrayList<>(elements), minSize, maxSize, uniquenessExtractors, elementArbitrary);
}
private ShrinkableSet(
List extends Shrinkable> elements,
int minSize, int maxSize,
Collection extends FeatureExtractor> uniquenessExtractors,
@Nullable Arbitrary elementArbitrary
) {
super(elements, minSize, maxSize, uniquenessExtractors, elementArbitrary);
}
@Override
public Stream>> shrink() {
return super.shrink().filter(shrinkableSet -> shrinkableSet.value().size() >= minSize);
}
@Override
protected boolean hasReallyGrown(Shrinkable> grownShrinkable) {
return grownShrinkable.value().size() > elements.size();
}
@Override
Set createValue(List extends Shrinkable> shrinkables) {
// See https://richardstartin.github.io/posts/5-java-mundane-performance-tricks#size-hashmaps-whenever-possible
// for how to compute initial capacity of hash maps
int capacityWithLoadFactor = shrinkables.size() * 4 / 3;
// Using loop instead of stream to make stack traces more readable
Set values = new LinkedHashSet<>(capacityWithLoadFactor);
for (Shrinkable shrinkable : shrinkables) {
values.add(shrinkable.value());
}
return values;
}
@Override
Shrinkable> createShrinkable(List extends Shrinkable> shrunkElements) {
return new ShrinkableSet<>(shrunkElements, minSize, maxSize, uniquenessExtractors, elementArbitrary);
}
}