Class StreamIterator<E>

  • Type Parameters:
    E - The stream and iterator type.
    All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, java.util.Iterator<E>

    class StreamIterator<E>
    extends java.lang.Object
    implements java.util.Iterator<E>, java.io.Closeable
    Wraps and presents a stream as a closable iterator resource that automatically closes itself when reaching the end of stream.
    Since:
    2.9.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Iterator<E> iterator  
      private java.util.stream.Stream<E> stream  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private StreamIterator​(java.util.stream.Stream<E> stream)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Closes the underlying stream.
      boolean hasNext()  
      static <T> java.util.Iterator<T> iterator​(java.util.stream.Stream<T> stream)
      Wraps and presents a stream as a closable resource that automatically closes itself when reaching the end of stream.
      E next()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Iterator

        forEachRemaining, remove
    • Field Detail

      • iterator

        private final java.util.Iterator<E> iterator
      • stream

        private final java.util.stream.Stream<E> stream
    • Constructor Detail

      • StreamIterator

        private StreamIterator​(java.util.stream.Stream<E> stream)
    • Method Detail

      • iterator

        public static <T> java.util.Iterator<T> iterator​(java.util.stream.Stream<T> stream)
        Wraps and presents a stream as a closable resource that automatically closes itself when reaching the end of stream.

        Warning

        In order to close the stream, the call site MUST either close the stream it allocated OR call the iterator until the end.

        Type Parameters:
        T - The stream and iterator type.
        Parameters:
        stream - The stream iterate.
        Returns:
        A new iterator.
      • hasNext

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

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

        public void close()
        Closes the underlying stream.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable