Java Code Examples for java.util.concurrent.CompletionStage#toCompletableFuture()

The following examples show how to use java.util.concurrent.CompletionStage#toCompletableFuture() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
CompletableFuture<V> invokeLoaderImmediately(K key, Object keyContext) {
    List<K> keys = singletonList(key);
    CompletionStage<V> singleLoadCall;
    try {
        Object context = loaderOptions.getBatchLoaderContextProvider().getContext();
        BatchLoaderEnvironment environment = BatchLoaderEnvironment.newBatchLoaderEnvironment()
                .context(context).keyContexts(keys, singletonList(keyContext)).build();
        if (isMapLoader()) {
            singleLoadCall = invokeMapBatchLoader(keys, environment).thenApply(list -> list.get(0));
        } else {
            singleLoadCall = invokeListBatchLoader(keys, environment).thenApply(list -> list.get(0));
        }
        return singleLoadCall.toCompletableFuture();
    } catch (Exception e) {
        return CompletableFutureKit.failedFuture(e);
    }
}
 
Example 2
private <U,V> CompletableFuture<V> biApplyStage(
    Executor e, CompletionStage<U> o,
    BiFunction<? super T,? super U,? extends V> f) {
    CompletableFuture<U> b;
    if (f == null || (b = o.toCompletableFuture()) == null)
        throw new NullPointerException();
    CompletableFuture<V> d = new CompletableFuture<V>();
    if (e != null || !d.biApply(this, b, f, null)) {
        BiApply<T,U,V> c = new BiApply<T,U,V>(e, d, this, b, f);
        bipush(b, c);
        c.tryFire(SYNC);
    }
    return d;
}
 
Example 3
private <U extends T> CompletableFuture<Void> orAcceptStage(
    Executor e, CompletionStage<U> o, Consumer<? super T> f) {
    CompletableFuture<U> b;
    if (f == null || (b = o.toCompletableFuture()) == null)
        throw new NullPointerException();
    CompletableFuture<Void> d = new CompletableFuture<Void>();
    if (e != null || !d.orAccept(this, b, f, null)) {
        OrAccept<T,U> c = new OrAccept<T,U>(e, d, this, b, f);
        orpush(b, c);
        c.tryFire(SYNC);
    }
    return d;
}
 
Example 4
private <U,V> CompletableFuture<V> biApplyStage(
    Executor e, CompletionStage<U> o,
    BiFunction<? super T,? super U,? extends V> f) {
    CompletableFuture<U> b;
    if (f == null || (b = o.toCompletableFuture()) == null)
        throw new NullPointerException();
    CompletableFuture<V> d = new CompletableFuture<V>();
    if (e != null || !d.biApply(this, b, f, null)) {
        BiApply<T,U,V> c = new BiApply<T,U,V>(e, d, this, b, f);
        bipush(b, c);
        c.tryFire(SYNC);
    }
    return d;
}
 
Example 5
private <U extends T,V> CompletableFuture<V> orApplyStage(
    Executor e, CompletionStage<U> o,
    Function<? super T, ? extends V> f) {
    CompletableFuture<U> b;
    if (f == null || (b = o.toCompletableFuture()) == null)
        throw new NullPointerException();
    CompletableFuture<V> d = new CompletableFuture<V>();
    if (e != null || !d.orApply(this, b, f, null)) {
        OrApply<T,U,V> c = new OrApply<T,U,V>(e, d, this, b, f);
        orpush(b, c);
        c.tryFire(SYNC);
    }
    return d;
}
 
Example 6
private <U extends T,V> CompletableFuture<V> orApplyStage(
    Executor e, CompletionStage<U> o,
    Function<? super T, ? extends V> f) {
    CompletableFuture<U> b;
    if (f == null || (b = o.toCompletableFuture()) == null)
        throw new NullPointerException();
    CompletableFuture<V> d = new CompletableFuture<V>();
    if (e != null || !d.orApply(this, b, f, null)) {
        OrApply<T,U,V> c = new OrApply<T,U,V>(e, d, this, b, f);
        orpush(b, c);
        c.tryFire(SYNC);
    }
    return d;
}
 
Example 7
private <U,V> CompletableFuture<V> biApplyStage(
    Executor e, CompletionStage<U> o,
    BiFunction<? super T,? super U,? extends V> f) {
    CompletableFuture<U> b;
    if (f == null || (b = o.toCompletableFuture()) == null)
        throw new NullPointerException();
    CompletableFuture<V> d = new CompletableFuture<V>();
    if (e != null || !d.biApply(this, b, f, null)) {
        BiApply<T,U,V> c = new BiApply<T,U,V>(e, d, this, b, f);
        bipush(b, c);
        c.tryFire(SYNC);
    }
    return d;
}
 
Example 8
private CompletableFuture<Void> orRunStage(Executor e, CompletionStage<?> o,
                                           Runnable f) {
    CompletableFuture<?> b;
    if (f == null || (b = o.toCompletableFuture()) == null)
        throw new NullPointerException();
    CompletableFuture<Void> d = new CompletableFuture<Void>();
    if (e != null || !d.orRun(this, b, f, null)) {
        OrRun<T,?> c = new OrRun<>(e, d, this, b, f);
        orpush(b, c);
        c.tryFire(SYNC);
    }
    return d;
}
 
Example 9
Source Project: quarkus   File: VertxHttpRequest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public CompletionStage<Void> executeAsyncIo(CompletionStage<Void> f) {
    // check if this CF is already resolved
    CompletableFuture<Void> ret = f.toCompletableFuture();
    // if it's not resolved, we may need to suspend
    if (!ret.isDone() && !isSuspended()) {
        suspend();
    }
    return ret;
}
 
Example 10
public final boolean exec() {
    CompletableFuture<U> d, fr; U u; Throwable ex;
    if ((d = this.dst) != null && d.result == null) {
        try {
            CompletionStage<U> cs = fn.apply(arg);
            fr = (cs == null) ? null : cs.toCompletableFuture();
            ex = (fr == null) ? new NullPointerException() : null;
        } catch (Throwable rex) {
            ex = rex;
            fr = null;
        }
        if (ex != null)
            u = null;
        else {
            Object r = fr.result;
            if (r == null)
                r = fr.waitingGet(false);
            if (r instanceof AltResult) {
                ex = ((AltResult)r).ex;
                u = null;
            }
            else {
                @SuppressWarnings("unchecked") U ur = (U) r;
                u = ur;
            }
        }
        d.internalComplete(u, ex);
    }
    return true;
}
 
Example 11
private <U extends T> CompletableFuture<Void> orAcceptStage(
    Executor e, CompletionStage<U> o, Consumer<? super T> f) {
    CompletableFuture<U> b;
    if (f == null || (b = o.toCompletableFuture()) == null)
        throw new NullPointerException();
    CompletableFuture<Void> d = new CompletableFuture<Void>();
    if (e != null || !d.orAccept(this, b, f, null)) {
        OrAccept<T,U> c = new OrAccept<T,U>(e, d, this, b, f);
        orpush(b, c);
        c.tryFire(SYNC);
    }
    return d;
}
 
Example 12
@Test
public void toCompletableFutureShouldPassException() throws ExecutionException, InterruptedException {
    CompletionStage<String> completionStage = createCompletionStage(EXCEPTION);

    CompletableFuture<String> completableFuture = completionStage.toCompletableFuture();

    finish(completionStage);

    try {
        completableFuture.get();
    } catch (ExecutionException e) {
        assertThat(e.getCause()).isSameAs(EXCEPTION);
    }
}
 
Example 13
private CompletableFuture<Void> biRunStage(Executor e, CompletionStage<?> o,
                                           Runnable f) {
    CompletableFuture<?> b;
    if (f == null || (b = o.toCompletableFuture()) == null)
        throw new NullPointerException();
    CompletableFuture<Void> d = new CompletableFuture<Void>();
    if (e != null || !d.biRun(this, b, f, null)) {
        BiRun<T,?> c = new BiRun<>(e, d, this, b, f);
        bipush(b, c);
        c.tryFire(SYNC);
    }
    return d;
}
 
Example 14
private CompletableFuture<Void> biRunStage(Executor e, CompletionStage<?> o,
                                           Runnable f) {
    CompletableFuture<?> b;
    if (f == null || (b = o.toCompletableFuture()) == null)
        throw new NullPointerException();
    CompletableFuture<Void> d = new CompletableFuture<Void>();
    if (e != null || !d.biRun(this, b, f, null)) {
        BiRun<T,?> c = new BiRun<>(e, d, this, b, f);
        bipush(b, c);
        c.tryFire(SYNC);
    }
    return d;
}
 
Example 15
private CompletableFuture<Void> biRunStage(Executor e, CompletionStage<?> o,
                                           Runnable f) {
    CompletableFuture<?> b;
    if (f == null || (b = o.toCompletableFuture()) == null)
        throw new NullPointerException();
    CompletableFuture<Void> d = new CompletableFuture<Void>();
    if (e != null || !d.biRun(this, b, f, null)) {
        BiRun<T,?> c = new BiRun<>(e, d, this, b, f);
        bipush(b, c);
        c.tryFire(SYNC);
    }
    return d;
}
 
Example 16
private <U> CompletableFuture<Void> biAcceptStage(
    Executor e, CompletionStage<U> o,
    BiConsumer<? super T,? super U> f) {
    CompletableFuture<U> b;
    if (f == null || (b = o.toCompletableFuture()) == null)
        throw new NullPointerException();
    CompletableFuture<Void> d = new CompletableFuture<Void>();
    if (e != null || !d.biAccept(this, b, f, null)) {
        BiAccept<T,U> c = new BiAccept<T,U>(e, d, this, b, f);
        bipush(b, c);
        c.tryFire(SYNC);
    }
    return d;
}
 
Example 17
private <U,V> CompletableFuture<V> biApplyStage(
    Executor e, CompletionStage<U> o,
    BiFunction<? super T,? super U,? extends V> f) {
    CompletableFuture<U> b;
    if (f == null || (b = o.toCompletableFuture()) == null)
        throw new NullPointerException();
    CompletableFuture<V> d = new CompletableFuture<V>();
    if (e != null || !d.biApply(this, b, f, null)) {
        BiApply<T,U,V> c = new BiApply<T,U,V>(e, d, this, b, f);
        bipush(b, c);
        c.tryFire(SYNC);
    }
    return d;
}
 
Example 18
Source Project: Java8CN   File: CompletableFuture.java    License: Apache License 2.0 5 votes vote down vote up
private CompletableFuture<Void> biRunStage(Executor e, CompletionStage<?> o,
                                           Runnable f) {
    CompletableFuture<?> b;
    if (f == null || (b = o.toCompletableFuture()) == null)
        throw new NullPointerException();
    CompletableFuture<Void> d = new CompletableFuture<Void>();
    if (e != null || !d.biRun(this, b, f, null)) {
        BiRun<T,?> c = new BiRun<>(e, d, this, b, f);
        bipush(b, c);
        c.tryFire(SYNC);
    }
    return d;
}
 
Example 19
public final void run() {
    final CompletableFuture<? extends T> a;
    final Function<? super T, ? extends CompletionStage<U>> fn;
    final CompletableFuture<U> dst;
    Object r; T t; Throwable ex; Executor e;
    if ((dst = this.dst) != null &&
        (fn = this.fn) != null &&
        (a = this.src) != null &&
        (r = a.result) != null &&
        compareAndSet(0, 1)) {
        if (r instanceof AltResult) {
            ex = ((AltResult)r).ex;
            t = null;
        }
        else {
            ex = null;
            @SuppressWarnings("unchecked") T tr = (T) r;
            t = tr;
        }
        CompletableFuture<U> c = null;
        U u = null;
        boolean complete = false;
        if (ex == null) {
            if ((e = executor) != null)
                execAsync(e, new AsyncCompose<T,U>(t, fn, dst));
            else {
                try {
                    CompletionStage<U> cs = fn.apply(t);
                    c = (cs == null) ? null : cs.toCompletableFuture();
                    if (c == null)
                        ex = new NullPointerException();
                } catch (Throwable rex) {
                    ex = rex;
                }
            }
        }
        if (c != null) {
            ThenCopy<U> d = null;
            Object s;
            if ((s = c.result) == null) {
                CompletionNode p = new CompletionNode
                    (d = new ThenCopy<U>(c, dst));
                while ((s = c.result) == null) {
                    if (UNSAFE.compareAndSwapObject
                        (c, COMPLETIONS, p.next = c.completions, p))
                        break;
                }
            }
            if (s != null && (d == null || d.compareAndSet(0, 1))) {
                complete = true;
                if (s instanceof AltResult) {
                    ex = ((AltResult)s).ex;  // no rewrap
                    u = null;
                }
                else {
                    @SuppressWarnings("unchecked") U us = (U) s;
                    u = us;
                }
            }
        }
        if (complete || ex != null)
            dst.internalComplete(u, ex);
        if (c != null)
            c.helpPostComplete();
    }
}
 
Example 20
/**
 * Create a new adapter for the given {@link CompletionStage}.
 * @since 4.3.7
 */
public CompletableToListenableFutureAdapter(CompletionStage<T> completionStage) {
	this(completionStage.toCompletableFuture());
}