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.*;
import java.util.function.*;
import java.util.stream.*;

import net.jqwik.api.*;

public class MappedShrinkable implements Shrinkable {

	private final Shrinkable toMap;
	private final Function mapper;

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

	@Override
	public U value() {
		return mapper.apply(toMap.value());
	}

	@Override
	public Stream> shrink() {
		return toMap.shrink().map(this::toMappedShrinkable);
	}

	public Shrinkable toMappedShrinkable(Shrinkable shrinkable) {
		return shrinkable.map(mapper);
	}

	@Override
	public Optional> grow(Shrinkable before, Shrinkable after) {
		if (before instanceof MappedShrinkable && after instanceof MappedShrinkable) {
			Shrinkable beforeToMap = ((MappedShrinkable) before).toMap;
			Shrinkable afterToMap = ((MappedShrinkable) after).toMap;
			return toMap.grow(beforeToMap, afterToMap).map(this::toMappedShrinkable);
		}
		return toMap.grow(before, after).map(this::toMappedShrinkable);
	}

	@Override
	public Stream> grow() {
		return toMap.grow().map(this::toMappedShrinkable);
	}

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

}