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

net.jqwik.engine.properties.shrinking.MappedShrinkable Maven / Gradle / Ivy

There is a newer version: 1.9.1
Show newest version
package net.jqwik.engine.properties.shrinking;

import java.util.function.*;

import net.jqwik.api.*;

public class MappedShrinkable implements Shrinkable {

	private final Shrinkable toMap;
	private final Function mapper;
	private final U value;

	public MappedShrinkable(Shrinkable toMap, Function mapper) {
		this.toMap = toMap;
		this.mapper = mapper;
		this.value = mapper.apply(toMap.value());
	}

	@Override
	public U value() {
		return value;
	}

	@Override
	public ShrinkingSequence shrink(Falsifier falsifier) {
		Falsifier toMapFalsifier = aT -> falsifier.test(mapper.apply(aT));
		return toMap.shrink(toMapFalsifier) //
			.map(result -> result.map(shrinkable -> shrinkable.map(mapper)));
	}

	@Override
	public ShrinkingDistance distance() {
		return toMap.distance();
	}

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

	@Override
	public int hashCode() {
		return toMap.hashCode();
	}

	@Override
	public String toString() {
		return String.format("Mapped<%s>(%s)|%s", value().getClass().getSimpleName(), value(), toMap);
	}

}