org.jgroups.util.CompletableFutures Maven / Gradle / Ivy
package org.jgroups.util;
import java.util.concurrent.*;
import java.util.function.Consumer;
/**
* Utility class with {@link CompletableFuture} and {@link CompletionStage} useful methods.
*
* @author Pedro Ruivo
* @since 5.2
*/
public enum CompletableFutures {
INSTANCE;
private static final CompletableFuture> NULL = CompletableFuture.completedFuture(null);
private static final Consumer> VOID_CONSUMER = o -> {
};
/**
* Same as {@link #join(CompletableFuture)} but it receives a {@link CompletionStage} as parameter.
*
* @see #join(CompletableFuture)
*/
public static T join(CompletionStage cs) {
return join(cs.toCompletableFuture());
}
/**
* Waits for the {@link CompletableFuture} to complete.
*
* Any non {@link RuntimeException} thrown is converted to a {@link RuntimeException}.
*
* @param cf The {@link CompletableFuture}
* @param The value type.
* @return The value of the completed {@link CompletableFuture}.
*/
public static T join(CompletableFuture cf) {
try {
return cf.join();
} catch (CompletionException e) {
if (e.getCause() instanceof RuntimeException) {
throw (RuntimeException) e.getCause();
}
throw new RuntimeException(e.getCause());
}
}
/**
* A {@code null} completed {@link CompletableFuture}.
*
* @param The value type.
* @return The {@link CompletableFuture}.
*/
public static CompletableFuture completedNull() {
//noinspection unchecked
return (CompletableFuture) NULL;
}
/**
* Consumes any value and return a {@link Void}.
*
* @param The value type.
* @return The {@link Consumer}.
*/
public static Consumer voidConsumer() {
//noinspection unchecked
return (Consumer) VOID_CONSUMER;
}
}