/app-core/trunk/Neddle/Extensions/IteratorIsolate.cs
http://neddle.codeplex.com · C# · 127 lines · 53 code · 9 blank · 65 comment · 5 complexity · 74d2796bed7239b3701a62993e487e8a MD5 · raw file
- /******************************************************************************
- * Project: Neddle Community Edition
- * Filename: IteratorIsolate.cs
- * Created By: Kristopher Cargile <kristopher@neddle.org>
- * Date Created: March 29, 2011
- * ****************************************************************************
- * IMPORTANT GNU LICENSE INFORMATION. DO NOT REMOVE THIS SECTION!
- *
- * Neddle :: Copyright (c) 2010-2012, Cargile Technology Group, LLC
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- ******************************************************************************/
-
- using System;
- using System.Collections;
-
- namespace Neddle
- {
- /// <summary>
- /// Allows you to modify the collection
- /// while within a for each loop.
- /// </summary>
- public class IteratorIsolate : IEnumerable
- {
- /// <summary>
- /// IteratorIsolateEnumerator
- /// </summary>
- internal class IteratorIsolateEnumerator : IEnumerator
- {
- protected ArrayList items = new ArrayList();
- protected int currentItem;
-
- /// <summary>
- /// Initializes a new instance of the <see cref="IteratorIsolateEnumerator"/> class.
- /// </summary>
- /// <param name="enumerator">The enumerator.</param>
- internal IteratorIsolateEnumerator( IEnumerator enumerator )
- {
- while(enumerator.MoveNext() != false)
- {
- items.Add( enumerator.Current );
- }
- IDisposable disposable = enumerator as IDisposable;
- if(disposable != null)
- {
- disposable.Dispose();
- }
- currentItem = -1;
- }
-
- /// <summary>
- /// Sets the enumerator to its initial position, which is before the first element in the collection.
- /// </summary>
- /// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception>
- public void Reset()
- {
- currentItem = -1;
- }
-
- /// <summary>
- /// Advances the enumerator to the next element of the collection.
- /// </summary>
- /// <returns>
- /// true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.
- /// </returns>
- /// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception>
- public bool MoveNext()
- {
- currentItem++;
- if(currentItem == items.Count)
- return false;
-
- return true;
- }
-
- /// <summary>
- /// Gets the current element in the collection.
- /// </summary>
- /// <value></value>
- /// <returns>The current element in the collection.</returns>
- /// <exception cref="T:System.InvalidOperationException">The enumerator is positioned before the first element of the collection or after the last element. </exception>
- public object Current
- {
- get
- {
- return items[currentItem];
- }
- }
- }
-
- /// <summary>
- /// Initializes a new instance of the <see cref="IteratorIsolate"/> class.
- /// </summary>
- /// <param name="enumerable">The enumerable.</param>
- public IteratorIsolate( IEnumerable enumerable )
- {
- this.enumerable = enumerable;
- }
-
- /// <summary>
- /// Returns an enumerator that iterates through a collection.
- /// </summary>
- /// <returns>
- /// An <see cref="T:System.Collections.IEnumerator"></see> object that can be used to iterate through the collection.
- /// </returns>
- public IEnumerator GetEnumerator()
- {
- return new IteratorIsolateEnumerator( enumerable.GetEnumerator() );
- }
- /// <summary>
- ///
- /// </summary>
- protected IEnumerable enumerable;
- }
- }