@@ -126,48 +126,11 @@ public static <T> CompletableFuture<T> exceptionallyCompletedFuture(Throwable ex
126126 return result ;
127127 }
128128
129- public static <T > void copyResults (CompletableFuture <T > source , CompletableFuture <T > target ) {
130- source .whenComplete ((o , throwable ) -> {
131- if (throwable != null ) {
132- target .completeExceptionally (throwable );
133- return ;
134- }
135- target .complete (o );
136- });
137- }
138-
139-
140- public static <U , T > CompletableFuture <U > reduce (List <CompletableFuture <T >> values , U initialValue , BiFunction <U , T , U > aggregator ) {
141- CompletableFuture <U > result = new CompletableFuture <>();
142- reduceImpl (values , 0 , initialValue , aggregator , result );
143- return result ;
144- }
145-
146- public static <U , T > CompletableFuture <U > reduce (CompletableFuture <List <T >> values , U initialValue , BiFunction <U , T , U > aggregator ) {
147- return values .thenApply (list -> {
148- U result = initialValue ;
149- for (T value : list ) {
150- result = aggregator .apply (result , value );
151- }
152- return result ;
153- });
154- }
155-
156129 public static <U , T > CompletableFuture <List <U >> flatMap (List <T > inputs , Function <T , CompletableFuture <U >> mapper ) {
157130 List <CompletableFuture <U >> collect = ImmutableKit .map (inputs , mapper );
158131 return Async .each (collect );
159132 }
160133
161- private static <U , T > void reduceImpl (List <CompletableFuture <T >> values , int curIndex , U curValue , BiFunction <U , T , U > aggregator , CompletableFuture <U > result ) {
162- if (curIndex == values .size ()) {
163- result .complete (curValue );
164- return ;
165- }
166- values .get (curIndex ).
167- thenApply (oneValue -> aggregator .apply (curValue , oneValue ))
168- .thenAccept (newValue -> reduceImpl (values , curIndex + 1 , newValue , aggregator , result ));
169- }
170-
171134 public static <U , T > CompletableFuture <List <U >> map (CompletableFuture <List <T >> values , Function <T , U > mapper ) {
172135 return values .thenApply (list -> ImmutableKit .map (list , mapper ));
173136 }
0 commit comments