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

com.github.basking2.sdsai.itrex.iterators.Iterators Maven / Gradle / Ivy

package com.github.basking2.sdsai.itrex.iterators;

import com.github.basking2.sdsai.itrex.iterators.splitjoin.JoinUncertainIteratorsIterator;
import com.github.basking2.sdsai.itrex.iterators.splitjoin.SplitMapUncertainIterator;

import java.util.Arrays;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.List;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.function.Function;
import java.util.function.Predicate;

public class Iterators {

    public static Iterator EMPTY_ITERATOR = new Iterator() {

        @Override
        public boolean hasNext() {
            return false;
        }

        @Override
        public Object next() {
            throw new NoSuchElementException("Next called on the empty iterator.");
        }
    };

    /**
     * Map iterables and arrays to iterators or returns null.
     * 

* If an iterator is passed it, it is returned. *

* This will not wrap a non-iterable object (or array) into a single element iterator. See {@link #wrap(Object[])} for that. * * @param o An object we would like to try and convert to an {@link Iterator}. * @param The type returned by the {@link Iterator}. * @return An iterator that walks over the elements in o, or null if we cannot convert o to an {@link Iterator}. */ @SuppressWarnings("unchecked") public static Iterator toIterator(final Object o) { if (o instanceof Iterator) { return ((Iterator) o); } if (o instanceof Iterable) { return ((Iterable) o).iterator(); } if (o instanceof Object[]) { return Arrays.asList((T[]) o).iterator(); } return null; } /** * Return true if the object is an iterator or can be made into an iterator via {@link #toIterator(Object)}. * @param o The object to check. * @return true if the object is an iterator or can be made into an iterator via {@link #toIterator(Object)}. */ public static boolean isIter(final Object o) { return (o instanceof Iterator) || (o instanceof Iterable) || (o instanceof Object[]); } /** * Wrap one or more values into an iterator. * * @param ts The t values to wrap into an interator. * @param The type returned by the returned iterator. * @return An interator that will walk through ts values. */ @SafeVarargs public static Iterator wrap(final T... ts) { return new Iterator() { int idx = 0; @Override public boolean hasNext() { return idx < ts.length; } @Override public T next() { try { return ts[idx++]; } catch (final ArrayIndexOutOfBoundsException e) { throw new NoSuchElementException(e.getMessage()); } } }; } /** * Construct and return a {@link NullSkippingIterator}. * * @param iterator The iterator. * @param A type for iterator. * @return A {@link NullSkippingIterator}. */ public static Iterator skipNulls(final Iterator iterator) { return new NullSkippingIterator(iterator); } /** * Construct and return a {@link MappingIterator}. * * @param iterator The iterator that provides the input T values. * @param f The mapping function. * @param The input type. * @param The output type. * @return a {@link MappingIterator}. */ public static MappingIterator mapIterator(final Iterator iterator, final MappingIterator.Mapper f) { return new MappingIterator(iterator, f); } /** * Build an {@link IteratorIterator} that will flatten the given iterator of iterators of T. * * @param iterator An iterator of iterators of type T. * @param The type to iterate. * @return An iterator that has flattened the iterator of iterators. */ public static IteratorIterator flatten(final Iterator> iterator) { return new IteratorIterator(iterator); } /** * Collect the elements of an iterator into a {@link List}. * * @param itr The iterator to materialize. * @param The type of list elements. * @return a list for the iterator. */ public static List toList(final Iterator itr) { final ArrayList list = new ArrayList(); while (itr.hasNext()) { list.add(itr.next()); } return list; } public static FilterIterator filterIterator(final Iterator iterator, final Predicate p) { return new FilterIterator<>(iterator, p); } /** * Build an iterator that will concurrently process elements split from the source iterator. * * @param executorService The executor service. * @param inputs Inputs to be split into many sub-iterations based on the split function. * @param splitFunction Map input types to keys that will group like items together for mapping in separate threads. * @param mapper The mapper that maps sub-iterations in individual tasks in the executor. * @param The result type. * @param The input type. * @param The key type to group iterations by. * * @return An iterator that will manage all the complexities of splitting, mapping, and joining. */ public static Iterator splitMapJoinIterator( final ExecutorService executorService, final Iterator inputs, final Function splitFunction, final MappingIterator.Mapper mapper ) { return JoinUncertainIteratorsIterator.join( executorService, new SplitMapUncertainIterator<>(inputs, splitFunction, mapper) ); } @SuppressWarnings("unchecked") public static Iterator emptyIterator() { return (Iterator) EMPTY_ITERATOR; } }