All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jgroups.util.CompletableFutures Maven / Gradle / Ivy

Go to download

This artifact provides a single jar that contains all classes required to use remote EJB and JMS, including all dependencies. It is intended for use by those not using maven, maven users should just import the EJB and JMS BOM's instead (shaded JAR's cause lots of problems with maven, as it is very easy to inadvertently end up with different versions on classes on the class path).

The newest version!
package org.jgroups.util;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CompletionStage;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * 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 CompletableFuture TRUE_CF = CompletableFuture.completedFuture(Boolean.TRUE);
    private static final CompletableFuture FALSE_CF = CompletableFuture.completedFuture(Boolean.FALSE);
    private static final Consumer VOID_CONSUMER = o -> {
    };
    private static final Function NULL_FUNCTION = o -> null;

    /**
     * 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; } /** * Wraps the {@code throwable} into {@link CompletionException}. * * @param throwable The {@link Throwable} to wrap. * @return The {@link CompletionException} with {@code throwable}. */ public static CompletionException wrapAsCompletionException(Throwable throwable) { return throwable instanceof CompletionException ? (CompletionException) throwable : new CompletionException(throwable); } /** * @param The value's type. * @return A {@link Function} that converts any value to {@link Void}. */ public static Function toVoidFunction() { //noinspection unchecked return (Function) NULL_FUNCTION; } /** * @return A {@link CompletableFuture} completed with value {@link Boolean#TRUE} */ public static CompletableFuture completedTrue() { return TRUE_CF; } /** * @return A {@link CompletableFuture} completed with value {@link Boolean#FALSE} */ public static CompletableFuture completedFalse() { return FALSE_CF; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy