package com.voodoodyne.gstrap.gae.taskqueue;

import com.google.appengine.api.taskqueue.QueueFactory;
import lombok.extern.slf4j.Slf4j;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Utilitiy method for dealing with queues
 */
@Slf4j
public class Queues
{
	/** 'default' is a java keyword */
	public static QueueHelper deflt() {
		return new QueueHelper(QueueFactory.getDefaultQueue());
	}

	/**
	 */
	public static QueueHelper named(final String queueName) {
		return new QueueHelper(queueName);
	}

	/**
	 * Add the tasks to their default queues in the most optimum way possible.
	 */
	public static <T extends GuicyDeferredTask> void add(final Collection<T> tasks) {
		log.debug("Enqueueing {} tasks", tasks.size());

		final Map<QueueHelper, List<T>> byQueue = tasks.stream().collect(Collectors.groupingBy(GuicyDeferredTask::defaultQueue));

		byQueue.forEach(QueueHelper::add);
	}
}

@@ -950,11 +950,522 @@
 add);%0A%09%7D
+%0A%0A%09/**%0A%09 * Add the tasks to their default queues in the most optimum way possible. Add a delay.%0A%09 */%0A%09public static %3CT extends GuicyDeferredTask%3E void add(final Collection%3CT%3E tasks, final long countdownMillis) %7B%0A%09%09log.debug(%22Enqueueing %7B%7D tasks with delay %7B%7D%22, tasks.size(), countdownMillis);%0A%0A%09%09final Map%3CQueueHelper, List%3CT%3E%3E byQueue = tasks.stream().collect(Collectors.groupingBy(GuicyDeferredTask::defaultQueue));%0A%0A%09%09byQueue.forEach((queue, list) -%3E queue.withCountdownMillis(countdownMillis).add(list));%0A%09%7D
 %0A%7D%0A

Add Queues.add(tasks, countdown) method