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

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

import org.jspecify.annotations.*;

public class JustArbitrary implements Arbitrary {

	private final T value;

	public JustArbitrary(T value) {
		this.value = value;
	}

	@Override
	public  Arbitrary flatMap(Function> mapper) {
		// Optimization: just(value).flatMap(mapper) -> mapper(value)
		return mapper.apply(value);
	}

	@Override
	public  Arbitrary map(Function mapper) {
		// Optimization: just(value).map(mapper) -> just(mapper(value))
		return new JustArbitrary<>(mapper.apply(value));
	}

	@Override
	public RandomGenerator generator(int tries) {
		return random -> Shrinkable.unshrinkable(value);
	}

	@Override
	public Optional> exhaustive(long maxNumberOfSamples) {
		return ExhaustiveGenerators.choose(Arrays.asList(value), maxNumberOfSamples);
	}

	@Override
	public EdgeCases edgeCases(int maxEdgeCases1) {
		return maxEdgeCases1 <= 0
				   ? EdgeCases.none()
				   : EdgeCases.fromSupplier(() -> Shrinkable.unshrinkable(value));
	}

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

		JustArbitrary that = (JustArbitrary) o;
		return Objects.equals(value, that.value);
	}

	@Override
	public int hashCode() {
		return value != null ? value.hashCode() : 0;
	}
}