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

The following examples show how to use java.util.concurrent.CompletionStage#toCompletableFuture() . 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
Source File: DataLoaderHelper.java    From java-dataloader with Apache License 2.0 6 votes vote down vote up
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
Source File: VertxHttpRequest.java    From quarkus with 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 3
Source File: CompletableFuture.java    From Java8CN with 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 4
Source File: CompletableFuture.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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
Source File: CompletableFuture.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
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 6
Source File: CompletableFuture.java    From openjdk-jdk8u with GNU General Public License v2.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 7
Source File: CompletableFuture.java    From TencentKona-8 with GNU General Public License v2.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 8
Source File: CompletableFuture.java    From openjdk-jdk8u-backup with GNU General Public License v2.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 9
Source File: AbstractCompletionStageTest.java    From completion-stage with Apache License 2.0 5 votes vote down vote up
@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 10
Source File: CompletableFuture.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
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 11
Source File: CompletableFuture.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
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 12
Source File: CompletableFuture.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
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 13
Source File: CompletableFuture.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
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 14
Source File: CompletableFuture.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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 15
Source File: CompletableFuture.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
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 16
Source File: CompletableFuture.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
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 17
Source File: CompletableFuture.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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 18
Source File: CompletableFuture.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
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 19
Source File: CompletableFuture.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
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
Source File: CompletableToListenableFutureAdapter.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Create a new adapter for the given {@link CompletionStage}.
 * @since 4.3.7
 */
public CompletableToListenableFutureAdapter(CompletionStage<T> completionStage) {
	this(completionStage.toCompletableFuture());
}