/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

  1. /******************************************************************************
  2. * Project: Neddle Community Edition
  3. * Filename: IteratorIsolate.cs
  4. * Created By: Kristopher Cargile <kristopher@neddle.org>
  5. * Date Created: March 29, 2011
  6. * ****************************************************************************
  7. * IMPORTANT GNU LICENSE INFORMATION. DO NOT REMOVE THIS SECTION!
  8. *
  9. * Neddle :: Copyright (c) 2010-2012, Cargile Technology Group, LLC
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  24. ******************************************************************************/
  25. using System;
  26. using System.Collections;
  27. namespace Neddle
  28. {
  29. /// <summary>
  30. /// Allows you to modify the collection
  31. /// while within a for each loop.
  32. /// </summary>
  33. public class IteratorIsolate : IEnumerable
  34. {
  35. /// <summary>
  36. /// IteratorIsolateEnumerator
  37. /// </summary>
  38. internal class IteratorIsolateEnumerator : IEnumerator
  39. {
  40. protected ArrayList items = new ArrayList();
  41. protected int currentItem;
  42. /// <summary>
  43. /// Initializes a new instance of the <see cref="IteratorIsolateEnumerator"/> class.
  44. /// </summary>
  45. /// <param name="enumerator">The enumerator.</param>
  46. internal IteratorIsolateEnumerator( IEnumerator enumerator )
  47. {
  48. while(enumerator.MoveNext() != false)
  49. {
  50. items.Add( enumerator.Current );
  51. }
  52. IDisposable disposable = enumerator as IDisposable;
  53. if(disposable != null)
  54. {
  55. disposable.Dispose();
  56. }
  57. currentItem = -1;
  58. }
  59. /// <summary>
  60. /// Sets the enumerator to its initial position, which is before the first element in the collection.
  61. /// </summary>
  62. /// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception>
  63. public void Reset()
  64. {
  65. currentItem = -1;
  66. }
  67. /// <summary>
  68. /// Advances the enumerator to the next element of the collection.
  69. /// </summary>
  70. /// <returns>
  71. /// true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.
  72. /// </returns>
  73. /// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception>
  74. public bool MoveNext()
  75. {
  76. currentItem++;
  77. if(currentItem == items.Count)
  78. return false;
  79. return true;
  80. }
  81. /// <summary>
  82. /// Gets the current element in the collection.
  83. /// </summary>
  84. /// <value></value>
  85. /// <returns>The current element in the collection.</returns>
  86. /// <exception cref="T:System.InvalidOperationException">The enumerator is positioned before the first element of the collection or after the last element. </exception>
  87. public object Current
  88. {
  89. get
  90. {
  91. return items[currentItem];
  92. }
  93. }
  94. }
  95. /// <summary>
  96. /// Initializes a new instance of the <see cref="IteratorIsolate"/> class.
  97. /// </summary>
  98. /// <param name="enumerable">The enumerable.</param>
  99. public IteratorIsolate( IEnumerable enumerable )
  100. {
  101. this.enumerable = enumerable;
  102. }
  103. /// <summary>
  104. /// Returns an enumerator that iterates through a collection.
  105. /// </summary>
  106. /// <returns>
  107. /// An <see cref="T:System.Collections.IEnumerator"></see> object that can be used to iterate through the collection.
  108. /// </returns>
  109. public IEnumerator GetEnumerator()
  110. {
  111. return new IteratorIsolateEnumerator( enumerable.GetEnumerator() );
  112. }
  113. /// <summary>
  114. ///
  115. /// </summary>
  116. protected IEnumerable enumerable;
  117. }
  118. }