org.jtrim2.taskgraph.ResultTransformerFactory 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;
final class ResultTransformerFactory implements TaskFactory {
private final ResultTransformer resultTransformer;
public ResultTransformerFactory(
Class dependencyResultType,
Function super R2, ? extends R> resultTransformer) {
this.resultTransformer = new ResultTransformer<>(dependencyResultType, resultTransformer);
}
@Override
public CancelableFunction createTaskNode(
CancellationToken cancelToken,
TaskNodeCreateArgs nodeDef) throws Exception {
TaskInputRef resultRef = resultTransformer.bindInput(nodeDef.nodeKey(), nodeDef.inputs());
return taskCancelToken -> resultRef.consumeInput();
}
private static final class ResultTransformer {
private final Class dependencyResultType;
private final Function super R2, ? extends R> resultTransformer;
public ResultTransformer(
Class dependencyResultType,
Function super R2, ? extends R> resultTransformer) {
this.dependencyResultType = Objects.requireNonNull(dependencyResultType, "dependencyResultType");
this.resultTransformer = Objects.requireNonNull(resultTransformer, "resultTransformer");
}
public TaskInputRef bindInput(TaskNodeKey srcKey, TaskInputBinder inputs) {
TaskNodeKey dependencyKey = srcKey.withResultType(dependencyResultType);
TaskInputRef resultRef = inputs.bindInput(dependencyKey);
return convertResult(resultRef, resultTransformer);
}
private static TaskInputRef convertResult(
TaskInputRef srcRef,
Function super R2, ? extends R> resultTransformer) {
return () -> {
R2 result = srcRef.consumeInput();
return resultTransformer.apply(result);
};
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy