I am fairly new to the Groovy arena. I recently modified some code to add the following block. This code is added to remove dates we were supposed to remove from the total counts. This data goes on reports that a bug was noticed, that we weren't removing the queue entry dates.

    def queueEntry = QueueEntry.findById(it.id)<<<START ADD>>>
    Set dates = new HashSet<Long>()

    def children = QueueEntry.findAllByParent(queueEntry)

    for(QueueEntry qe : children){
        def f = new GregorianCalendar()
        def l = new GregorianCalendar()
        while(f < l){
            if(f.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY && f.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY){//only add weekdays
            def xx = new GregorianCalendar()
            f = xx
    } <<<STOP ADD>>>
    Set outsideDays = it.numberOfDaysOutsideCVB
    Set days = DateUtils.businessDaysBetweenDates(it.entryTimestamp, it.exitTimestamp)
    turnTimes << days.size()

The application is now crawling. I am obviously doing something wrong. When this is run for small data-sets it will complete slowly. On larger sets it doesn't finish. Prior to this change it was completing.


2 Answers 2


I found the logic of the set operations a bit hard confusing. It appears that the goal is to take some set of days, named days, and remove entries from it. So, start with days and remove entries from it. You don't need the other sets named outsideDays and dates.

It's weird that it.numberOfDaysOutsideCVB returns a set rather than a number, as suggested by the method name.

The looping mechanism is cumbersome:

def xx = new GregorianCalendar()
f = xx

The l day is always removed, regardless of the day of the week. That deserves an explanatory comment. Furthermore, f and l are somewhat cryptic variable names.

Set days = DateUtils.businessDaysBetweenDates(it.entryTimestamp, it.exitTimestamp)

def children = QueueEntry.findAllByParent(QueueEntry.findById(it.id))
for (QueueEntry qe : children) {
    def entry = new GregorianCalendar()
    def exit = new GregorianCalendar()

    entry.upto(exit.previous()) { day ->
        switch (day.get(Calendar.DAY_OF_WEEK)) {
          case Calendar.SATURDAY:
          case Calendar.SUNDAY:
    // Remove the last day regardless of day of week. (Why?)
turnTimes << days.size()
  • \$\begingroup\$ I was not the original developer of this. This code that I added was pulled from another report. I believe the f is first and l is last. The last or l is removed to take off the last day in the calculation. Thanks for all your help and suggestions! \$\endgroup\$
    – Zirous Tom
    Apr 9, 2015 at 13:08

Here's a way to create a List of Lists, with the inner List containing Calendar instances which include only weekdays within the two timestamps.

BONUS: The code is concurrent so it can process multiple QueueEntries at the same time. That could help with the large data sets.

import static groovyx.gpars.GParsPool.withPool

class QueueEntry {
    Date entryTimestamp
    Date exitTimestamp

// Create some QueueEntr(ies)
def entries = [
    [entryTimestamp: new Date(), exitTimestamp: new Date() + 10],
    [entryTimestamp: new Date() + 5, exitTimestamp: new Date() + 12],
].collect { it as QueueEntry }

// Compute the dates concurrently.
withPool {
        .map { 
        // Convert each entry into a Groovy Range of Calendar(s)
        def start = it.entryTimestamp.clone().clearTime().toCalendar()
        def end = it.exitTimestamp.clone().clearTime().toCalendar()

    }.map {range ->
         * Iterate through all the Calendars in the Range and collect
         * the ones that are weekdays.
        range.findAll {
            !(it[Calendar.DAY_OF_WEEK] in [Calendar.SATURDAY, Calendar.SUNDAY])
    }.reduce { a, b -> [a, b] } // Reduce to a List of List(s).
}.each {dates ->
    // Do as you please with the list of dates (Calendar instances).
    print dates.time 

I think the Calendar Range iteration may not be concurrent. I keep thinking that part can be fine-tuned.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.