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

net.jqwik.engine.properties.arbitraries.combinations.DefaultCombinator2 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 DefaultCombinator2 implements Combinators.Combinator2 {
	protected final Arbitrary a2;
	protected final Arbitrary a1;

	public DefaultCombinator2(Arbitrary a1, Arbitrary a2) {
		this.a1 = a1;
		this.a2 = a2;
	}

	@Override
	public  Arbitrary as(Combinators.F2 combinator) {
		return new CombineArbitrary<>(combineFunction(combinator), a1, a2);
	}

	@Override
	public Combinators.Combinator2 filter(Combinators.F2 filter) {
		return new Filtered<>(a1, a2, filter);
	}

	@SuppressWarnings("unchecked")
	protected  Function, ? extends R> combineFunction(Combinators.F2 combinator2) {
		return params -> combinator2.apply((T1) params.get(0), (T2) params.get(1));
	}

	private static class Filtered extends DefaultCombinator2 {
		private final Combinators.F2 filter;

		private Filtered(Arbitrary a1, Arbitrary a2, Combinators.F2 filter) {
			super(a1, a2);
			this.filter = filter;
		}

		@Override
		public  Arbitrary as(Combinators.F2 combinator) {
			return new CombineArbitrary<>(Function.identity(), a1, a2)
					   .filter(combineFunction(filter)::apply)
					   .map(combineFunction(combinator));
		}

		@Override
		public Combinators.Combinator2 filter(Combinators.F2 filter) {
			return new Filtered<>(a1, a2, combineFilters(this.filter, filter));
		}

		private Combinators.F2 combineFilters(
			Combinators.F2 first,
			Combinators.F2 second
		) {
			return (t1, t2) -> first.apply(t1, t2) && second.apply(t1, t2);
		}

	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy