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

Try iterating or transforming Object into Collection in DefaultGroovyMethods like count(), sort() and other Collection methods.

    XMLWordPrintableJSON

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.1.1
    • Fix Version/s: 2.2.0-beta-1
    • Component/s: groovy-jdk
    • Labels:

      Description

      Lets say we have following Utils class (XML taken from groovykoans.org):

      class Utils {
      
        static String MOVIES_XML = """<movie-catalog>
          <movie id="6">
              <title>Total Recall</title>
              <year>1990</year>
          </movie>
          <movie id="4">
              <title>The Terminator</title>
              <year>1984</year>
          </movie>
          <movie id="5">
              <title>The Expendables</title>
              <year>2010</year>
          </movie>
          <movie id="1">
              <title>Conan the Barbarian</title>
              <year>1982</year>
          </movie>
          <movie id="3">
              <title>Predator</title>
              <year>1987</year>
          </movie>
          <movie id="2">
              <title>True Lies</title>
              <year>1994</year>
          </movie>
          <movie id="7">
              <title>Kindergarten Cop</title>
              <year>1990</year>
          </movie>
      </movie-catalog>"""
      
        static def getMovies() {
          new XmlSlurper().parseText(Utils.MOVIES_XML).movie
        }
      }
      

      I would like to count movies with id greater than 5.

      This doesn't work (throws MissingMethodException):

      assert Utils.movies.count{it.@id.text() > '5'} == 2
      

      But this does:

      assert Utils.movies.iterator().count{it.@id.text() > '5'} == 2
      

      This works too:

      assert Utils.movies.collect().count{it.@id.text() > '5'} == 2
      

      The same behaviour applies to Utils.movies.sort{it.@id.text()}.
      Also collectEntries, collectMany, sum, join and many other Collection methods have this. Only collect() method works as expected.

      Something like this would be useful in GroovyDefaultMethods:

          public static Number count(Object self, Closure closure) {
              return count(collect(self), closure);
          }
      
          public static List sort(Object self) {
              return sort(collect(self));
          }
      
          public static List sort(Object self, Closure closure) {
              return sort(collect(self), closure);
          }
      
          //etc...
      

      I noticed also that groovy.util.slurpersupport.NodeChildren doesn't implement the Iterable class. Maybe changing

      public abstract class GPathResult extends GroovyObjectSupport implements Writable, Buildable {
      ...
      }
      

      to

      public abstract class GPathResult extends GroovyObjectSupport implements Writable, Buildable, Iterable {
      ...
      }
      

      would solve some problems.

        Attachments

          Activity

            People

            • Assignee:
              paulk Paul King
              Reporter:
              markus.paaso Markus Paaso
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: