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

net.jqwik.engine.properties.arbitraries.ArbitraryMap 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 net.jqwik.api.*;
import net.jqwik.api.support.*;

import org.jspecify.annotations.*;

public class ArbitraryMap implements Arbitrary {
	private final Arbitrary self;
	private final Function mapper;

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

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

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

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

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

	@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;

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

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