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.engine.properties.*;
import net.jqwik.engine.properties.arbitraries.exhaustive.*;
import net.jqwik.engine.properties.arbitraries.randomized.*;
import net.jqwik.engine.properties.shrinking.*;

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);
	}

	@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 HashSet<>(elementList);
			return new ShrinkableSet<>(elementSet, minSize1, maxSize, uniquenessExtractors);
		}, 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, T, U> mapper) {
		return this.map(elements -> elements.stream()
											.map(e -> mapper.apply(elements, e))
											.collect(Collectors.toSet()));
	}

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

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