Class StreamWrapper<T>

java.lang.Object
org.apache.sis.internal.stream.BaseStreamWrapper<T,Stream<T>>
org.apache.sis.internal.stream.StreamWrapper<T>
Type Parameters:
T - the type of objects contained in the stream, as specified in Stream interface.
All Implemented Interfaces:
AutoCloseable, BaseStream<T,Stream<T>>, Stream<T>
Direct Known Subclasses:
DeferredStream, PaginedStream

public abstract class StreamWrapper<T> extends BaseStreamWrapper<T,Stream<T>> implements Stream<T>
A stream which delegates all operations to another stream. This stream gives to subclasses an opportunity to redirect some operations to optimized implementations.

Note: It is sometimes necessary to defer the creation of the source stream until a terminal operation is invoked. Such deferred source creation is managed by the DeferredStream subclass.

Since:
1.1
Version:
1.1
  • Field Details

    • source

      Stream<T> source
      The source of elements, or null if this StreamWrapper is no longer the active stream. This is initialized at creation time and set to null after it has been determined that all subsequent operations shall be done on a new stream instead of this stream.
      See Also:
  • Constructor Details

    • StreamWrapper

      StreamWrapper()
      Creates a new wrapper with initially no source. The source field should be initialized by subclass constructor.
    • StreamWrapper

      protected StreamWrapper(Stream<T> source)
      Creates a new wrapper for the given stream.
      Parameters:
      source - the stream to wrap.
  • Method Details

    • source

      protected final Stream<T> source()
      Verifies that this stream is still the active one, then returns the source of this stream.
      Specified by:
      source in class BaseStreamWrapper<T,Stream<T>>
      Returns:
      the stream containing actual data.
      Throws:
      IllegalStateException - if this stream is no longer the active stream on which to apply operations.
    • delegate

      protected final Stream<T> delegate()
      Same as source() but marks this stream is inactive. All subsequent operations must be done on the returned stream. This is used by this class for map(…) and flatMap(…) operations. May also be used by subclasses when an operation does not allow further optimizations.
      Returns:
      the stream containing actual data.
      Throws:
      IllegalStateException - if this stream is no longer the active stream on which to apply operations.
    • update

      private Stream<T> update(Stream<T> result)
      Invoked after an intermediate operation for determining which stream is the active one. If this stream is still active, then this method returns this. Otherwise the given stream is returned.
    • parallel

      public Stream<T> parallel()
      Returns an equivalent stream that is parallel.
      Specified by:
      parallel in interface BaseStream<T,Stream<T>>
    • sequential

      public Stream<T> sequential()
      Returns an equivalent stream that is sequential.
      Specified by:
      sequential in interface BaseStream<T,Stream<T>>
    • unordered

      public Stream<T> unordered()
      Returns an equivalent stream that is unordered.
      Specified by:
      unordered in interface BaseStream<T,Stream<T>>
    • filter

      public Stream<T> filter(Predicate<? super T> predicate)
      Returns a stream with elements of this stream that match the given predicate.
      Specified by:
      filter in interface Stream<T>
    • map

      public <R> Stream<R> map(Function<? super T,? extends R> mapper)
      Returns a stream with results of applying the given function to the elements of this stream.
      Specified by:
      map in interface Stream<T>
    • mapToInt

      public IntStream mapToInt(ToIntFunction<? super T> mapper)
      Returns a stream with results of applying the given function to the elements of this stream.
      Specified by:
      mapToInt in interface Stream<T>
    • mapToLong

      public LongStream mapToLong(ToLongFunction<? super T> mapper)
      Returns a stream with results of applying the given function to the elements of this stream.
      Specified by:
      mapToLong in interface Stream<T>
    • mapToDouble

      public DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper)
      Returns a stream with results of applying the given function to the elements of this stream.
      Specified by:
      mapToDouble in interface Stream<T>
    • flatMap

      public <R> Stream<R> flatMap(Function<? super T,? extends Stream<? extends R>> mapper)
      Returns a stream with results of applying the given function to the elements of this stream.
      Specified by:
      flatMap in interface Stream<T>
    • flatMapToInt

      public IntStream flatMapToInt(Function<? super T,? extends IntStream> mapper)
      Returns a stream with results of applying the given function to the elements of this stream.
      Specified by:
      flatMapToInt in interface Stream<T>
    • flatMapToLong

      public LongStream flatMapToLong(Function<? super T,? extends LongStream> mapper)
      Returns a stream with results of applying the given function to the elements of this stream.
      Specified by:
      flatMapToLong in interface Stream<T>
    • flatMapToDouble

      public DoubleStream flatMapToDouble(Function<? super T,? extends DoubleStream> mapper)
      Returns a stream with results of applying the given function to the elements of this stream.
      Specified by:
      flatMapToDouble in interface Stream<T>
    • distinct

      public Stream<T> distinct()
      Returns a stream with distinct elements of this stream.
      Specified by:
      distinct in interface Stream<T>
    • sorted

      public Stream<T> sorted()
      Returns a stream with elements of this stream sorted in natural order.
      Specified by:
      sorted in interface Stream<T>
    • sorted

      public Stream<T> sorted(Comparator<? super T> comparator)
      Returns a stream with elements of this stream sorted using the given comparator.
      Specified by:
      sorted in interface Stream<T>
    • peek

      public Stream<T> peek(Consumer<? super T> action)
      Returns a stream performing the specified action on each element when consumed.
      Specified by:
      peek in interface Stream<T>
    • limit

      public Stream<T> limit(long maxSize)
      Returns a stream with truncated at the given number of elements.
      Specified by:
      limit in interface Stream<T>
    • skip

      public Stream<T> skip(long n)
      Returns a stream discarding the specified number of elements.
      Specified by:
      skip in interface Stream<T>
    • forEach

      public void forEach(Consumer<? super T> action)
      Performs an action for each element of this stream.
      Specified by:
      forEach in interface Stream<T>
    • forEachOrdered

      public void forEachOrdered(Consumer<? super T> action)
      Performs an action for each element of this stream in encounter order.
      Specified by:
      forEachOrdered in interface Stream<T>
    • reduce

      public T reduce(T identity, BinaryOperator<T> accumulator)
      Performs a reduction on the elements of this stream.
      Specified by:
      reduce in interface Stream<T>
    • reduce

      public Optional<T> reduce(BinaryOperator<T> accumulator)
      Performs a reduction on the elements of this stream.
      Specified by:
      reduce in interface Stream<T>
    • reduce

      public <U> U reduce(U identity, BiFunction<U,? super T,U> accumulator, BinaryOperator<U> combiner)
      Performs a reduction on the elements of this stream.
      Specified by:
      reduce in interface Stream<T>
    • collect

      public <R> R collect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner)
      Performs a mutable reduction on the elements of this stream.
      Specified by:
      collect in interface Stream<T>
    • collect

      public <R, A> R collect(Collector<? super T,A,R> collector)
      Performs a mutable reduction on the elements of this stream.
      Specified by:
      collect in interface Stream<T>
    • min

      public Optional<T> min(Comparator<? super T> comparator)
      Returns the minimum element of this stream according to the provided comparator.
      Specified by:
      min in interface Stream<T>
    • max

      public Optional<T> max(Comparator<? super T> comparator)
      Returns the maximum element of this stream according to the provided comparator.
      Specified by:
      max in interface Stream<T>
    • count

      public long count()
      Returns the number of elements in this stream.
      Specified by:
      count in interface Stream<T>
    • anyMatch

      public boolean anyMatch(Predicate<? super T> predicate)
      Returns whether at least one element of this stream matches the provided predicate.
      Specified by:
      anyMatch in interface Stream<T>
    • allMatch

      public boolean allMatch(Predicate<? super T> predicate)
      Returns whether all elements of this stream match the provided predicate.
      Specified by:
      allMatch in interface Stream<T>
    • noneMatch

      public boolean noneMatch(Predicate<? super T> predicate)
      Returns whether none element of this stream match the provided predicate.
      Specified by:
      noneMatch in interface Stream<T>
    • findFirst

      public Optional<T> findFirst()
      Returns the first element of this stream.
      Specified by:
      findFirst in interface Stream<T>
    • findAny

      public Optional<T> findAny()
      Returns any element of this stream.
      Specified by:
      findAny in interface Stream<T>
    • iterator

      public Iterator<T> iterator()
      Returns an iterator for the elements of this stream.
      Specified by:
      iterator in interface BaseStream<T,Stream<T>>
    • spliterator

      public Spliterator<T> spliterator()
      Returns a spliterator for the elements of this stream.
      Specified by:
      spliterator in interface BaseStream<T,Stream<T>>
    • toArray

      public Object[] toArray()
      Returns all elements in an array.
      Specified by:
      toArray in interface Stream<T>
    • toArray

      public <A> A[] toArray(IntFunction<A[]> generator)
      Returns all elements in an array.
      Specified by:
      toArray in interface Stream<T>
    • onClose

      public Stream<T> onClose(Runnable closeHandler)
      Returns an equivalent stream with an additional close handler.
      Specified by:
      onClose in interface BaseStream<T,Stream<T>>