3

I'm reading Java Stream API source code and I came across the StreamSplitrators class which contains Spliterator implementations for wrapping and delegating spliterators class (one of these class is AbstractWrappingSpliterator). I know that AbstractWrappingSpliterator is a wrapper for spliterator of a pipeline helper on first operation. So the goal of this class is to provide Spliterator<P_OUT> based on the following parameter that it gets in its constructor

  1. PipelineHelper<P_OUT> ph
  2. Supplier<Spliterator<P_IN> spliteratorSupplier

But what I don't understand is:

  1. Why in partial traversal(tryAdvance method) this implementation first put element in the buffer and then read element from that buffer and then consume it, what is the benefit of using buffer here?

  2. Why this class uses buffer for partial traversal (in tryAdvance method) and in complete traversal does not use that buffer and just delegate source spliterator elements to pipelineHelper. wrapAndCopyInto method?

Important note: some of the methods mentioned above are implemented in AbstractWrappingSpliterator's subclasses but the logic is what I described above

(In Java Doc partial traversal refers to using tryAdvance() method and complete traversal refers to using forEachRemaining() method)

Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38
Tashkhisi
  • 2,070
  • 1
  • 7
  • 20
  • 5
    It might have been implemented this way to deal with [this issue](https://stackoverflow.com/q/29229373/2711488), so perhaps, it’s not needed anymore, but has not changed (yet). – Holger Jul 10 '20 at 18:56

0 Answers0