Using BaseStream.isParallel() after calling terminal operation

Tagir F. Valeev amaembo at
Sun Feb 7 08:45:35 UTC 2016


Currently the isParallel() spec says [1]:

> Calling this method after invoking an terminal stream operation
> method may yield unpredictable results.

The spliterator() spec says [2]:

> This is a terminal operation.

As a consequence, we cannot legally call isParallel() after calling
spliterator(). However it's actually called in Stream.concat()
implementation [3] as well as in Stream.takeWhile()/dropWhile()
default implementations [4], [5]. The same is for primitive streams.
In all of these cases (especially in concat case) it's possible that
third-party Stream interface implementation is passed, which,
according to spec, can legally return garbage or even throw
IllegalStateException upon calling isParallel(). So to my
understanding, either spec or implementations should be corrected to
match each other.

My suggestion is to make exceptions for calling isParallel() after
spliterator() or iterator() terminal operation: isParallel() should
still return correct value. Though probably it should be additionally
specified what is considered as correct value in this case.

What do you think? Should we bother about such small thing?

With best regards,
Tagir Valeev


More information about the core-libs-dev mailing list