using System;
namespace Wayne.Lib.AsyncManager
{
///
/// Async manager using Int as operation id.
///
public class IntAsyncManager : AsyncManager
{
private int currentValue;
private readonly int maxValue;
private readonly int wrapTo;
///
/// Constructor.
/// Uses a default timeout of 1 hour.
///
/// The Id.
/// The parent entity.
public IntAsyncManager(int id, IIdentifiableEntity parentEntity)
: this(id, parentEntity, ServiceContainerFactory.Create(), 0, int.MaxValue, 0)
{
}
///
/// Constructor.
/// Uses a default timeout of 1 hour.
///
/// The Id.
/// The parent entity.
///
/// The initial value.
/// The maximum (included) value.
public IntAsyncManager(int id, IIdentifiableEntity parentEntity, IServiceLocator serviceLocator, int initialValue, int maxValue)
: this(id, parentEntity, serviceLocator, initialValue, maxValue, initialValue)
{
}
///
/// Constructor.
/// Uses a default timeout of 1 hour.
///
/// The Id.
/// The parent entity.
///
/// The initial value.
/// The maximum (included) value.
/// This is the next value after reaching the maximum value.
public IntAsyncManager(int id, IIdentifiableEntity parentEntity, IServiceLocator serviceLocator, int initialValue, int maxValue, int wrapTo)
: this(id, parentEntity, serviceLocator, initialValue, maxValue, wrapTo, TimeSpan.FromHours(1))
{
}
///
/// Constructor.
///
/// The Id.
/// The parent entity.
///
/// The initial value.
/// The maximum (included) value.
/// This is the next value after reaching the maximum value.
/// Sets the maximum age an operation can achieve.
/// Minimum value is 1 minute. If null no timer is created.
///
public IntAsyncManager(int id, IIdentifiableEntity parentEntity, IServiceLocator serviceLocator, int initialValue, int maxValue, int wrapTo, TimeSpan? cleanOutstandingOperationsOlderThan)
: base(id, parentEntity, serviceLocator, cleanOutstandingOperationsOlderThan)
{
if (initialValue > maxValue)
throw new ArgumentException("The initialValue may not be greater than the maxValue!");
if (wrapTo > maxValue)
throw new ArgumentException("The wrapTo may not be greater than the maxValue!");
currentValue = initialValue;
this.maxValue = maxValue;
this.wrapTo = wrapTo;
}
///
/// Generates the next id.
///
///
protected override int CreateNextOperationId()
{
int value = currentValue;
long nextLong = (long)currentValue + 1;
if (nextLong > maxValue)
nextLong = wrapTo;
currentValue = (int)nextLong;
return value;
}
///
/// Entity subtype
///
public override string EntitySubType
{
get { return "int"; }
}
}
}