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
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.2.0-beta-2
    • Component/s: groovy-jdk
    • Labels:
      None

      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

        Alvaro Sanchez-Mariscal created issue -
        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 Delcroix 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 Delcroix 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 -
        Resolution Fixed [ 1 ]
        Status Open [ 1 ] Resolved [ 5 ]
        Fix Version/s 2.2.0-beta-2 [ 19431 ]
        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 ]
        Mark Thomas made changes -
        Project Import Sun Apr 05 13:32:57 UTC 2015 [ 1428240777691 ]
        Mark Thomas made changes -
        Workflow jira [ 12733107 ] Default workflow, editable Closed status [ 12744943 ]
        Mark Thomas made changes -
        Project Import Mon Apr 06 02:11:23 UTC 2015 [ 1428286283443 ]
        Mark Thomas made changes -
        Workflow jira [ 12967284 ] Default workflow, editable Closed status [ 12975021 ]
        Transition Time In Source Status Execution Times Last Executer Last Execution Date
        Open Open Resolved Resolved
        1207d 3m 1 Pascal Schumacher 01/Aug/13 02:30
        Resolved Resolved Closed Closed
        74d 15h 23m 1 Paul King 14/Oct/13 17:54

          People

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

            Dates

            • Created:
              Updated:
              Resolved:

              Development