Skip to content

Package: Futures

Futures

nameinstructionbranchcomplexitylinemethod
completedExceptionally(Throwable)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
lambda$map$2(BiFunction, CompletableFuture, Object, Throwable)
M: 20 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
lambda$mapFailed$1(Function, CompletableFuture, Object, Throwable)
M: 33 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
lambda$reportTo$0(CompletableFuture, Object, Throwable)
M: 12 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
map(CompletionStage, BiFunction)
M: 18 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
mapFailed(CompletionStage, Function)
M: 18 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
reportTo(CompletionStage, CompletableFuture)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2017, 2022 Red Hat Inc and others.
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Red Hat Inc - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.client.gateway.spi.util;
14:
15: import java.util.Objects;
16: import java.util.concurrent.CompletableFuture;
17: import java.util.concurrent.CompletionException;
18: import java.util.concurrent.CompletionStage;
19: import java.util.concurrent.Future;
20: import java.util.function.BiFunction;
21: import java.util.function.Function;
22:
23: /**
24: * Helper methods for working with {@link Future}s or {@link CompletionStage}s
25: */
26: public final class Futures {
27:
28: private Futures() {
29: }
30:
31: public static <T> CompletableFuture<T> completedExceptionally(Throwable error) {
32: final CompletableFuture<T> future = new CompletableFuture<>();
33: future.completeExceptionally(error);
34: return future;
35: }
36:
37: private static <T> CompletionStage<T> reportTo(final CompletionStage<T> source, final CompletableFuture<T> target) {
38: return source.whenComplete((value, error) -> {
39:• if (error != null) {
40: target.completeExceptionally(error);
41: } else {
42: target.complete(value);
43: }
44: });
45: }
46:
47: public static <T> CompletableFuture<T> mapFailed(final CompletionStage<T> stage, final Function<Throwable, CompletionStage<T>> function) {
48:
49: Objects.requireNonNull(stage);
50: Objects.requireNonNull(function);
51:
52: final CompletableFuture<T> future = new CompletableFuture<>();
53:
54: stage.whenComplete((value, error) -> {
55: try {
56:• if (error != null) {
57: final CompletionStage<T> result = function.apply(error);
58:• if (result != null) {
59: reportTo(result, future);
60: } else {
61: future.completeExceptionally(error);
62: }
63: } else {
64: future.complete(value);
65: }
66: } catch (final Exception e) {
67: future.completeExceptionally(new CompletionException(e));
68: }
69: });
70:
71: return future;
72: }
73:
74: public static <T, R> CompletableFuture<R> map(final CompletionStage<T> stage, final BiFunction<T, Throwable, CompletionStage<R>> function) {
75:
76: Objects.requireNonNull(stage);
77: Objects.requireNonNull(function);
78:
79: final CompletableFuture<R> future = new CompletableFuture<>();
80:
81: stage.whenComplete((value, error) -> {
82: try {
83: final CompletionStage<R> result = function.apply(value, error);
84: reportTo(result, future);
85: } catch (final Exception e) {
86: future.completeExceptionally(new CompletionException(e));
87: }
88: });
89:
90: return future;
91: }
92: }