[PATCH] Re: Custom spliterator for Collections.nCopies(n, obj).stream()

Tagir F. Valeev amaembo at gmail.com
Sat Aug 22 09:17:28 UTC 2015


Hello!

PS> With reuse it becomes more compelling :-) In both cases of
PS> singleton/nCopies the spliterator characteristics can be the same
PS> and that of the already existing singleton spliterator implementation.

The only difference is the DISTINCT characteristic. I think it's good
to report it based on the list size, so
Collections.nCopies(1, obj).spliterator() can report DISTINCT as well.

PS> I would be happy to accept a patch (with tests, if existing tests
PS> do not cover this already, i suspect they might but we still need
PS> to check). Have you signed the OCA [1]. If so i can accept a patch
PS> from you and publish as a webrev for review.

Here's my patch to the Collections class. Implementation of the
ConstantSpliterator is added, singletonSpliterator method now uses it
as well as CopiesList::spliterator. CopiesList::stream and
CopiesList::parallelStream methods are removed as unnecessary.

The resulting bytecode is roughly 750 bytes less after applying my
patch.

As for tests, it seems that
test/java/util/Spliterator/SpliteratorTraversingAndSplittingTest.java
covers both singletonSpliterator and nCopies().spliterator() pretty
well. I checked that these tests succeed with my changes while failed
when some mistake in ConstantSpliterator is introduced. If you think
that  more  tests are necessary, please suggest what exactly should be
tested and where to put them.

With best regards,
Tagir Valeev.


More information about the core-libs-dev mailing list