I read up on spliterators today and implemented one using Spliterators.spliteratorUnknownSize(iterator(), Spliterator.NONNULL). According to spliteratorUnknownSize()'s documentation
The [resulting] spliterator is not late-binding
Being new to spliterators I am wondering why that is the case. If I ensure iterator() to be late-binding, the resulting spliterator should also be, no? spliteratorUnknownSize() is merely creating an IteratorSpliterator which does not bind to the element source yet.
I.e., I would love to understand why the resulting spliterator is not late-binding. Thank you.
According to the javadocs:
"A
Spliteratorthat does not reportIMMUTABLEorCONCURRENTis expected to have a documented policy concerning: when theSpliteratorbinds to the element source; and detection of structural interference of the element source detected after binding. A late-bindingSpliteratorbinds to the source of elements at the point of first traversal, first split, or first query for estimated size, rather than at the time theSpliteratoris created. ASpliteratorthat is not late-binding binds to the source of elements at the point of construction or first invocation of any method. Modifications made to the source prior to binding are reflected when theSpliteratoris traversed. After binding aSpliteratorshould, on a best-effort basis, throwConcurrentModificationExceptionif structural interference is detected. ..."
So if you parse that carefully, late-binding versus non-late-binding is really about when detection of structural interference kicks in.
A Spliterator wrapping an arbitrary iterator cannot guarantee to detect structural interference. It depends on how the Iterator is implemented. And even for Iterators that do detect (or mitigate) structural interference, the Spliterator cannot make any guarantees about when the detection starts; i.e. when the "binding" occurs.
In short, it cannot guarantee true late-binding semantics.
If I ensure
iterator()to be late-binding, the resultingSpliteratorshould also be, no?
The javadocs don't guarantee that.
In practice: it probably should be, though it depends on the implementation of Spliterators. But making such a statement in the javadocs may constrain the implementation of future versions of the Spliterators class and its nested classes in ways that are harmful.
You may disagree with my analysis. However the authors of the javadocs have clearly and deliberately stated that these Spliterators are not late bound. If you think they are wrong about that, raise a Bug Report against the javadocs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With