package com.map.command;

import java.util.*;
import com.map.command.WaypointCommand;


//TODO - CP - Logging is needed any place that a failure result occurs.

/**
 * @author Chris Park @ Infinetix Corp.
 * Date: 9-14-2020
 * Description: Singleton class used to process and track Waypoint commands. This
 * system allows for Undo/Redo support.
 */
public class CommandManager {
	private static CommandManager cmInstance = null;
	
	private LinkedList<WaypointCommand> processedCommands;
	private LinkedList<WaypointCommand> revertedCommands;
	
	private CommandManager() {
		processedCommands = new LinkedList<WaypointCommand>();
		revertedCommands  = new LinkedList<WaypointCommand>();		
	}
	
	/**
	 * Returns an instance of this singleton class. This is
	 * the intended form of retrieval and instantiation.
	 */
	public static CommandManager getInstance() {
		if(cmInstance == null) {
			cmInstance = new CommandManager();
		}
		
		return cmInstance;
	}
	
	/**
	 * Executes a waypoint command, adds it to the processedCommands
	 * list, and resets the revertedCommands list to maintain command
	 * order continuity.
	 * @return Boolean - Whether or not the operation was successful.
	 */
	public boolean process(WaypointCommand command) {
		boolean result;
		
		result = command.execute();
		
		if(result == true) {
			processedCommands.push(command);
			revertedCommands.clear();
		}
		
		return result;
	}
		
	/**
	 * Pulls the most recently executed waypoint command from the
	 * processedCommands list, tells it to undo itself, and adds that
	 * command to the revertedCommands list.
	 * @return Boolean - Whether or not the operation was successful.
	 */
	public boolean undo() {
		WaypointCommand command;
		boolean result;
		
		if(processedCommands.isEmpty()) {
			return false;
		}
		
		command = processedCommands.pop();
		result = command.undo();
		
		if(result == true) {
			revertedCommands.push(command);
		}
		
		return result;
	}
	
	/**
	 * Pulls the most recently undone waypoint command from the 
	 * revertedCommands list, tells it to re-execute itself, and 
	 * adds it to the processedCommands list
	 * @return Boolean - Whether or not the operation was successful.
	 */
	public boolean redo() {
		WaypointCommand command;
		boolean result;
		
		if(revertedCommands.isEmpty()) {
			return false;
		}
		
		command = revertedCommands.pop();
		result = command.execute();
		
		if(result == true) {
			processedCommands.push(command);
		}
		
		return result;
	}
}

@@ -84,82 +84,8 @@
 d;%0A%0A
-%0A//TODO - CP - Logging is needed any place that a failure result occurs.%0A%0A
 /**%0A
@@ -1226,33 +1226,125 @@
 f(result == 
-true) %7B%0A%09
+false) %7B%0A%09%09%09System.err.println(%22CommandManager - Process command failure.%22);%0A%09%09%09return false;%0A%09%09%7D%0A%09%09%0A
 %09%09processedC
@@ -1368,17 +1368,16 @@
 and);%0A%09%09
-%09
 reverted
@@ -1390,28 +1390,24 @@
 ds.clear();%0A
-%09%09%7D%0A
 %09%09%0A%09%09return 
@@ -1770,32 +1770,120 @@
 ds.isEmpty()) %7B%0A
+%09%09%09System.err.println(%0A%09%09%09%09%09%22CommandManager - Failed undo. processed queue is empty.%22);%0A
 %09%09%09return false;
@@ -2392,24 +2392,111 @@
 sEmpty()) %7B%0A
+%09%09%09System.err.println(%0A%09%09%09%09%09%22CommandManager - Failed redo. reverted queue is empty.%22);%0A
 %09%09%09return fa

Add error reporting for CommandManager