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

net.jqwik.vavr.arbitraries.base.MapBasedArbitrary Maven / Gradle / Ivy

package net.jqwik.vavr.arbitraries.base;

import io.vavr.Tuple2;
import io.vavr.collection.Traversable;

import net.jqwik.api.Arbitrary;
import net.jqwik.api.Arbitraries;
import net.jqwik.api.RandomDistribution;
import net.jqwik.api.arbitraries.ArbitraryDecorator;
import net.jqwik.vavr.api.arbitraries.MapArbitrary;

import java.util.function.Function;

/**
 * @param  Key type
 * @param  Value type
 * @param  Map type
 */
public abstract class MapBasedArbitrary>>
		extends ArbitraryDecorator
		implements MapArbitrary {

	private net.jqwik.api.arbitraries.MapArbitrary javaMapArbitrary;

	public MapBasedArbitrary(final Arbitrary keysArbitrary, final Arbitrary valuesArbitrary) {
		this.javaMapArbitrary = Arbitraries.maps(keysArbitrary, valuesArbitrary);
	}

	@Override
	public MapArbitrary ofMinSize(final int minSize) {
		final MapBasedArbitrary clone = typedClone();
		clone.javaMapArbitrary = javaMapArbitrary.ofMinSize(minSize);
		return clone;
	}

	@Override
	public MapArbitrary ofMaxSize(final int maxSize) {
		final MapBasedArbitrary clone = typedClone();
		clone.javaMapArbitrary = javaMapArbitrary.ofMaxSize(maxSize);
		return clone;
	}

	@Override
	public MapArbitrary withSizeDistribution(final RandomDistribution distribution) {
		final MapBasedArbitrary clone = typedClone();
		clone.javaMapArbitrary = javaMapArbitrary.withSizeDistribution(distribution);
		return clone;
	}

	@Override
	protected Arbitrary arbitrary() {
		return javaMapArbitrary.map(this::convertJavaMapToVavrMap);
	}

	protected abstract U convertJavaMapToVavrMap(java.util.Map javaMap);

	@Override
	public MapArbitrary uniqueKeys(final Function by) {
		final MapBasedArbitrary clone = typedClone();
		clone.javaMapArbitrary = javaMapArbitrary.uniqueKeys(by);
		return clone;
	}

	@Override
	public MapArbitrary uniqueValues(final Function by) {
		final MapBasedArbitrary clone = typedClone();
		clone.javaMapArbitrary = javaMapArbitrary.uniqueValues(by);
		return clone;
	}

	@SuppressWarnings("unchecked")
	@Override
	public MapArbitrary uniqueValues() {
		return uniqueValues((Function) Function.identity());
	}

}