package org.apollo.game.scheduling;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Queue;

import org.apollo.util.CollectionUtil;

/**
 * A class which manages {@link ScheduledTask}s.
 *
 * @author Graham
 */
public final class Scheduler {

	/**
	 * An {@link ArrayDeque} of tasks that are pending execution.
	 */
	private final Queue<ScheduledTask> pendingTasks = new ArrayDeque<>();

	/**
	 * An {@link ArrayList} of currently active tasks.
	 */
	private final List<ScheduledTask> tasks = new ArrayList<>();

	/**
	 * Called every pulse: executes tasks that are still pending, adds new tasks and stops old tasks.
	 */
	public void pulse() {
		CollectionUtil.pollAll(pendingTasks, tasks::add);

		for (Iterator<ScheduledTask> iterator = tasks.iterator(); iterator.hasNext();) {
			ScheduledTask task = iterator.next();
			task.pulse();
			if (task.isRunning()) {
				iterator.remove();
			}
		}
	}

	/**
	 * Schedules a new task.
	 *
	 * @param task The task to schedule.
	 * @return {@code true} if the task was added successfully.
	 */
	public boolean schedule(ScheduledTask task) {
		return pendingTasks.add(task);
	}

}
@@ -325,29 +325,17 @@
 %09 * 
-An %7B@link ArrayDeq
+The Que
 ue
-%7D
  of 
@@ -416,21 +416,16 @@
  pending
-Tasks
  = new A
@@ -453,28 +453,16 @@
 %09 * 
-An %7B@link Array
+The 
 List
-%7D
  of 
@@ -525,21 +525,22 @@
 edTask%3E 
-tasks
+active
  = new A
@@ -567,101 +567,101 @@
 %09 * 
-Called every pulse: executes tasks that are still pending, adds new tasks and stops old tasks
+Pulses the %7B@link Queue%7D of %7B@link ScheduledTask%7Ds, removing those that are no longer running
 .%0A%09 
@@ -722,20 +722,16 @@
 ding
-Tasks, tasks
+, active
 ::ad
@@ -777,21 +777,22 @@
 rator = 
-tasks
+active
 .iterato
@@ -881,15 +881,17 @@
 ();%0A
+%0A
 %09%09%09if (
+!
 task
@@ -1148,13 +1148,8 @@
 ding
-Tasks
 .add

Remove actions at the correct time.