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

net.neoforged.art.internal.AsyncHelper Maven / Gradle / Ivy

There is a newer version: 2.0.5
Show newest version
/*
 * Copyright (c) Forge Development LLC and contributors
 * SPDX-License-Identifier: LGPL-2.1-only
 */

package net.neoforged.art.internal;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

class AsyncHelper {
    private final ExecutorService exec;
    AsyncHelper(int threads) {
        if (threads <= 0)
            throw new IllegalArgumentException("Really.. no threads to process things? What do you want me to use a genie?");
        else if (threads == 1)
            exec = Executors.newSingleThreadExecutor();
        else
            exec = Executors.newWorkStealingPool(threads);
    }

    public  void consumeAll(Collection inputs, Function namer, Consumer consumer) {
        Function>> toCallable = i -> new Pair<>(namer.apply(i), () -> {
            consumer.accept(i);
            return null;
        });
        invokeAll(inputs.stream().map(toCallable).collect(Collectors.toList()));
    }

    public  List invokeAll(Collection inputs, Function namer, Function converter) {
        Function>> toCallable = i -> new Pair<>(namer.apply(i), () -> converter.apply(i));
        return invokeAll(inputs.stream().map(toCallable).collect(Collectors.toList()));
    }

    public  List invokeAll(Collection>> tasks) {
            List ret = new ArrayList<>(tasks.size());
            List>> processed = new ArrayList<>(tasks.size());
            for (Pair> task : tasks) {
                processed.add(new Pair<>(task.getLeft(), exec.submit(task.getRight())));
            }
            for (Pair> future : processed) {
                try {
                    O done = future.getRight().get();
                    if (done != null)
                        ret.add(done);
                } catch (InterruptedException | ExecutionException e) {
                    throw new RuntimeException("Failed to execute task " + future.getLeft(), e);
                }
            }
            return ret;
    }

    public void shutdown() {
        exec.shutdown();
    }
}