All Downloads are FREE. Search and download functionalities are using the official Maven repository.

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> elements,
		int minSize, int maxSize,
		Collection> uniquenessExtractors,
		@Nullable Arbitrary elementArbitrary
	) {
		this(new ArrayList<>(elements), minSize, maxSize, uniquenessExtractors, elementArbitrary);
	}

	private ShrinkableSet(
		List> elements,
		int minSize, int maxSize,
		Collection> 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> 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> shrunkElements) {
		return new ShrinkableSet<>(shrunkElements, minSize, maxSize, uniquenessExtractors, elementArbitrary);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy