PageRenderTime 419ms CodeModel.GetById 198ms app.highlight 4ms RepoModel.GetById 133ms app.codeStats 1ms

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