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

es.iti.wakamiti.lsp.FutureUtil Maven / Gradle / Ivy

/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

package es.iti.wakamiti.lsp;

import java.util.concurrent.*;
import java.util.function.*;

import es.iti.wakamiti.api.util.ThrowableFunction;
import org.eclipse.lsp4j.jsonrpc.CompletableFutures;
import org.slf4j.*;





public final class FutureUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(FutureUtil.class);

    private static Executor executor = Executors.newCachedThreadPool();

    public interface ThrowableRunnable {
        void run() throws Exception;
    }

    private FutureUtil() { }


    public static CompletableFuture run(ThrowableRunnable runnable) {
        return CompletableFutures.computeAsync(canceler -> {
            canceler.checkCanceled();
            return runnable;
        });
    }


    public static  CompletableFuture run(ThrowableFunction function, U input) {
        return CompletableFutures.computeAsync(canceler -> {
            canceler.checkCanceled();
            return function.apply(input);
        });
    }



    public static CompletableFuture runDelayed(ThrowableRunnable runnable, int delaySeconds) {
        return CompletableFutures.computeAsync(
            CompletableFuture.delayedExecutor(delaySeconds, TimeUnit.SECONDS),
            canceler -> {
                canceler.checkCanceled();
                return runnable;
            }
        );
    }


    public static void whenDone(Future future, Runnable actionWhenDone) {
        executor.execute(()->{
            while (!future.isDone()) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    LOGGER.error(e.toString(),e);
                }
            }
            actionWhenDone.run();
        });
    }


    public static  CompletableFuture empty() {
        return CompletableFuture.completedFuture(null);
    }



    static   CompletableFuture processEvent(String event, T params, Function method) {
        return CompletableFuture
                .completedFuture(LoggerUtil.logEntry(event, params))
                .thenApply(loggingError(method))
                .thenApply(response -> LoggerUtil.logExit(event, response));

    }



	static   CompletableFuture processEvent(String event, T params, Supplier method) {
        return CompletableFuture
                .completedFuture(LoggerUtil.logEntry(event, params))
                .thenApply(loggingError(x -> method.get()))
                .thenApply(response -> LoggerUtil.logExit(event, response));

    }



    private static  Function loggingError(Function method) {
		return input -> {
			try {
				return method.apply(input);
			} catch (Exception e) {
				LOGGER.error("UNEXPECTED ERROR", e);
				throw e;
			}
		};
	}

}