Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-8271

Take/TakeRight methods on iterator needlessly calls hasNext for one too many elements

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.4.12
    • Fix Version/s: 2.4.13
    • Component/s: groovy-jdk
    • Labels:
      None

      Description

      I expect the TakeIterator to only call the hasNext() method for the delegate a maximum of N times where N is the number of elements to take. If the iterator represents some expensive operations, or an unexhausted infinite stream that has exactly N elements available, the TakeIterator will block waiting for the Nth+1 element only to return N of them.

      In short, change the TakeIterator's hasNext function from

      return delegate.hasNext() && num > 0;
      to

      return num > 0 && delegate.hasNext();

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                paulk Paul King
                Reporter:
                cheeseheadtothe Marty Neal
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: