Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8328316

Finisher cannot emit if stream is sequential and integrator returned false

    XMLWordPrintable

Details

    • b16
    • generic
    • generic

    Backports

      Description

        A DESCRIPTION OF THE PROBLEM :
        When using a gatherer with an integrator that short-circuits (returns false) on a sequential stream, the finisher cannot add elements using downstream.push(element). If the integrator does not short circuit, or the stream is parallel, pushing an element from the finisher succeeds.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        1. Create a gatherer with an integrator that will short circuit in a particular scenario, and with a finisher that pushes an element downstream.
        2. Use the gatherer with a sequential stream that causes it to short circuit
        3. Observe the resulting stream (e.g. by collecting to a list)

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        The element pushed by the finisher should have included in the resulting stream
        ACTUAL -
        The element pushed by the finisher is not in the resulting stream

        ---------- BEGIN SOURCE ----------
        import java.util.stream.Gatherer;
        import java.util.stream.Stream;

        public class ShortCircuitWithFinisher {
            public static void main(String[] args) {
                Gatherer<Integer, Void, Integer> shortCircuit =
                        Gatherer.of((state, element, downstream) -> false,
                                (state, finisher) -> finisher.push(1));
                Gatherer<Integer, Void, Integer> notShortCircuit =
                        Gatherer.of((state, element, downstream) -> true,
                                (state, finisher) -> finisher.push(1));

                System.out.println(" Short-circuit (sequential stream): " +
                        Stream.of(1, 2).gather(shortCircuit).toList());
                System.out.println(" Short-circuit (parallel stream): " +
                        Stream.of(1, 2).parallel().gather(shortCircuit).toList());
                System.out.println("Not short-circuit (sequential stream): " +
                        Stream.of(1, 2).gather(notShortCircuit).toList());
                System.out.println(" Not short-circuit (parallel stream): " +
                        Stream.of(1, 2).parallel().gather(notShortCircuit).toList());

        // Output:
        // Short-circuit (sequential stream): []
        // Short-circuit (parallel stream): [1]
        // Not short-circuit (sequential stream): [1]
        // Not short-circuit (parallel stream): [1]
            }
        }

        ---------- END SOURCE ----------

        FREQUENCY : always


        Attachments

          Issue Links

            Activity

              People

                vklang Viktor Klang
                webbuggrp Webbug Group
                Votes:
                0 Vote for this issue
                Watchers:
                6 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved: