66import com .github .lokic .javaplus .func .function .Function5 ;
77import com .github .lokic .javaplus .func .tuple .*;
88import com .github .lokic .javaplus .tuple .*;
9+ import lombok .SneakyThrows ;
910
1011import java .util .List ;
1112import java .util .Map ;
13+ import java .util .concurrent .Callable ;
1214import java .util .concurrent .CompletableFuture ;
15+ import java .util .concurrent .ExecutionException ;
1316import java .util .function .Function ;
17+ import java .util .function .Supplier ;
1418
1519public class CompletableFutures {
1620
1721 public static <T > CompletableFuture <List <T >> sequence (List <CompletableFuture <T >> futures ) {
18- return CompletableFuture .allOf (futures .toArray (new CompletableFuture [futures . size () ]))
22+ return CompletableFuture .allOf (futures .toArray (new CompletableFuture [0 ]))
1923 .thenApply (v -> futures .stream ().map (CompletableFuture ::join ).collect (java .util .stream .Collectors .<T >toList ()));
2024 }
2125
2226 public static <K , V > CompletableFuture <Map <K , V >> sequence (Map <K , CompletableFuture <V >> futures ) {
23- return CompletableFuture .allOf (futures .values ().toArray (new CompletableFuture [futures . size () ]))
27+ return CompletableFuture .allOf (futures .values ().toArray (new CompletableFuture [0 ]))
2428 .thenApply (v -> futures .entrySet ().stream ().collect (java .util .stream .Collectors .toMap (Map .Entry ::getKey , e -> e .getValue ().join ())));
2529 }
2630
31+ public static <T > CompletableFuture <T > supply (Supplier <T > supplier ) {
32+ CompletableFuture <T > future = new CompletableFuture <>();
33+ try {
34+ T data = supplier .get ();
35+ future .complete (data );
36+ } catch (Throwable ex ) {
37+ future .completeExceptionally (ex );
38+ }
39+ return future ;
40+ }
41+
42+ public static <T > CompletableFuture <T > call (Callable <T > callable ) {
43+ CompletableFuture <T > future = new CompletableFuture <>();
44+ try {
45+ T data = callable .call ();
46+ future .complete (data );
47+ } catch (Throwable ex ) {
48+ future .completeExceptionally (ex );
49+ }
50+ return future ;
51+ }
52+
53+
54+ @ SneakyThrows
55+ public static <T > T getOrElseSneakyThrow (CompletableFuture <T > future ) {
56+ try {
57+ return future .get ();
58+ } catch (ExecutionException e ) {
59+ throw e .getCause ();
60+ }
61+ }
62+
63+ public static <T > T getOrElseThrow (CompletableFuture <T > future ) throws Throwable {
64+ return getOrElseThrow (future , Function .identity ());
65+ }
66+
67+ public static <T , X extends Throwable > T getOrElseThrow (CompletableFuture <T > future , Function <? super Throwable , X > exceptionProvider ) throws X {
68+ try {
69+ return future .get ();
70+ } catch (ExecutionException e ) {
71+ throw exceptionProvider .apply (e .getCause ());
72+ } catch (Throwable e ) {
73+ throw exceptionProvider .apply (e );
74+ }
75+ }
76+
2777 public static class Fors {
2878
2979 private Fors () {
@@ -34,7 +84,6 @@ public static <T1, T2> Function<T1, CompletableFuture<Tuple2<T1, T2>>> For(Funct
3484 return t1 -> f .apply (t1 ).thenApply (t2 -> Tuple .of (t1 , t2 ));
3585 }
3686
37-
3887 public static <T1 , T2 , T3 > Function <Tuple2 <T1 , T2 >, CompletableFuture <Tuple3 <T1 , T2 , T3 >>> For (Function2 <T1 , T2 , CompletableFuture <T3 >> f ) {
3988 return t -> f .apply (t .getT1 (), t .getT2 ()).thenApply (t3 -> Tuple .of (t .getT1 (), t .getT2 (), t3 ));
4089 }
@@ -51,7 +100,6 @@ public static <T1, T2, T3, T4, T5, T6> Function<Tuple5<T1, T2, T3, T4, T5>, Comp
51100 return t -> f .apply (t .getT1 (), t .getT2 (), t .getT3 (), t .getT4 (), t .getT5 ()).thenApply (t6 -> Tuple .of (t .getT1 (), t .getT2 (), t .getT3 (), t .getT4 (), t .getT5 (), t6 ));
52101 }
53102
54-
55103 public static <T1 , R > Function <T1 , R > Yield (Function <T1 , R > f ) {
56104 return f ;
57105 }
0 commit comments