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

org.redisson.misc.CompletableFutureWrapper Maven / Gradle / Ivy

Go to download

Easy Redis Java client and Real-Time Data Platform. Valkey compatible. Sync/Async/RxJava3/Reactive API. Client side caching. Over 50 Redis based Java objects and services: JCache API, Apache Tomcat, Hibernate, Spring, Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Bloom filter, Scheduler, RPC

There is a newer version: 3.40.2
Show newest version
/**
 * Copyright (c) 2013-2024 Nikita Koksharov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.redisson.misc;

import org.redisson.api.RFuture;

import java.util.concurrent.*;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 *
 *
 * @author Nikita Koksharov
 * @param  value type
 */
public class CompletableFutureWrapper implements RFuture {

    private final CompletableFuture future;
    private CompletableFuture lastFuture;

    public CompletableFutureWrapper(V value) {
        this(CompletableFuture.completedFuture(value));
    }

    public CompletableFutureWrapper(Throwable ex) {
        this(new CompletableFuture<>());
        this.future.completeExceptionally(ex);
    }

    public CompletableFutureWrapper(CompletionStage stage) {
        this.future = stage.toCompletableFuture();
        this.lastFuture = future;
    }

    public CompletableFutureWrapper(CompletableFuture future) {
        this.future = future;
        this.lastFuture = future;
    }

    @Override
    public  CompletionStage thenApply(Function fn) {
        return future.thenApply(fn);
    }

    @Override
    public  CompletionStage thenApplyAsync(Function fn) {
        return future.thenApplyAsync(fn);
    }

    @Override
    public  CompletionStage thenApplyAsync(Function fn, Executor executor) {
        return future.thenApplyAsync(fn, executor);
    }

    @Override
    public CompletionStage thenAccept(Consumer action) {
        return future.thenAccept(action);
    }

    @Override
    public CompletionStage thenAcceptAsync(Consumer action) {
        return future.thenAcceptAsync(action);
    }

    @Override
    public CompletionStage thenAcceptAsync(Consumer action, Executor executor) {
        return future.thenAcceptAsync(action, executor);
    }

    @Override
    public CompletionStage thenRun(Runnable action) {
        return future.thenRun(action);
    }

    @Override
    public CompletionStage thenRunAsync(Runnable action) {
        return future.thenRunAsync(action);
    }

    @Override
    public CompletionStage thenRunAsync(Runnable action, Executor executor) {
        return future.thenRunAsync(action, executor);
    }

    @Override
    public  CompletionStage thenCombine(CompletionStage other, BiFunction fn) {
        return future.thenCombine(other, fn);
    }

    @Override
    public  CompletionStage thenCombineAsync(CompletionStage other, BiFunction fn) {
        return future.thenCombineAsync(other, fn);
    }

    @Override
    public  CompletionStage thenCombineAsync(CompletionStage other, BiFunction fn, Executor executor) {
        return future.thenCombineAsync(other, fn, executor);
    }

    @Override
    public  CompletionStage thenAcceptBoth(CompletionStage other, BiConsumer action) {
        return future.thenAcceptBoth(other, action);
    }

    @Override
    public  CompletionStage thenAcceptBothAsync(CompletionStage other, BiConsumer action) {
        return future.thenAcceptBothAsync(other, action);
    }

    @Override
    public  CompletionStage thenAcceptBothAsync(CompletionStage other, BiConsumer action, Executor executor) {
        return future.thenAcceptBothAsync(other, action, executor);
    }

    @Override
    public CompletionStage runAfterBoth(CompletionStage other, Runnable action) {
        return future.runAfterBoth(other, action);
    }

    @Override
    public CompletionStage runAfterBothAsync(CompletionStage other, Runnable action) {
        return future.runAfterBothAsync(other, action);
    }

    @Override
    public CompletionStage runAfterBothAsync(CompletionStage other, Runnable action, Executor executor) {
        return future.runAfterBothAsync(other, action, executor);
    }

    @Override
    public  CompletionStage applyToEither(CompletionStage other, Function fn) {
        return future.applyToEither(other, fn);
    }

    @Override
    public  CompletionStage applyToEitherAsync(CompletionStage other, Function fn) {
        return future.applyToEitherAsync(other, fn);
    }

    @Override
    public  CompletionStage applyToEitherAsync(CompletionStage other, Function fn, Executor executor) {
        return future.applyToEitherAsync(other, fn, executor);
    }

    @Override
    public CompletionStage acceptEither(CompletionStage other, Consumer action) {
        return future.acceptEither(other, action);
    }

    @Override
    public CompletionStage acceptEitherAsync(CompletionStage other, Consumer action) {
        return future.acceptEitherAsync(other, action);
    }

    @Override
    public CompletionStage acceptEitherAsync(CompletionStage other, Consumer action, Executor executor) {
        return future.acceptEitherAsync(other, action, executor);
    }

    @Override
    public CompletionStage runAfterEither(CompletionStage other, Runnable action) {
        return future.runAfterEither(other, action);
    }

    @Override
    public CompletionStage runAfterEitherAsync(CompletionStage other, Runnable action) {
        return future.runAfterEitherAsync(other, action);
    }

    @Override
    public CompletionStage runAfterEitherAsync(CompletionStage other, Runnable action, Executor executor) {
        return future.runAfterEitherAsync(other, action, executor);
    }

    @Override
    public  CompletionStage thenCompose(Function> fn) {
        return future.thenCompose(fn);
    }

    @Override
    public  CompletionStage thenComposeAsync(Function> fn) {
        return future.thenComposeAsync(fn);
    }

    @Override
    public  CompletionStage thenComposeAsync(Function> fn, Executor executor) {
        return future.thenComposeAsync(fn, executor);
    }

    @Override
    public  CompletionStage handle(BiFunction fn) {
        return future.handle(fn);
    }

    @Override
    public  CompletionStage handleAsync(BiFunction fn) {
        return future.handleAsync(fn);
    }

    @Override
    public  CompletionStage handleAsync(BiFunction fn, Executor executor) {
        return future.handleAsync(fn, executor);
    }

    @Override
    public CompletionStage whenComplete(BiConsumer action) {
        return future.whenComplete(action);
    }

    @Override
    public CompletionStage whenCompleteAsync(BiConsumer action) {
        return future.whenCompleteAsync(action);
    }

    @Override
    public CompletionStage whenCompleteAsync(BiConsumer action, Executor executor) {
        return future.whenCompleteAsync(action, executor);
    }

    @Override
    public CompletionStage exceptionally(Function fn) {
        return future.exceptionally(fn);
    }

    @Override
    public CompletableFuture toCompletableFuture() {
        return future;
    }

    public V getNow(V valueIfAbsent) {
        return future.getNow(valueIfAbsent);
    }

    public boolean complete(V value) {
        return future.complete(value);
    }

    @Override
    public boolean cancel(boolean mayInterruptIfRunning) {
        return future.cancel(mayInterruptIfRunning);
    }

    @Override
    public boolean isCancelled() {
        return future.isCancelled();
    }

    @Override
    public boolean isDone() {
        return future.isDone();
    }

    @Override
    public V get() throws InterruptedException, ExecutionException {
        return future.get();
    }

    @Override
    public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
        return future.get(timeout, unit);
    }

    @Override
    public boolean isSuccess() {
        return future.isDone() && !future.isCompletedExceptionally();
    }

    @Override
    public Throwable cause() {
        if (future.isDone()) {
            try {
                future.getNow(null);
            } catch (CompletionException e) {
                return e.getCause();
            } catch (CancellationException e) {
                return e;
            }
        }
        return null;
    }

    @Override
    public V getNow() {
        return future.getNow(null);
    }

    @Override
    public V join() {
        return future.join();
    }

    @Override
    public boolean await(long timeout, TimeUnit unit) throws InterruptedException {
        try {
            future.get(timeout, unit);
        } catch (ExecutionException e) {
            // skip
        } catch (TimeoutException e) {
            return false;
        }
        return true;
    }

    @Override
    public boolean await(long timeoutMillis) throws InterruptedException {
        return await(timeoutMillis, TimeUnit.MILLISECONDS);
    }

    @Override
    public RFuture sync() throws InterruptedException {
        try {
            future.get();
            return this;
        } catch (ExecutionException e) {
            throw (RuntimeException) e.getCause();
        }
    }

    @Override
    public RFuture syncUninterruptibly() {
        try {
            future.join();
            return this;
        } catch (CompletionException e) {
            throw (RuntimeException) e.getCause();
        }
    }

    @Override
    public RFuture await() throws InterruptedException {
        try {
            future.get();
        } catch (ExecutionException e) {
            // skip
        }
        return this;
    }

    @Override
    public RFuture awaitUninterruptibly() {
        try {
            future.join();
        } catch (Exception e) {
            // skip
        }
        return this;
    }

    @Override
    public boolean awaitUninterruptibly(long timeout, TimeUnit unit) {
        try {
            future.get(timeout, unit);
        } catch (ExecutionException | TimeoutException e) {
            // skip
        } catch (InterruptedException e) {
            awaitUninterruptibly(timeout, unit);
        }
        return future.isDone();
    }

    @Override
    public boolean awaitUninterruptibly(long timeoutMillis) {
        return awaitUninterruptibly(timeoutMillis, TimeUnit.MILLISECONDS);
    }

    @Override
    public void onComplete(BiConsumer action) {
        lastFuture = lastFuture.whenComplete(action);
    }

}