using System;
using System.Collections.Generic;
namespace AutoGroupGenerator
{
///
/// Queue wrapper that executes commands in sequence.
///
public class CommandQueue
{
#region Fields
private readonly Queue m_ProcessingQueue = new Queue();
#endregion
#region Properties
///
/// Gets or sets a title describing the queue.
///
public string Title { get; set; }
///
/// Gets the number of commands remaining in the queue.
///
public int RemainingCommandCount => m_ProcessingQueue.Count;
#endregion
#region Methods
///
/// Initializes an empty command queue.
///
public CommandQueue()
{
}
///
/// Initializes a queue with a single command.
///
/// Action to execute.
/// Description of the action.
public CommandQueue(Action action, string info)
{
AddCommand(action, info);
Title = info;
}
///
/// Removes all queued commands.
///
protected void ClearQueue()
{
m_ProcessingQueue.Clear();
}
///
/// Hook executed before command processing begins.
///
public virtual void PreExecute()
{
}
///
/// Hook executed after command processing ends.
///
public virtual void PostExecute()
{
}
///
/// Executes the next command in the queue.
///
/// The informational label for the executed command.
public string ExecuteNextCommand()
{
var currentUnit = m_ProcessingQueue.Dequeue();
currentUnit.Action.Invoke();
return currentUnit.Info;
}
///
/// Adds a command to the queue.
///
/// Action to execute.
/// Optional description for the action.
public void AddCommand(Action action, string info = null)
{
m_ProcessingQueue.Enqueue(new Command
{
Action = action,
Info = info,
});
}
///
/// Adds a preconstructed command to the queue.
///
/// The command to enqueue.
public void AddCommand(Command command)
{
m_ProcessingQueue.Enqueue(command);
}
#endregion
}
}