using System;
using System35;
using JetBrains.Annotations;
using JetBrains.Omea.OpenAPI;
namespace JetBrains.Omea.AsyncProcessing
{
///
/// An job that executes a single delegate.
///
public class ActionJob : AbstractNamedJob, IEquatable
{
#region Data
[NotNull]
private readonly Action _action;
[CanBeNull]
private readonly object _identity;
[NotNull]
private readonly string _name;
#endregion
#region Init
public ActionJob([NotNull] string name, [NotNull] Action action)
: this(name, action, null)
{
}
///
/// Creates the job.
///
/// Name of the activity. Does not take part in merging the jobs.
/// The activity to be executed by the job. Does not take part in merging the jobs.
/// An optional identity for the job by which the async jobs will be merged together. If the is specified, it's used to jo's equality checks. If it's Null, the job is considered to be unique.
public ActionJob([NotNull] string name, [NotNull] Action action, [CanBeNull] object identity)
{
if(name == null)
throw new ArgumentNullException("name");
if(action == null)
throw new ArgumentNullException("action");
_name = name;
_action = action;
_identity = identity;
}
#endregion
#region Overrides
public override bool Equals(object obj)
{
if(ReferenceEquals(this, obj))
return true;
return Equals(obj as ActionJob);
}
public override int GetHashCode()
{
return _identity != null ? _identity.GetHashCode() : base.GetHashCode();
}
///
///Returns a that represents the current .
///
///
///
///A that represents the current .
///
///2
public override string ToString()
{
return string.Format("Job “{0}”", Name);
}
///
/// Override this method in order to perform an one-step job or to do
/// initialization work for a many-steps job.
///
protected override void Execute()
{
_action();
}
///
/// Gets or sets the name of the job.
///
/// The name of the last executing job is displayed in the tooltip for
/// the async processor status indicator in the status bar.
[NotNull]
public override string Name
{
get
{
return _name;
}
}
#endregion
#region IEquatable Members
public bool Equals(ActionJob other)
{
if(other == null)
return false;
if(_identity == null)
return ReferenceEquals(this, other);
return Equals(_identity, other._identity);
}
#endregion
}
}