EmptyCollection.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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="ICollection"/>.
  25. /// </summary>
  26. /// <remarks>
  27. /// <para>
  28. /// A singleton implementation of the <see cref="ICollection"/>
  29. /// interface that always represents an empty collection.
  30. /// </para>
  31. /// </remarks>
  32. /// <author>Nicko Cadell</author>
  33. /// <author>Gert Driesen</author>
  34. #if !NETCF
  35. [Serializable]
  36. #endif
  37. public sealed class EmptyCollection : ICollection
  38. {
  39. #region Private Instance Constructors
  40. /// <summary>
  41. /// Initializes a new instance of the <see cref="EmptyCollection" /> class.
  42. /// </summary>
  43. /// <remarks>
  44. /// <para>
  45. /// Uses a private access modifier to enforce the singleton pattern.
  46. /// </para>
  47. /// </remarks>
  48. private EmptyCollection()
  49. {
  50. }
  51. #endregion Private Instance Constructors
  52. #region Public Static Properties
  53. /// <summary>
  54. /// Gets the singleton instance of the empty collection.
  55. /// </summary>
  56. /// <returns>The singleton instance of the empty collection.</returns>
  57. /// <remarks>
  58. /// <para>
  59. /// Gets the singleton instance of the empty collection.
  60. /// </para>
  61. /// </remarks>
  62. public static EmptyCollection Instance
  63. {
  64. get { return s_instance; }
  65. }
  66. #endregion Public Static Properties
  67. #region Implementation of ICollection
  68. /// <summary>
  69. /// Copies the elements of the <see cref="ICollection"/> to an
  70. /// <see cref="Array"/>, starting at a particular Array index.
  71. /// </summary>
  72. /// <param name="array">The one-dimensional <see cref="Array"/>
  73. /// that is the destination of the elements copied from
  74. /// <see cref="ICollection"/>. The Array must have zero-based
  75. /// indexing.</param>
  76. /// <param name="index">The zero-based index in array at which
  77. /// copying begins.</param>
  78. /// <remarks>
  79. /// <para>
  80. /// As the collection is empty no values are copied into the array.
  81. /// </para>
  82. /// </remarks>
  83. public void CopyTo(System.Array array, int index)
  84. {
  85. // copy nothing
  86. }
  87. /// <summary>
  88. /// Gets a value indicating if access to the <see cref="ICollection"/> is synchronized (thread-safe).
  89. /// </summary>
  90. /// <value>
  91. /// <b>true</b> if access to the <see cref="ICollection"/> is synchronized (thread-safe); otherwise, <b>false</b>.
  92. /// </value>
  93. /// <remarks>
  94. /// <para>
  95. /// For the <see cref="EmptyCollection"/> this property is always <c>true</c>.
  96. /// </para>
  97. /// </remarks>
  98. public bool IsSynchronized
  99. {
  100. get { return true; }
  101. }
  102. /// <summary>
  103. /// Gets the number of elements contained in the <see cref="ICollection"/>.
  104. /// </summary>
  105. /// <value>
  106. /// The number of elements contained in the <see cref="ICollection"/>.
  107. /// </value>
  108. /// <remarks>
  109. /// <para>
  110. /// As the collection is empty the <see cref="Count"/> is always <c>0</c>.
  111. /// </para>
  112. /// </remarks>
  113. public int Count
  114. {
  115. get { return 0; }
  116. }
  117. /// <summary>
  118. /// Gets an object that can be used to synchronize access to the <see cref="ICollection"/>.
  119. /// </summary>
  120. /// <value>
  121. /// An object that can be used to synchronize access to the <see cref="ICollection"/>.
  122. /// </value>
  123. /// <remarks>
  124. /// <para>
  125. /// As the collection is empty and thread safe and synchronized this instance is also
  126. /// the <see cref="SyncRoot"/> object.
  127. /// </para>
  128. /// </remarks>
  129. public object SyncRoot
  130. {
  131. get { return this; }
  132. }
  133. #endregion Implementation of ICollection
  134. #region Implementation of IEnumerable
  135. /// <summary>
  136. /// Returns an enumerator that can iterate through a collection.
  137. /// </summary>
  138. /// <returns>
  139. /// An <see cref="IEnumerator"/> that can be used to
  140. /// iterate through the collection.
  141. /// </returns>
  142. /// <remarks>
  143. /// <para>
  144. /// As the collection is empty a <see cref="NullEnumerator"/> is returned.
  145. /// </para>
  146. /// </remarks>
  147. public IEnumerator GetEnumerator()
  148. {
  149. return NullEnumerator.Instance;
  150. }
  151. #endregion Implementation of IEnumerable
  152. #region Private Static Fields
  153. /// <summary>
  154. /// The singleton instance of the empty collection.
  155. /// </summary>
  156. private readonly static EmptyCollection s_instance = new EmptyCollection();
  157. #endregion Private Static Fields
  158. }
  159. }