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

com.github.basking2.sdsai.itrex.functions.ThreadFunction Maven / Gradle / Ivy

There is a newer version: 1.1.23
Show newest version
package com.github.basking2.sdsai.itrex.functions;

import com.github.basking2.sdsai.itrex.EvaluationContext;
import com.github.basking2.sdsai.itrex.SExprRuntimeException;
import com.github.basking2.sdsai.itrex.iterators.FutureIterator;

import java.util.Iterator;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;

import static com.github.basking2.sdsai.itrex.iterators.Iterators.toIterator;

/**
 * This takes a single iterator and, for each element in that iterator, returns a single element wrapped in a Future.
 *
 * Use join to unwrap the value in that future.
 */
public class ThreadFunction implements FunctionInterface>> {
    private final Executor executor;

    public ThreadFunction(final Executor executor) {
        this.executor= executor;
    }

    @Override
    public Iterator> apply(final Iterator iterator, final EvaluationContext evaluationContext) {
        if (!iterator.hasNext()) {
            throw new SExprRuntimeException("Thread function requires 1 argument that is an iterator or iterable.");
        }

        final Iterator objectIterator = toIterator(iterator.next());

        if (objectIterator == null) {
            throw new SExprRuntimeException("Required first argument to thread function could not be converted to an Iterator.");
        }

        final FutureIterator fi = new FutureIterator(objectIterator, executor);

        return fi;
    }
}