groovy
  1. groovy
  2. GROOVY-4164

A groovier way of iterate over dates/calendars: upTo() and downTo()

    Details

    • Type: New Feature New Feature
    • Status: Closed Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.2.0-beta-2
    • Component/s: groovy-jdk
    • Labels:
      None
    • Number of attachments :
      0

      Description

      Iterating over date intervals is currently possible using the following code:

      def start = new GregorianCalendar(2010, Calendar.JANUARY, 1).time
      def end = new GregorianCalendar(2010, Calendar.DECEMBER, 31).time
      
      def current = start
      while (current <= end) {
      	println current
      	current++
      }
      

      But following the style in which numbers are iterated, we can have a groovier code:

      start.upTo(end) {
      	println it
      }
      
      end.downTo(start) {
      	println it
      }
      

      Proposed implementation:

      	Date.metaClass {
      		upTo << { Date end, Closure c ->
      			def next = delegate
      			while (next <= end) {
      				c.call(next)
      				next++
      			}
      		}
      		downTo << { Date start, Closure c ->
      			def next = delegate
      			while (next >= start) {
      				c.call(next)
      				next--
      			}
      		}
      	}
      

        Activity

        Hide
        Alvaro Sanchez-Mariscal added a comment -

        Note that a similar functionality may be added to Calendar using

        next.setTime(next.time + 1)

        instead of

        next++

        and so on.

        Show
        Alvaro Sanchez-Mariscal added a comment - Note that a similar functionality may be added to Calendar using next.setTime(next.time + 1) instead of next++ and so on.
        Hide
        Guillaume Laforge added a comment -

        We would not use EMC for implementing this by the way, we'd implement the feature through a category like mechanism, by adding methods to the DefaultGroovyMethods class.
        Also, as discussed on Twitter together, we should support the range notation as well.
        And this should also work both for Dates and Calendars.

        Show
        Guillaume Laforge added a comment - We would not use EMC for implementing this by the way, we'd implement the feature through a category like mechanism, by adding methods to the DefaultGroovyMethods class. Also, as discussed on Twitter together, we should support the range notation as well. And this should also work both for Dates and Calendars.
        Pascal Schumacher made changes -
        Field Original Value New Value
        Assignee Pascal Schumacher [ pschumacher ]
        Pascal Schumacher made changes -
        Summary A groovier way of iterate over dates: upTo() and downTo() A groovier way of iterate over dates/calendars: upTo() and downTo()
        Hide
        Pascal Schumacher added a comment - - edited

        I submitted a pull request for this.

        Show
        Pascal Schumacher added a comment - - edited I submitted a pull request for this.
        Pascal Schumacher made changes -
        Priority Major [ 3 ] Minor [ 4 ]
        Hide
        Pascal Schumacher added a comment -

        Merged the pull request.

        Show
        Pascal Schumacher added a comment - Merged the pull request.
        Pascal Schumacher made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Fix Version/s 2.2.0-beta-2 [ 19431 ]
        Resolution Fixed [ 1 ]
        Hide
        Alvaro Sanchez-Mariscal added a comment -

        Thanks a lot!

        Show
        Alvaro Sanchez-Mariscal added a comment - Thanks a lot!
        Paul King made changes -
        Status Resolved [ 5 ] Closed [ 6 ]

          People

          • Assignee:
            Pascal Schumacher
            Reporter:
            Alvaro Sanchez-Mariscal
          • Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: