Class TupleSpliterator<T>
- Type Parameters:
T
-
- All Implemented Interfaces:
Spliterator<T[]>
Takes an array of spliterators and returns a spliterator of tuples. Each tuple is built by taking the
next element
of each spliterator.
The iteration stops as soon as there is at least one spliterator which of Spliterator.tryAdvance(Consumer)
method returns false.
Parallelism support is limited: trySplit()
succeeds as long as all the underlining spliterators are able
to return prefixes having all the same size (tails of different size have chances to be managed by the behaviour of
tryAdvance(Consumer)
, by cutting results at the shortest one).
Another restriction is that all base spliterators must be Spliterator.IMMUTABLE
and
non-Spliterator.CONCURRENT
.
See also StreamUtils.tupleStream(boolean, java.util.stream.Stream...)
and
uk.ac.ebi.utils.streams.StreamUtilsTest
for usage examples.
- Author:
- brandizi
- Date:
- 26 Jul 2017
-
Nested Class Summary
Nested classes/interfaces inherited from interface java.util.Spliterator
Spliterator.OfDouble, Spliterator.OfInt, Spliterator.OfLong, Spliterator.OfPrimitive<T extends Object,
T_CONS extends Object, T_SPLITR extends Spliterator.OfPrimitive<T, T_CONS, T_SPLITR>> -
Field Summary
Fields inherited from interface java.util.Spliterator
CONCURRENT, DISTINCT, IMMUTABLE, NONNULL, ORDERED, SIZED, SORTED, SUBSIZED
-
Constructor Summary
ConstructorDescriptionTupleSpliterator
(Spliterator<? extends T>[] spliterators) Initialises with base spliterators. -
Method Summary
Modifier and TypeMethodDescriptionint
As a minimum, this will contain NONNULL | DISTINCT | IMMUTABLE.long
This is the shortest size found in base spliterators.static <TT> TupleSpliterator<TT>
of
(Spliterator<? extends TT>... spliterators) Just a facility to avoid too much generics fiddling.boolean
tryAdvance
(Consumer<? super T[]> action) As explained above, if all the underlining spliterators have an element to return (ie, their tryAdvance()) is invoked to get the element they have to return and the return value checked to be true), a tuple is built with all such elements and then passed to the action parameter.Spliterator<T[]>
trySplit()
A splits succeeds when all the base spliterators return a prefix of the same length.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.Spliterator
forEachRemaining, getComparator, getExactSizeIfKnown, hasCharacteristics
-
Constructor Details
-
TupleSpliterator
Initialises with base spliterators. See above for details. @see alsocharacteristics()
.
-
-
Method Details
-
of
Just a facility to avoid too much generics fiddling. -
tryAdvance
As explained above, if all the underlining spliterators have an element to return (ie, their tryAdvance()) is invoked to get the element they have to return and the return value checked to be true), a tuple is built with all such elements and then passed to the action parameter. If that doens't happen, returns false and the action here is not invoked.- Specified by:
tryAdvance
in interfaceSpliterator<T>
-
trySplit
A splits succeeds when all the base spliterators return a prefix of the same length. If that is the case, the new split result will be a newTupleSpliterator
based on the prefixes returned by the #trySplit() operation invoked upon the base spliterators. This iterator is left with the base iterators it already has, so with its tails. Due to the behaviour oftryAdvance(Consumer)
, such tails can have different sizes, they'll determine a tuple iterator sized like the shortest tail. Examples (assume the splits happen as described)TupleIterator 1: 1, 2, 3, 4, 5, 6 => 1, 2, 3 | 4, 5, 6 a, b, c, d, e, f => a, b, c | d, e, f TupleIterator 2: 1, 2, 3, 4, 5, 6 => 1, 2, 3 | 4, 5, 6 a, b, c, d => a, b, c | d
trySplit() over the second tuple spliterator will return a tuple iterator yielding 3 elements (1a, 2b, 3c) and will leave an original spliterator with one element only (4d).- Specified by:
trySplit
in interfaceSpliterator<T>
-
estimateSize
public long estimateSize()This is the shortest size found in base spliterators. Hence, it will beLong.MAX_VALUE
if all of them return that, including when they are infinite or don't know their count. Having a non-precise result doesn't preventtrySplit()
from splitting the way we requires.- Specified by:
estimateSize
in interfaceSpliterator<T>
-
characteristics
public int characteristics()As a minimum, this will contain NONNULL | DISTINCT | IMMUTABLE. It will never contain SORTED, CONCURRENT. Might contain SIZED, SUBSIZED, ORDERED, if all the base spliterators do. Details to explain this are:- SORTED, we don't currently provide a
Spliterator.getComparator()
and hence this is not set. - DISTINCT, this is set, we return arrays and they're all technically distinct (as per the default
Object.equals(Object)
) - CONCURRENT, is not set, we're IMMUTABLE
- IMMUTABLE, this is set, we expect base spliterators to be immutable too (or that you know what you're doing)
- SIZED, SUBSIZED, ORDERED, are set for the result if all the base spliterators have these flags
- NONNULL is always set for the result, for its items are non-null tuples, which of elements might be null
- Specified by:
characteristics
in interfaceSpliterator<T>
- SORTED, we don't currently provide a
-