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

com.github.basking2.sdsai.itrex.functions.FlattenFunction 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.iterators.IteratorIterator;

import java.util.Arrays;
import java.util.Iterator;

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

/**
 * Flatten an iterator of iterators a single layer.
 *
 * That is, if you have an iterator-of-iterator-of-iterators, you will get back an iterator-of-iterators.
 * If you have an iterator-of-iterator-of-ints, you will get back an iterator of ints.
 *
 * Another way to view this is that it takes away the top-level iterator.
 */
public class FlattenFunction implements FunctionInterface> {
    @SuppressWarnings("unchecked")
    @Override
    public Iterator apply(final Iterator iterator, final EvaluationContext evaluationContext) {

        Iterator> itrItrObj = mapIterator(iterator, e -> {
            if (e instanceof Iterator) {
                return (Iterator) e;
            }

            if (e instanceof Iterable) {
                return ((Iterable)e).iterator();
            }

            if (e instanceof Object[]) {
                return Arrays.asList((Object[])e).iterator();
            }

            return Arrays.asList((Object)e).iterator();

        });

        return new IteratorIterator<>(itrItrObj);
    }
}