0

What would be the best way to query if a stream has been consumed? Was something akin to Stream::isConsumed ever considered?

I acknowledge this being similar to the following question:

How to check if a Java 8 Stream is empty?

Albeit, the difference seems that checking for stream consumption seems not require partial consumption of said stream. Do the answers of the link above also apply here?

My use case is similar to the following:

/**
   ... This operation ensures, regardless whether the prefix or suffix stream is
   parallel, that the suffix stream is computed only after the prefix stream has
   been exhausted...
 */
public static <E> Stream<E> andThen(
        Stream<? extends E> stream,
        Supplier<? extends Stream<? extends E>> after);

EDIT (4/23/18) showing implementation details were not as important.


In andThen, stream must exhaust before appending the remaining elements produced by after.

Granted, some may prefer writing a Spliterator, but even then if writing trySplit you may want to know if the children split from the prefix spliterator have exhausted their elements (blocking if not...?) before splitting the suffix; you probably would not want to prematurely consume any prefix elements to do so.

Related slightly; probably orthogonal; would a peeking spliterator be useful at all?

abstract boolean tryPeek(Consumer<? super E> action);
abstract boolean skip();
default boolean tryAdvance(Consumer<? super E> action) {return tryPeek(action) && skip();}
bizness86
  • 57
  • 7
  • 2
    I don't think this is valid usage of streams. Firstly, a `Stream` should always be considered infinite if not documented otherwise. Secondly, your `.filter(__ -> buffer.remainingCapacity() == 0)` is not an appropriate filter; the predicate is supposed to check the element of the stream it's filtering, not an external state. – daniu Apr 22 '18 at 08:00
  • To elaborate @daniu’s comment further, your entire code relies on assumptions about the processing order of your functions. The processing order is explicitly specified to be undefined. – Holger Apr 23 '18 at 10:19
  • @Holger could you clarify; so, for each element, `peek` consumer may not apply before the `filter` predicate, not apply before the `map` function? Perhaps, I did this wrong: https://stuartmarks.wordpress.com/2015/01/09/writing-stateful-stream-operations/ – bizness86 Apr 23 '18 at 21:21
  • @bizness86 that article already mentions a consequence of the unspecified evaluation order; in case of a parallel stream, that stateful predicate will pick an arbitrary element if there are multiple elements with the same property value. It says that will do the intended thing with a sequential stream, but it must be emphasized that the specification doesn’t guaranty an evaluation order even for sequential streams (there was such a sentence in pre-release versions but it has been intentionally removed before the final version). – Holger Apr 24 '18 at 06:14
  • @Holger I see; yes, that would be the intended behavior if parallel; I would document accordingly. I did not know that there was no guaranty for sequential streams; My chief concern is computing/appending the remaining elements left in the buffer *only after* the initial stream is consumed. The linked question asked about checking for empty stream, but that possibly required consuming elements. It seems checking for whether a stream is consumed does not need to consume elements; however, there is no way to check on Stream or Spliterator. – bizness86 Apr 24 '18 at 18:15

0 Answers0