net.jqwik.engine.properties.arbitraries.combinations.DefaultCombinator3 Maven / Gradle / Ivy
The newest version!
package net.jqwik.engine.properties.arbitraries.combinations;
import java.util.*;
import java.util.function.*;
import org.jspecify.annotations.*;
import net.jqwik.api.*;
public class DefaultCombinator3
extends DefaultCombinator2
implements Combinators.Combinator3 {
protected final Arbitrary a3;
public DefaultCombinator3(Arbitrary a1, Arbitrary a2, Arbitrary a3) {
super(a1, a2);
this.a3 = a3;
}
@Override
public Arbitrary as(Combinators.F3 super T1, ? super T2, ? super T3, ? extends R> combinator) {
return new CombineArbitrary<>(combineFunction(combinator), a1, a2, a3);
}
@Override
public Combinators.Combinator3 filter(Combinators.F3 super T1, ? super T2, ? super T3, Boolean> filter) {
return new Filtered<>(a1, a2, a3, filter);
}
@SuppressWarnings("unchecked")
protected Function, R> combineFunction(Combinators.F3 super T1, ? super T2, ? super T3, ? extends R> combinator) {
return params -> combinator.apply((T1) params.get(0), (T2) params.get(1), (T3) params.get(2));
}
private static class Filtered extends DefaultCombinator3 {
private final Combinators.F3 super T1, ? super T2, ? super T3, Boolean> filter;
private Filtered(Arbitrary a1, Arbitrary a2, Arbitrary a3, Combinators.F3 super T1, ? super T2, ? super T3, Boolean> filter) {
super(a1, a2, a3);
this.filter = filter;
}
@Override
public Arbitrary as(Combinators.F3 super T1, ? super T2, ? super T3, ? extends R> combinator) {
return new CombineArbitrary<>(Function.identity(), a1, a2, a3)
.filter(combineFunction(filter)::apply)
.map(combineFunction(combinator));
}
@Override
public Combinators.Combinator3 filter(Combinators.F3 super T1, ? super T2, ? super T3, Boolean> filter) {
return new Filtered<>(a1, a2, a3, combineFilters(this.filter, filter));
}
@SuppressWarnings("unchecked")
private Combinators.F3 combineFilters(
Combinators.F3 super T1, ? super T2, ? super T3, Boolean> filter1,
Combinators.F3 super T1, ? super T2, ? super T3, Boolean> filter2
) {
return (t1, t2, t3) -> filter1.apply(t1, t2, t3) && filter2.apply(t1, t2, t3);
}
}
}