Details
-
Type:
New Feature
-
Status:
Open
-
Priority:
Major
-
Resolution: Unresolved
-
Affects Version/s: None
-
Fix Version/s: None
-
Component/s: groovy-jdk
-
Labels:None
-
Number of attachments :
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--
}
}
}
Note that a similar functionality may be added to Calendar using
instead of
and so on.