Class CloseableIterator<T>

  • Type Parameters:
    T - the type of elements returned by the iterator
    All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, java.util.Iterator<T>

    public abstract class CloseableIterator<T>
    extends CloseableResource<java.util.Iterator<T>>
    implements java.util.Iterator<T>
    A CloseableIterator<T> is an iterator which requires cleanup when it is no longer in use. When the user creates CloseableIterator, the closeResource() method needs to be implemented to define how the resource can be closed. The iterator is not necessarily Closeable so we do not provide a default implementation for closeResource(). Users are required to use the factory methods instead of the constructors to create instances.
    • Method Detail

      • hasNext

        public boolean hasNext()
        Specified by:
        hasNext in interface java.util.Iterator<T>
      • next

        public T next()
        Specified by:
        next in interface java.util.Iterator<T>
      • create

        public static <T> CloseableIterator<T> create​(java.util.Iterator<? extends T> iterator,
                                                      java.util.function.Consumer<java.lang.Void> closeAction)
        Wrap around an iterator with a resource to close. How to close the resource is defined by the caller in the lambda expression.
        Type Parameters:
        T - the type of the iterable
        Parameters:
        iterator - the iterator to wrap around
        closeAction - the callback to properly clean up the resource
        Returns:
        the closeable iterator
      • noopCloseable

        public static <T> CloseableIterator<T> noopCloseable​(java.util.Iterator<? extends T> iterator)
        Wraps around an iterator with no resource to close.
        Type Parameters:
        T - the type of the iterable
        Parameters:
        iterator - the iterator to wrap around
        Returns:
        the closeable iterator
      • concat

        public static <T> CloseableIterator<T> concat​(CloseableIterator<T> a,
                                                      CloseableIterator<T> b)
        Combines two iterators into a single iterator.
        Type Parameters:
        T - type of iterator
        Parameters:
        a - an iterator
        b - another iterator
        Returns:
        the concatenated iterator
      • concat

        public static <T> CloseableIterator<T> concat​(java.util.List<CloseableIterator<T>> iterators)
        Concatenates iterators.
        Type Parameters:
        T - type of iterator
        Parameters:
        iterators - a list of iterators
        Returns:
        a concatenated iterator that iterate over all elements from each of the child iterators
      • size

        public static int size​(CloseableIterator<?> iter)
        Consumes the iterator, closes it, and returns its size.
        Parameters:
        iter - the iter to get the size of
        Returns:
        the size of the iter