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

org.glassfish.enterprise.concurrent.internal.ManagedCompletableFuture Maven / Gradle / Ivy

/*
 * Copyright (c) 2022 Contributors to the Eclipse Foundation
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0, which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * This Source Code may also be made available under the following Secondary
 * Licenses when the conditions for such availability set forth in the
 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
 * version 2 with the GNU Classpath Exception, which is available at
 * https://www.gnu.org/software/classpath/license.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
 */
package org.glassfish.enterprise.concurrent.internal;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

import jakarta.enterprise.concurrent.ManagedExecutorService;

/**
 * Managed CompletableFuture, using the provided ManagedExecutorServiceImpl.
 *
 * @author Petr Aubrecht <[email protected]>
 */
public class ManagedCompletableFuture extends CompletableFuture {

    private final ManagedExecutorService executor;

    public ManagedCompletableFuture(ManagedExecutorService executor) {
        this.executor = executor;
    }

    // default executor is our executor
    @Override
    public Executor defaultExecutor() {
        return executor;
    }

    // this method is used to create a new CompletableFuture, provide the managed one
    @Override
    public  CompletableFuture newIncompleteFuture() {
        return new ManagedCompletableFuture<>(executor);
    }

    @Override
    public  CompletableFuture thenApply(Function fn) {
        return super.thenApply(executor.getContextService().contextualFunction(fn));
    }

    public static  CompletableFuture supplyAsync(Supplier supplier, ManagedExecutorService executor) {
        ManagedCompletableFuture managedFuture = new ManagedCompletableFuture<>(executor);
        executor.execute(() -> {
            try {
                managedFuture.complete(supplier.get());
            } catch (Exception e) {
                managedFuture.completeExceptionally(e);
            }
        });
        return managedFuture;
    }

    @Override
    public  CompletableFuture thenApplyAsync(Function fn) {
        return thenApplyAsync(fn, executor);
    }

    @Override
    public  CompletableFuture thenApplyAsync(Function fn, Executor executor) {
        return super.thenApplyAsync(this.executor.getContextService().contextualFunction(fn), executor);
    }

    @Override
    public  CompletableFuture thenCombineAsync(CompletionStage other, BiFunction fn) {
        return thenCombineAsync(other, executor.getContextService().contextualFunction(fn), executor);
    }

    @Override
    public  CompletableFuture thenCombineAsync(CompletionStage other, BiFunction fn, Executor executor) {
        return super.thenCombineAsync(other, this.executor.getContextService().contextualFunction(fn), executor);
    }

    @Override
    public  CompletableFuture handleAsync(BiFunction fn) {
        // FIXME: implement correctly
        return super.handleAsync(executor.getContextService().contextualFunction(fn));
    }

    @Override
    public  CompletableFuture thenCombine(CompletionStage other, BiFunction fn) {
        return super.thenCombine(other, executor.getContextService().contextualFunction(fn));
    }

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

    @Override
    public  CompletableFuture applyToEitherAsync(CompletionStage other, Function fn, Executor executor) {
        return super.applyToEitherAsync(other, this.executor.getContextService().contextualFunction(fn), executor); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/OverriddenMethodBody
    }

    @Override
    public  CompletableFuture thenAcceptBoth(CompletionStage other, BiConsumer action) {
        return super.thenAcceptBoth(other, executor.getContextService().contextualConsumer(action));
    }

    @Override
    public CompletableFuture exceptionally(Function fn) {
        return super.exceptionally(this.executor.getContextService().contextualFunction(fn));
    }

    public static  CompletableFuture completedFuture(U value, ManagedExecutorService executor) {
        ManagedCompletableFuture future = new ManagedCompletableFuture<>(executor);
        future.complete(value);
        return future;
    }

    public static  CompletionStage completedStage(U value, ManagedExecutorService executor) {
        ManagedCompletableFuture future = new ManagedCompletableFuture<>(executor);
        future.complete(value);
        return future;
    }

    public static  CompletableFuture failedFuture(Throwable ex, ManagedExecutorService executor) {
        return executor.failedFuture(ex);
    }

    public static  CompletionStage failedStage(Throwable ex, ManagedExecutorService executor) {
        return executor.failedStage(ex);
    }

    public static CompletableFuture runAsync(Runnable runnable, ManagedExecutorService executor) {
        return CompletableFuture.runAsync(runnable, executor);
    }

}