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

net.javacrumbs.futureconverter.java8guava.FutureConverter Maven / Gradle / Ivy

/**
 * Copyright 2009-2015 the original author or authors.
 *
 * 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 net.javacrumbs.futureconverter.java8guava;


import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;

import java.util.concurrent.CompletableFuture;

/**
 * Converts between {@link java.util.concurrent.CompletableFuture} and Guava {@link com.google.common.util.concurrent.ListenableFuture}.
 */
public class FutureConverter {

    /**
     * Converts {@link java.util.concurrent.CompletableFuture} to {@link com.google.common.util.concurrent.ListenableFuture}.
     *
     * @param completableFuture
     * @param 
     * @return
     */
    public static  ListenableFuture toListenableFuture(CompletableFuture completableFuture) {
        if (completableFuture instanceof CompletableListenableFuture) {
            return ((CompletableListenableFuture) completableFuture).getListenableFuture();
        } else {
            return new ListenableCompletableFutureWrapper<>(completableFuture);
        }
    }

    /**
     * Converts  {@link com.google.common.util.concurrent.ListenableFuture} to {@link java.util.concurrent.CompletableFuture}.
     *
     * @param listenableFuture
     * @param 
     * @return
     */
    public static  CompletableFuture toCompletableFuture(ListenableFuture listenableFuture) {
        if (listenableFuture instanceof ListenableCompletableFutureWrapper) {
            return ((ListenableCompletableFutureWrapper) listenableFuture).getWrappedFuture();
        } else {
            return buildCompletableFutureFromListenableFuture(listenableFuture);
        }
    }

    private static  CompletableFuture buildCompletableFutureFromListenableFuture(final ListenableFuture listenableFuture) {
        CompletableFuture completable = new CompletableListenableFuture(listenableFuture);
        Futures.addCallback(listenableFuture, new FutureCallback() {
            @Override
            public void onSuccess(T result) {
                completable.complete(result);
            }

            @Override
            public void onFailure(Throwable t) {
                completable.completeExceptionally(t);
            }
        }, MoreExecutors.directExecutor());
        return completable;
    }

    private static final class CompletableListenableFuture extends CompletableFuture {
        private final ListenableFuture listenableFuture;

        public CompletableListenableFuture(ListenableFuture listenableFuture) {
            this.listenableFuture = listenableFuture;
        }

        @Override
        public boolean cancel(boolean mayInterruptIfRunning) {
            boolean result = listenableFuture.cancel(mayInterruptIfRunning);
            super.cancel(mayInterruptIfRunning);
            return result;
        }

        public ListenableFuture getListenableFuture() {
            return listenableFuture;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy