Skip to content

Instantly share code, notes, and snippets.

View gszeliga's full-sized avatar

Guillermo Szeliga gszeliga

View GitHub Profile
public static class FutureCompositions<V , A extends WannabeApplicative<V>>{
private final Supplier<CompletableFuture<Outcome<A>>> _partial;
private FutureCompositions(Supplier<CompletableFuture<Outcome<A>>> state)
{
_partial=state;
}
public FutureCompositions<V, A> binding(Partial<A> stage)
//What we're gonna do with the async text when available
Partial<Builder> textToApply =
sources.value(textf)
.by((builder, text) -> builder.flatMapR(b -> text.mapR(b::text)));
//Same thing for the number
Partial<Builder> numberToApply =
sources.value(numberf)
.by((builder, number) -> builder.flatMapR(b -> number.mapR(b::number)));
CompositionSources<Builder> sources = CompositionSources.stickedTo(Builder.class);
public static class CompositionSources<B>
{
private CompositionSources(){ }
public interface Partial<B>
{
CompletableFuture<Outcome<B>> apply(CompletableFuture<Outcome<B>> b);
}
public interface MergingStage<B, V>{
public static class Builder implements WannabeApplicative<Message> {
private String _text;
private Integer _number;
public Builder text(String text){
_text=text;
return this;
}
public static class Message{
private final String _text;
private final Integer _number;
private Message(String msg, Integer number){
_text = msg;
_number = number;
}
CompletableFuture<Outcome<String>> textf =
completedFuture(maybe("And the number is %s!"));
CompletableFuture<Outcome<Integer>> numberf =
completedFuture(maybe(22));
//Acceptance validation on the range of data being requested
acceptableInterval(from, to, DeviceParameters.from(paramId).getResolution())
.fold(
failure -> asyncResponse
.resume(status(422)
.entity(asErrorReply(failure))
.build())
,
accepted -> runAsync(() -> {
Outcome<TariffStructure> tariff =
maybe(ctx.getTariffStructureService()
.flatMap(ts -> ts.get(ctx.getPrices().getTariffId())))
.orIfEmpty(() -> fail(format("Tariff structure with id '%d' could not be found", ctx.getPrices().getTariffId())));
import Either;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.ToString;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;