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

Clarify the specification of iterator and spliterator forEachRemaining

    XMLWordPrintable

    Details

    • Type: CSR
    • Status: Closed
    • Priority: P3
    • Resolution: Approved
    • Fix Version/s: 17
    • Component/s: core-libs
    • Labels:
      None
    • Subcomponent:
    • Compatibility Kind:
      behavioral
    • Compatibility Risk:
      minimal
    • Compatibility Risk Description:
      This is clarifying unspecified behaviour.
    • Interface Kind:
      Java API
    • Scope:
      SE

      Description

      Summary

      Consistently declare unspecified behavior of forEachRemaining on Iterator, Spliterator, and their specializations when an action throws an exception.

      Problem

      The behavior for forEachRemaining is ambiguous when an action throws an exception.

      Solution

      Clarify the behavior for forEachRemaining stating such behavior is unspecified if an action throws an exception.

      Specification

      The specification of forEachRemaining, accepting a primitive functional interface, on the primitive iterators is updated to be:

      /**
       * Performs the given action for each remaining element until all elements
       * have been processed or the action throws an exception.  Actions are
       * performed in the order of iteration, if that order is specified.
       * Exceptions thrown by the action are relayed to the caller.
       * <p>
       * The behavior of an iterator is unspecified if the action modifies the
       * source of elements in any way (even by calling the {@link #remove remove}
       * method or other mutator methods of {@code Iterator} subtypes),
       * unless an overriding class has specified a concurrent modification policy.
       * <p>
       * Subsequent behavior of an iterator is unspecified if the action throws an
       * exception.

      (Note that we use the term "source of elements" rather than "collection", as referred to in Iterator to avoid any association with Collection that cannot directly contain primitives.)

      In addition the specification of tryAdvance and forEachRemaining on Spliterator and the primitive specializations are also updated to include:

       * Subsequent behavior of a spliterator is unspecified if the action throws
       * an exception.

      See PR (https://github.com/openjdk/jdk/pull/4290/files) and attached for code differences.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              psandoz Paul Sandoz
              Reporter:
              dbessono Dmitry Bessonov
              Reviewed By:
              Stuart Marks
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: