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

org.infinispan.commons.util.concurrent.CompletableFutures Maven / Gradle / Ivy

There is a newer version: 15.1.0.Dev04
Show newest version
package org.infinispan.commons.util.concurrent;

import static java.util.Objects.requireNonNull;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.infinispan.commons.CacheException;

/**
 * Utility methods connecting {@link CompletableFuture} futures.
 *
 * @author Dan Berindei
 * @since 8.0
 */
public final class CompletableFutures {
   public static final CompletableFuture[] EMPTY_ARRAY = new CompletableFuture[0];

   private static final CompletableFuture completedTrueFuture = CompletableFuture.completedFuture(Boolean.TRUE);
   private static final CompletableFuture completedFalseFuture = CompletableFuture.completedFuture(Boolean.FALSE);
   private static final CompletableFuture completedEmptyMapFuture = CompletableFuture.completedFuture(Collections.emptyMap());
   private static final CompletableFuture completedNullFuture = CompletableFuture.completedFuture(null);
   private static final long BIG_DELAY_NANOS = TimeUnit.DAYS.toNanos(1);
   private static final Function TO_NULL = o -> null;
   private static final Function TO_TRUE_FUNCTION = o -> Boolean.TRUE;
   private static final Function identity = t -> t;

   private CompletableFutures() {
   }

   @SuppressWarnings("unchecked")
   public static  CompletableFuture> completedEmptyMap() {
      return (CompletableFuture>) completedEmptyMapFuture;
   }

   @SuppressWarnings("unchecked")
   public static  CompletableFuture completedNull() {
      return (CompletableFuture) completedNullFuture;
   }

   public static CompletableFuture completedTrue() {
      return completedTrueFuture;
   }

   public static CompletableFuture completedFalse() {
      return completedFalseFuture;
   }

   public static CompletionStage booleanStage(boolean trueOrFalse) {
      return trueOrFalse ? completedTrueFuture : completedFalseFuture;
   }

   public static  CompletableFuture> sequence(List> futures) {
      CompletableFuture all = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
      return all.thenApply(v -> futures.stream().map(CompletableFuture::join).collect(Collectors.toList()));
   }

   /**
    * It waits until the {@link CompletableFuture} is completed.
    * 

* It ignore if the {@link CompletableFuture} is completed normally or exceptionally. * * @param future the {@link CompletableFuture} to test. * @param time the timeout. * @param unit the timeout unit. * @return {@code true} if completed, {@code false} if timed out. * @throws InterruptedException if interrupted while waiting. * @throws NullPointerException if {@code future} or {@code unit} is {@code null}. */ public static boolean await(CompletableFuture future, long time, TimeUnit unit) throws InterruptedException { try { requireNonNull(future, "Completable Future must be non-null.").get(time, requireNonNull(unit, "Time Unit must be non-null")); return true; } catch (ExecutionException e) { return true; } catch (java.util.concurrent.TimeoutException e) { return false; } } /** * Same as {@link #await(CompletableFuture, long, TimeUnit)} but wraps checked exceptions in {@link CacheException} * * @param future the {@link CompletableFuture} to test. * @param time the timeout. * @param unit the timeout unit. * @return {@code true} if completed, {@code false} if timed out. * @throws NullPointerException if {@code future} or {@code unit} is {@code null}. */ public static boolean uncheckedAwait(CompletableFuture future, long time, TimeUnit unit) { try { requireNonNull(future, "Completable Future must be non-null.").get(time, requireNonNull(unit, "Time Unit must be non-null")); return true; } catch (ExecutionException e) { return true; } catch (java.util.concurrent.TimeoutException e) { return false; } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new CacheException(e); } } /** * Wait for a long time until the {@link CompletableFuture} is completed. * * @param future the {@link CompletableFuture}. * @param the return type. * @return the result value. * @throws ExecutionException if the {@link CompletableFuture} completed exceptionally. * @throws InterruptedException if the current thread was interrupted while waiting. */ public static T await(CompletableFuture future) throws ExecutionException, InterruptedException { try { return Objects.requireNonNull(future, "Completable Future must be non-null.").get(BIG_DELAY_NANOS, TimeUnit.NANOSECONDS); } catch (java.util.concurrent.TimeoutException e) { throw new IllegalStateException("This should never happen!", e); } } /** * Same as {@link #await(CompletableFuture)} but wraps checked exceptions in {@link CacheException} * * @param future the {@link CompletableFuture}. * @param the return type. * @return the result value. */ public static T uncheckedAwait(CompletableFuture future) { try { return Objects.requireNonNull(future, "Completable Future must be non-null.").get(BIG_DELAY_NANOS, TimeUnit.NANOSECONDS); } catch (java.util.concurrent.TimeoutException e) { throw new IllegalStateException("This should never happen!", e); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new CacheException(e); } catch (ExecutionException e) { throw new CacheException(e); } } public static CompletionException asCompletionException(Throwable t) { if (t instanceof CompletionException) { return ((CompletionException) t); } else { return new CompletionException(t); } } public static void rethrowExceptionIfPresent(Throwable t) { if (t != null) { throw asCompletionException(t); } } public static Throwable extractException(Throwable t) { if (t == null) return null; Throwable cause = t.getCause(); if (cause != null && t instanceof CompletionException) { return cause; } return t; } public static Function toNullFunction() { //noinspection unchecked return (Function) TO_NULL; } public static Function toTrueFunction() { //noinspection unchecked return (Function) TO_TRUE_FUNCTION; } public static Function identity() { //noinspection unchecked return (Function) identity; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy