/Rhino.Etl.Core/Enumerables/SingleRowEventRaisingEnumerator.cs

http://github.com/ayende/rhino-etl · C# · 127 lines · 54 code · 11 blank · 62 comment · 2 complexity · 8824ea875a8c390e5547f265d7ba7c8d MD5 · raw file

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Rhino.Etl.Core.Operations;
  4. namespace Rhino.Etl.Core.Enumerables
  5. {
  6. /// <summary>
  7. /// An enumerator that will raise the events on the operation for each iterated item
  8. /// </summary>
  9. public class SingleRowEventRaisingEnumerator : IEnumerable<Row>, IEnumerator<Row>
  10. {
  11. /// <summary>
  12. /// Represents the operation on which to raise events
  13. /// </summary>
  14. protected readonly IOperation operation;
  15. private readonly IEnumerable<Row> inner;
  16. private IEnumerator<Row> innerEnumerator;
  17. private Row previous;
  18. /// <summary>
  19. /// Initializes a new instance of the <see cref="SingleRowEventRaisingEnumerator"/> class.
  20. /// </summary>
  21. /// <param name="operation">The operation.</param>
  22. /// <param name="inner">The innerEnumerator.</param>
  23. public SingleRowEventRaisingEnumerator(IOperation operation, IEnumerable<Row> inner)
  24. {
  25. this.operation = operation;
  26. this.inner = inner;
  27. }
  28. ///<summary>
  29. ///Gets the element in the collection at the current position of the enumerator.
  30. ///</summary>
  31. ///
  32. ///<returns>
  33. ///The element in the collection at the current position of the enumerator.
  34. ///</returns>
  35. ///
  36. public Row Current
  37. {
  38. get { return innerEnumerator.Current; }
  39. }
  40. ///<summary>
  41. ///Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  42. ///</summary>
  43. ///<filterpriority>2</filterpriority>
  44. public void Dispose()
  45. {
  46. innerEnumerator.Dispose();
  47. }
  48. ///<summary>
  49. ///Advances the enumerator to the next element of the collection.
  50. ///</summary>
  51. ///
  52. ///<returns>
  53. ///true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.
  54. ///</returns>
  55. ///
  56. ///<exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception><filterpriority>2</filterpriority>
  57. public virtual bool MoveNext()
  58. {
  59. bool result = innerEnumerator.MoveNext();
  60. if (result)
  61. {
  62. previous = innerEnumerator.Current;
  63. operation.RaiseRowProcessed(Current);
  64. }
  65. return result;
  66. }
  67. ///<summary>
  68. ///Sets the enumerator to its initial position, which is before the first element in the collection.
  69. ///</summary>
  70. ///
  71. ///<exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception><filterpriority>2</filterpriority>
  72. public void Reset()
  73. {
  74. innerEnumerator.Reset();
  75. }
  76. ///<summary>
  77. ///Gets the current element in the collection.
  78. ///</summary>
  79. ///
  80. ///<returns>
  81. ///The current element in the collection.
  82. ///</returns>
  83. ///
  84. ///<exception cref="T:System.InvalidOperationException">The enumerator is positioned before the first element of the collection or after the last element.-or- The collection was modified after the enumerator was created.</exception><filterpriority>2</filterpriority>
  85. object IEnumerator.Current
  86. {
  87. get { return innerEnumerator.Current; }
  88. }
  89. ///<summary>
  90. ///Returns an enumerator that iterates through the collection.
  91. ///</summary>
  92. ///
  93. ///<returns>
  94. ///A <see cref="T:System.Collections.Generic.IEnumerator`1"></see> that can be used to iterate through the collection.
  95. ///</returns>
  96. ///<filterpriority>1</filterpriority>
  97. IEnumerator<Row> IEnumerable<Row>.GetEnumerator()
  98. {
  99. Guard.Against(inner == null, "Null enuerator detected, are you trying to read from the first operation in the process?");
  100. innerEnumerator = inner.GetEnumerator();
  101. return this;
  102. }
  103. ///<summary>
  104. ///Returns an enumerator that iterates through a collection.
  105. ///</summary>
  106. ///
  107. ///<returns>
  108. ///An <see cref="T:System.Collections.IEnumerator"></see> object that can be used to iterate through the collection.
  109. ///</returns>
  110. ///<filterpriority>2</filterpriority>
  111. public IEnumerator GetEnumerator()
  112. {
  113. return ((IEnumerable<Row>) this).GetEnumerator();
  114. }
  115. }
  116. }