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

collectEntries accepts one or more entry values for each iteration but not zero (via null)

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • None
    • 4.0.14, 3.0.19
    • groovy-jdk
    • None

    Description

      The collectEntries extension methods accept one entry (as an entry, array, list or map). And it accepts multiple entries (via a map). However, it does not let you return no entry (as null). You can probably return an empty map...

      Similar to findResults, I'd like to be able to conditionally return an entry or entries:

      def map = list.collectEntries { item ->
        if (item ...) {
          def key = ..., val = ...;
          Collections.singletonMap(key, val)
        }
      }
      

      org.codehaus.groovy.runtime.DefaultGroovyMethods#addEntry(Map,Object) could check for null before calling asType(newEntry, Map.Entry.class).

      collectMany has a similar issue. I need to write "if (x) y else Collections.emptyList()" instead of just "if (x) y" in the closure block. A simple null check in org.codehaus.groovy.runtime.DefaultGroovyMethods#collectMany(Iterable,Collection,Closure) could take care of this.

      Attachments

        Activity

          People

            emilles Eric Milles
            emilles Eric Milles
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: