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

com.github.basking2.sdsai.itrex.functions.ListFunction 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 java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * A function that materializes an iterator into a list and returns it.
 *
 * This is useful in situation where you want to set a variable using the "set" function
 * to return a list.
 *
 * Also, sometimes you want to defer evaluation of a list. In such situations you can make the first
 * element be the "list" function which simply returns the following arguments.
 */
public class ListFunction implements FunctionInterface> {
    @Override
    public List apply(Iterator objectIterator, final EvaluationContext evaluationContext) {
        final List list = new ArrayList<>();

        while (objectIterator.hasNext()) {
            list.add(objectIterator.next());
        }

        return list;
    }
}