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

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

The newest version!
package net.jqwik.engine.properties.arbitraries;

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

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

public class ArbitraryFlatMap implements Arbitrary {
	private final Arbitrary self;
	private final Function> mapper;

	public ArbitraryFlatMap(Arbitrary self, Function> mapper) {
		this.self = self;
		this.mapper = mapper;
	}

	@Override
	public RandomGenerator generator(int genSize) {
		return self.generator(genSize).flatMap(mapper, genSize, false);
	}

	@Override
	public RandomGenerator generatorWithEmbeddedEdgeCases(int genSize) {
		return self.generatorWithEmbeddedEdgeCases(genSize).flatMap(mapper, genSize, true);
	}

	@Override
	public Optional> exhaustive(long maxNumberOfSamples) {
		return self.exhaustive(maxNumberOfSamples)
				   .flatMap(generator -> ExhaustiveGenerators.flatMap(generator, mapper, maxNumberOfSamples));
	}

	@Override
	public EdgeCases edgeCases(int maxEdgeCases) {
		return EdgeCasesSupport.flatMapArbitrary(self.edgeCases(maxEdgeCases), mapper, maxEdgeCases);
	}

	@Override
	public boolean isGeneratorMemoizable() {
		return self.isGeneratorMemoizable();
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;

		ArbitraryFlatMap that = (ArbitraryFlatMap) o;
		if (!self.equals(that.self)) return false;
		return LambdaSupport.areEqual(mapper, that.mapper);
	}

	@Override
	public int hashCode() {
		return HashCodeSupport.hash(self, mapper.getClass());
	}
}