package cn.axzo.msg.center.utils;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.function.Supplier;

/**
 * @author yanglin
 */
public class AsyncSupplyTasks extends AsyncTasks<Object> {

    private final ExecutorService executor;

    /**
     * use common pool
     */
    public AsyncSupplyTasks() {
        this(null);
    }

    public AsyncSupplyTasks(ExecutorService executor) {
        this.executor = executor;
    }

    public <U> FutureWrapper<U> supplyAsync(Supplier<U> supplier) {
        CompletableFuture<U> future = executor == null
                ? CompletableFuture.supplyAsync(supplier)
                : CompletableFuture.supplyAsync(supplier, executor);
        //noinspection unchecked
        add((CompletableFuture<Object>) future);
        return new FutureWrapper<>(future);
    }

}