Closed lukaseder closed 8 years ago
A quick fix will simply not estimate sizes and remove all characteristics (similar to when transforming an Iterator
into a Spliterator
). We can still optimise later - correctness is more important right now.
(thanks again @amaembo!)
At least preserve ORDERED
. Probably it's not so important in your case, but StreamSupport.stream(Seq.of(1,2,3).cycle(3).spliterator(), true).limit(1).forEach(System.out::println)
may unexpectedly work incorrectly after this. Use simply delegate.characteristics() & ORDERED
.
Huh, good point. I hadn't thought about preserving the flag on presence. I suspect that similar preservations could be implemented for NONNULL
and IMMUTABLE
(in most cases)
@amaembo: Hmm, judging from the JDK source code, this ORDERED
flag is currently used only for parallel stream processing (unless I'm overlooking something), so it doesn't change much for jOOλ - at least not in JDK 8
IMMUTABLE
and CONCURRENT
could also be preserved, though they do not affect the behavior of standard Stream API. Keeping NONNULL
could be incorrect if you change the elements somehow (for example, perform mapping). It also currently not used by standard Stream API, but may be used by other third-party libs for some optimizations (including StreamEx ;-)). Something like StreamEx.of(Seq.of("a", null, "b").cycle(3)).parallel().distinct(2).toList()
may fail if you incorrectly report NONNULL
as parallel distinct(n)
cuts edges for non-null source. So better not to report it if you're not 100% sure.
Right, ORDERED
is only for parallel processing (at least now). However it's possible to create parallel stream from your Seq
using StreamSupport.stream(Seq.something().spliterator(), true)
.
StreamEx.of(Seq.of("a", null, "b").cycle(3)).parallel().distinct(2).toList()
That looks nifty! :)
So better not to report it if you're not 100% sure.
Exactly my thinking. Indeed, I haven't fully understood all of Spliterator
's SPI features. Specifically this one here (the original one) with the Comparator
seems a bit obscure to me.
StreamSupport.stream(Seq.something().spliterator(), true)
I cannot keep people from shooting themselves in their feet :)
The following usage:
Results in:
As reported by @amaembo: https://github.com/jOOQ/jOOL/issues/189#issuecomment-190124632
The reason for this is that our
Spliterator
implementation delegatesestimateSize()
andcharacteristics()
to the delegate spliterator, which isn't correct in general. For instance, in the event ofcycle(3)
, we must not haveSpliterator.SIZED
along with a size estimate of3
, because that would allocate an internal array of length 3, which is too small when we really need 9 values.