Class Stream<T>

  • Type Parameters:
    T - The type of objects in the stream.
    All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, java.lang.Iterable<T>

    public final class Stream<T>
    extends java.lang.Object
    implements java.lang.Iterable<T>, java.io.Closeable
    The Stream class implements Iterable to provide a simple mechanism for reading and parsing objects of a given type from data streamed via a Reader using a specified delimiter.

    Stream assumes that data is being pushed to the provided Reader asynchronously and utilizes a Scanner to block during iteration if the next object is not available. Iterable stream for parsing JSON and Server-Sent Events (SSE) data. Supports both newline-delimited JSON and SSE with optional stream termination.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Stream.StreamType  
    • Constructor Summary

      Constructors 
      Constructor Description
      Stream​(java.lang.Class<T> valueType, java.io.Reader reader, java.lang.String delimiter)
      Constructs a new Stream with the specified value type, reader, and delimiter.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      static <T> Stream<T> fromJson​(java.lang.Class<T> valueType, java.io.Reader reader)  
      static <T> Stream<T> fromJson​(java.lang.Class<T> valueType, java.io.Reader reader, java.lang.String delimiter)  
      static <T> Stream<T> fromSse​(java.lang.Class<T> valueType, java.io.Reader sseReader)  
      static <T> Stream<T> fromSse​(java.lang.Class<T> valueType, java.io.Reader sseReader, java.lang.String streamTerminator)  
      java.util.Iterator<T> iterator()
      Returns an iterator over the elements in this stream that blocks during iteration when the next object is not yet available.
      • Methods inherited from class java.lang.Object

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

        forEach, spliterator
    • Constructor Detail

      • Stream

        public Stream​(java.lang.Class<T> valueType,
                      java.io.Reader reader,
                      java.lang.String delimiter)
        Constructs a new Stream with the specified value type, reader, and delimiter.
        Parameters:
        valueType - The class of the objects in the stream.
        reader - The reader that provides the streamed data.
        delimiter - The delimiter used to separate elements in the stream.
    • Method Detail

      • fromJson

        public static <T> Stream<T> fromJson​(java.lang.Class<T> valueType,
                                             java.io.Reader reader,
                                             java.lang.String delimiter)
      • fromJson

        public static <T> Stream<T> fromJson​(java.lang.Class<T> valueType,
                                             java.io.Reader reader)
      • fromSse

        public static <T> Stream<T> fromSse​(java.lang.Class<T> valueType,
                                            java.io.Reader sseReader)
      • fromSse

        public static <T> Stream<T> fromSse​(java.lang.Class<T> valueType,
                                            java.io.Reader sseReader,
                                            java.lang.String streamTerminator)
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException
      • iterator

        public java.util.Iterator<T> iterator()
        Returns an iterator over the elements in this stream that blocks during iteration when the next object is not yet available.
        Specified by:
        iterator in interface java.lang.Iterable<T>
        Returns:
        An iterator that can be used to traverse the elements in the stream.