Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should Stream.iteratorStream be deprecated? #304

Open
clinuxrulz opened this issue Nov 9, 2016 · 6 comments
Open

Should Stream.iteratorStream be deprecated? #304

clinuxrulz opened this issue Nov 9, 2016 · 6 comments

Comments

@clinuxrulz
Copy link
Contributor

clinuxrulz commented Nov 9, 2016

The following static method in Stream is unsafe.

public static <A> Stream<A> iteratorStream(final Iterator<A> it)

Due to Stream's use of WeakRef, when you visit the elements of Stream you can get a different result on the 2nd pass. (Internal mutable iterator keeps its state while cache is cleared)

I think this method should be avoided at all times because its behavior is unpredictable.

@jbgi
Copy link
Member

jbgi commented Nov 9, 2016

👍
I am also wondering if the Stream<->java.util.Stream should be subject to the same deprecation (in the java8 module). Will have a look.

@clinuxrulz
Copy link
Contributor Author

Splitterators created with the correct Characteristics should be safe (unsure). Maybe Stream<->java.util.Stream can be made safe, rather than deprecated.

@clinuxrulz
Copy link
Contributor Author

https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html

Down the bottom.

"Consumable. The elements of a stream are only visited once during the life of a stream. Like an Iterator, a new stream must be generated to revisit the same elements of the source."

Java 8 Streams are designed for 1 pass only. So a 2nd pass is a misuse.

@jbgi
Copy link
Member

jbgi commented Nov 9, 2016

The deprecation of iteratorStream also mean the deprecation of iterableStream which also mean deprecating/refactoring api/implementation of big chunks of IterableW, F1Functions and ParModule.
So... kind of a can of worms :(

@clinuxrulz
Copy link
Contributor Author

clinuxrulz commented Nov 9, 2016

I see. It does not need attention yet. Its just something noticed a year ago that caught me out. Thought I make sure its noticed.

Even a warning in the javadoc that iteratorStream, can be used for constructing single pass Streams ONLY. And throw an Exception if a 2nd pass is detected when constructed with that method?

By throwing an Exception, at least we provide Deterministic behavior.

@clinuxrulz
Copy link
Contributor Author

clinuxrulz commented Nov 11, 2016

IterableW avoids this problems as its usage of streams created by iteratorStream are all single pass.

So safety by documentation? Make people aware of limitations of iteratorStream.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants