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

net.jqwik.engine.properties.arbitraries.DefaultSetArbitrary Maven / Gradle / Ivy

There is a newer version: 1.9.1
Show newest version
package net.jqwik.engine.properties.arbitraries;

import java.util.*;
import java.util.function.*;
import java.util.stream.*;

import net.jqwik.api.*;
import net.jqwik.api.arbitraries.*;
import net.jqwik.api.support.*;
import net.jqwik.engine.properties.*;
import net.jqwik.engine.properties.arbitraries.exhaustive.*;
import net.jqwik.engine.properties.arbitraries.randomized.*;
import net.jqwik.engine.properties.shrinking.*;

import org.jspecify.annotations.*;

public class DefaultSetArbitrary extends MultivalueArbitraryBase> implements SetArbitrary {

	public DefaultSetArbitrary(Arbitrary elementArbitrary) {
		super(elementArbitrary);
		uniquenessExtractors.add(FeatureExtractor.identity());
	}

	@Override
	protected Iterable toIterable(Set streamable) {
		return streamable;
	}

	@Override
	public RandomGenerator> generator(int genSize) {
		return rawGenerator(genSize, false);
	}

	@Override
	public RandomGenerator> generatorWithEmbeddedEdgeCases(int genSize) {
		return rawGenerator(genSize, true);
	}

	private RandomGenerator> rawGenerator(int genSize, boolean withEmbeddedEdgeCases) {
		RandomGenerator elementGenerator = elementGenerator(elementArbitrary, genSize, withEmbeddedEdgeCases);
		return RandomGenerators.set(elementGenerator, minSize, maxSize(), genSize, sizeDistribution, uniquenessExtractors, elementArbitrary);
	}

	@Override
	public Optional>> exhaustive(long maxNumberOfSamples) {
		return ExhaustiveGenerators.set(elementArbitrary, minSize, maxSize(), uniquenessExtractors, maxNumberOfSamples);
	}

	@Override
	public EdgeCases> edgeCases(int maxEdgeCases) {
		return edgeCases((elementList, minSize1) -> {
			Set> elementSet = new LinkedHashSet<>(elementList);
			return new ShrinkableSet<>(elementSet, minSize1, maxSize(), uniquenessExtractors, elementArbitrary);
		}, maxEdgeCases);
	}

	@Override
	public SetArbitrary ofMaxSize(int maxSize) {
		return (SetArbitrary) super.ofMaxSize(maxSize);
	}

	@Override
	public SetArbitrary ofMinSize(int minSize) {
		return (SetArbitrary) super.ofMinSize(minSize);
	}

	@Override
	public SetArbitrary withSizeDistribution(RandomDistribution distribution) {
		return (SetArbitrary) super.withSizeDistribution(distribution);
	}

	// TODO: Remove duplication with DefaultListArbitrary.mapEach()
	@Override
	public  Arbitrary> mapEach(BiFunction, ? super T, ? extends U> mapper) {
		return this.map(elements -> elements.stream()
											.map(e -> mapper.apply(elements, e))
											.collect(CollectorsSupport.toLinkedHashSet()));
	}

	// TODO: Remove duplication with DefaultListArbitrary.flatMapEach()
	@Override
	public  Arbitrary> flatMapEach(BiFunction, ? super T, ? extends Arbitrary> flatMapper) {
		return this.flatMap(elements -> {
			List> arbitraries =
				elements.stream()
						.map(e -> flatMapper.apply(elements, e))
						.collect(Collectors.toList());
			return Combinators.combine(arbitraries).as(LinkedHashSet::new);
		});
	}

	@Override
	public SetArbitrary uniqueElements() {
		return this;
	}

	@Override
	public SetArbitrary uniqueElements(Function by) {
		FeatureExtractor featureExtractor = by::apply;
		return (SetArbitrary) super.uniqueElements(featureExtractor);
	}
}