IntAsyncManager.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. namespace Wayne.Lib.AsyncManager
  3. {
  4. /// <summary>
  5. /// Async manager using Int as operation id.
  6. /// </summary>
  7. public class IntAsyncManager : AsyncManager<int>
  8. {
  9. private int currentValue;
  10. private readonly int maxValue;
  11. private readonly int wrapTo;
  12. /// <summary>
  13. /// Constructor.
  14. /// Uses a default timeout of 1 hour.
  15. /// </summary>
  16. /// <param name="id">The Id.</param>
  17. /// <param name="parentEntity">The parent entity.</param>
  18. public IntAsyncManager(int id, IIdentifiableEntity parentEntity)
  19. : this(id, parentEntity, ServiceContainerFactory.Create(), 0, int.MaxValue, 0)
  20. {
  21. }
  22. /// <summary>
  23. /// Constructor.
  24. /// Uses a default timeout of 1 hour.
  25. /// </summary>
  26. /// <param name="id">The Id.</param>
  27. /// <param name="parentEntity">The parent entity.</param>
  28. /// <param name="serviceLocator"></param>
  29. /// <param name="initialValue">The initial value.</param>
  30. /// <param name="maxValue">The maximum (included) value.</param>
  31. public IntAsyncManager(int id, IIdentifiableEntity parentEntity, IServiceLocator serviceLocator, int initialValue, int maxValue)
  32. : this(id, parentEntity, serviceLocator, initialValue, maxValue, initialValue)
  33. {
  34. }
  35. /// <summary>
  36. /// Constructor.
  37. /// Uses a default timeout of 1 hour.
  38. /// </summary>
  39. /// <param name="id">The Id.</param>
  40. /// <param name="parentEntity">The parent entity.</param>
  41. /// <param name="serviceLocator"></param>
  42. /// <param name="initialValue">The initial value.</param>
  43. /// <param name="maxValue">The maximum (included) value.</param>
  44. /// <param name="wrapTo">This is the next value after reaching the maximum value.</param>
  45. public IntAsyncManager(int id, IIdentifiableEntity parentEntity, IServiceLocator serviceLocator, int initialValue, int maxValue, int wrapTo)
  46. : this(id, parentEntity, serviceLocator, initialValue, maxValue, wrapTo, TimeSpan.FromHours(1))
  47. {
  48. }
  49. /// <summary>
  50. /// Constructor.
  51. /// </summary>
  52. /// <param name="id">The Id.</param>
  53. /// <param name="parentEntity">The parent entity.</param>
  54. /// <param name="serviceLocator"></param>
  55. /// <param name="initialValue">The initial value.</param>
  56. /// <param name="maxValue">The maximum (included) value.</param>
  57. /// <param name="wrapTo">This is the next value after reaching the maximum value.</param>
  58. /// <param name="cleanOutstandingOperationsOlderThan">Sets the maximum age an operation can achieve.
  59. /// Minimum value is 1 minute. If null no timer is created.
  60. /// </param>
  61. public IntAsyncManager(int id, IIdentifiableEntity parentEntity, IServiceLocator serviceLocator, int initialValue, int maxValue, int wrapTo, TimeSpan? cleanOutstandingOperationsOlderThan)
  62. : base(id, parentEntity, serviceLocator, cleanOutstandingOperationsOlderThan)
  63. {
  64. if (initialValue > maxValue)
  65. throw new ArgumentException("The initialValue may not be greater than the maxValue!");
  66. if (wrapTo > maxValue)
  67. throw new ArgumentException("The wrapTo may not be greater than the maxValue!");
  68. currentValue = initialValue;
  69. this.maxValue = maxValue;
  70. this.wrapTo = wrapTo;
  71. }
  72. /// <summary>
  73. /// Generates the next id.
  74. /// </summary>
  75. /// <returns></returns>
  76. protected override int CreateNextOperationId()
  77. {
  78. int value = currentValue;
  79. long nextLong = (long)currentValue + 1;
  80. if (nextLong > maxValue)
  81. nextLong = wrapTo;
  82. currentValue = (int)nextLong;
  83. return value;
  84. }
  85. /// <summary>
  86. /// Entity subtype
  87. /// </summary>
  88. public override string EntitySubType
  89. {
  90. get { return "int"; }
  91. }
  92. }
  93. }