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

org.jtrim2.taskgraph.ResultForwarderFactory Maven / Gradle / Ivy

package org.jtrim2.taskgraph;

import java.util.Objects;
import java.util.function.Function;
import org.jtrim2.cancel.CancellationToken;
import org.jtrim2.executor.CancelableFunction;
import org.jtrim2.executor.SyncTaskExecutor;

final class ResultForwarderFactory implements TaskFactory {
    private final NodeForwarder nodeForwarder;

    public  ResultForwarderFactory(Function, TaskNodeKey> dependencyFactory) {
        this.nodeForwarder = new NodeForwarder<>(dependencyFactory);
    }

    @Override
    public CancelableFunction createTaskNode(
            CancellationToken cancelToken,
            TaskNodeCreateArgs nodeDef) throws Exception {
        nodeDef.properties().setExecutor(SyncTaskExecutor.getSimpleExecutor());

        TaskInputRef resultRef = nodeForwarder.bindInput(nodeDef.nodeKey(), nodeDef.inputs());
        return taskCancelToken -> resultRef.consumeInput();
    }

    private static final class NodeForwarder {
        private final Function, TaskNodeKey> dependencyFactory;

        public NodeForwarder(Function, TaskNodeKey> dependencyFactory) {
            this.dependencyFactory = Objects.requireNonNull(dependencyFactory, "dependencyFactory");
        }

        public TaskInputRef bindInput(TaskNodeKey nodeKey, TaskInputBinder inputs) {
            TaskNodeKey dependencyKey = dependencyFactory.apply(nodeKey);
            return inputs.bindInput(dependencyKey);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy