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

com.jnape.palatable.lambda.semigroup.builtin.Compose Maven / Gradle / Ivy

There is a newer version: 5.5.0
Show newest version
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); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy