NullEnumerator.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #region Apache License
  2. //
  3. // Licensed to the Apache Software Foundation (ASF) under one or more
  4. // contributor license agreements. See the NOTICE file distributed with
  5. // this work for additional information regarding copyright ownership.
  6. // The ASF licenses this file to you under the Apache License, Version 2.0
  7. // (the "License"); you may not use this file except in compliance with
  8. // the License. You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. //
  18. #endregion
  19. using System;
  20. using System.Collections;
  21. namespace log4net.Util
  22. {
  23. /// <summary>
  24. /// An always empty <see cref="IEnumerator"/>.
  25. /// </summary>
  26. /// <remarks>
  27. /// <para>
  28. /// A singleton implementation of the <see cref="IEnumerator"/> over a collection
  29. /// that is empty and not modifiable.
  30. /// </para>
  31. /// </remarks>
  32. /// <author>Nicko Cadell</author>
  33. /// <author>Gert Driesen</author>
  34. public sealed class NullEnumerator : IEnumerator
  35. {
  36. #region Private Instance Constructors
  37. /// <summary>
  38. /// Initializes a new instance of the <see cref="NullEnumerator" /> class.
  39. /// </summary>
  40. /// <remarks>
  41. /// <para>
  42. /// Uses a private access modifier to enforce the singleton pattern.
  43. /// </para>
  44. /// </remarks>
  45. private NullEnumerator()
  46. {
  47. }
  48. #endregion Private Instance Constructors
  49. #region Public Static Properties
  50. /// <summary>
  51. /// Get the singleton instance of the <see cref="NullEnumerator" />.
  52. /// </summary>
  53. /// <returns>The singleton instance of the <see cref="NullEnumerator" />.</returns>
  54. /// <remarks>
  55. /// <para>
  56. /// Gets the singleton instance of the <see cref="NullEnumerator" />.
  57. /// </para>
  58. /// </remarks>
  59. public static NullEnumerator Instance
  60. {
  61. get { return s_instance; }
  62. }
  63. #endregion Public Static Properties
  64. #region Implementation of IEnumerator
  65. /// <summary>
  66. /// Gets the current object from the enumerator.
  67. /// </summary>
  68. /// <remarks>
  69. /// Throws an <see cref="InvalidOperationException" /> because the
  70. /// <see cref="NullDictionaryEnumerator" /> never has a current value.
  71. /// </remarks>
  72. /// <remarks>
  73. /// <para>
  74. /// As the enumerator is over an empty collection its <see cref="Current"/>
  75. /// value cannot be moved over a valid position, therefore <see cref="Current"/>
  76. /// will throw an <see cref="InvalidOperationException"/>.
  77. /// </para>
  78. /// </remarks>
  79. /// <exception cref="InvalidOperationException">The collection is empty and <see cref="Current"/>
  80. /// cannot be positioned over a valid location.</exception>
  81. public object Current
  82. {
  83. get { throw new InvalidOperationException(); }
  84. }
  85. /// <summary>
  86. /// Test if the enumerator can advance, if so advance
  87. /// </summary>
  88. /// <returns><c>false</c> as the <see cref="NullEnumerator" /> cannot advance.</returns>
  89. /// <remarks>
  90. /// <para>
  91. /// As the enumerator is over an empty collection its <see cref="Current"/>
  92. /// value cannot be moved over a valid position, therefore <see cref="MoveNext"/>
  93. /// will always return <c>false</c>.
  94. /// </para>
  95. /// </remarks>
  96. public bool MoveNext()
  97. {
  98. return false;
  99. }
  100. /// <summary>
  101. /// Resets the enumerator back to the start.
  102. /// </summary>
  103. /// <remarks>
  104. /// <para>
  105. /// As the enumerator is over an empty collection <see cref="Reset"/> does nothing.
  106. /// </para>
  107. /// </remarks>
  108. public void Reset()
  109. {
  110. }
  111. #endregion Implementation of IEnumerator
  112. #region Private Static Fields
  113. /// <summary>
  114. /// The singleton instance of the <see cref="NullEnumerator" />.
  115. /// </summary>
  116. private readonly static NullEnumerator s_instance = new NullEnumerator();
  117. #endregion Private Static Fields
  118. }
  119. }