⚡ Sangsakala Proxy 🏠 Home
Skip to content

Should Stream.iteratorStream be deprecated? #304

Open

Description

@clinuxrulz

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.

Activity

  1. jbgi commented on Nov 9, 2016

    @jbgi
    Member

    👍
    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.

  2. clinuxrulz commented on Nov 9, 2016

    @clinuxrulz
    ContributorAuthor

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

  3. clinuxrulz commented on Nov 9, 2016

    @clinuxrulz
    ContributorAuthor

    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.

  4. jbgi commented on Nov 9, 2016

    @jbgi
    Member

    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 :(

  5. clinuxrulz commented on Nov 9, 2016

    @clinuxrulz
    ContributorAuthor

    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.

  6. clinuxrulz commented on Nov 11, 2016

    @clinuxrulz
    ContributorAuthor

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions