com.jnape.palatable.lambda.semigroup.builtin.Compose Maven / Gradle / Ivy
Show all versions of lambda Show documentation
package com.jnape.palatable.lambda.semigroup.builtin;
import com.jnape.palatable.lambda.functions.Fn1;
import com.jnape.palatable.lambda.functions.specialized.SemigroupFactory;
import com.jnape.palatable.lambda.monoid.Monoid;
import com.jnape.palatable.lambda.semigroup.Semigroup;
import java.util.concurrent.CompletableFuture;
/**
* A {@link Semigroup} instance formed by {@link CompletableFuture}<A>
and a semigroup over
* A
. If either {@link CompletableFuture}s completes exceptionally, the result is also an exceptionally
* completed future.
*
* Note that this operation only takes as long as the slowest future to complete.
*
* For the {@link Monoid}, see {@link com.jnape.palatable.lambda.monoid.builtin.Compose}.
*
* @param the future parameter type
*/
public final class Compose implements SemigroupFactory, CompletableFuture> {
private static final Compose> INSTANCE = new Compose<>();
private Compose() {
}
@Override
public Semigroup> checkedApply(Semigroup aSemigroup) {
return (futureX, futureY) -> futureX.thenCompose(x -> futureY.thenApply(y -> aSemigroup.apply(x, y)));
}
@SuppressWarnings("unchecked")
public static Compose compose() {
return (Compose) INSTANCE;
}
public static Semigroup> compose(Semigroup aSemigroup) {
return Compose.compose().apply(aSemigroup);
}
public static Fn1, CompletableFuture> compose(Semigroup aSemigroup,
CompletableFuture x) {
return compose(aSemigroup).apply(x);
}
public static CompletableFuture compose(Semigroup aSemigroup,
CompletableFuture x,
CompletableFuture y) {
return compose(aSemigroup, x).apply(y);
}
}