Class StreamCollector<T>

java.lang.Object
org.jdrupes.builder.core.StreamCollector<T>
Type Parameters:
T - the generic type

public class StreamCollector<T> extends Object

A StreamCollector allows the user to combine several Streams into one. The collected streams are terminated when stream() is called. If the collector is cached, stream() can be invoked several times and each invocation returns a new Stream with the collected content.

Note that cached collectors are implemented in a straightforward manner by putting all contents in a list and then returning a stream for the list.

  • Constructor Details

    • StreamCollector

      public StreamCollector(boolean cached)
      Instantiates a new collector.
      Parameters:
      cached - determines if contents is cached
  • Method Details

    • add

      @SafeVarargs public final StreamCollector<T> add(Stream<? extends T>... sources)
      Use all given streams as sources.
      Parameters:
      sources - the sources
      Returns:
      the stream collector
    • add

      @SafeVarargs public final StreamCollector<T> add(T... items)
      Convenience method for adding a enumerated items.
      Parameters:
      items - the item
      Returns:
      the stream collector
    • stream

      public Stream<T> stream()
      Provide the contents from the stream(s).
      Returns:
      the stream<? extends t>
    • cached

      public static <T> StreamCollector<T> cached()
      Create a cached collector.
      Type Parameters:
      T - the generic type
      Returns:
      the cached stream
    • cached

      public static <T> StreamCollector<T> cached(Stream<T> source)
      Create a cached collector initially containing a single source stream.
      Type Parameters:
      T - the generic type
      Parameters:
      source - the source
      Returns:
      the cached stream
    • uncached

      public static <T> StreamCollector<T> uncached()
      Create an un-cached collector.
      Type Parameters:
      T - the generic type
      Returns:
      the cached stream
    • uncached

      public static <T> StreamCollector<T> uncached(Stream<T> source)
      Create an un-cached collector initially containing a single source stream.
      Type Parameters:
      T - the generic type
      Parameters:
      source - the source
      Returns:
      the cached stream