EmptyDictionary.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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="IDictionary"/>.
  25. /// </summary>
  26. /// <remarks>
  27. /// <para>
  28. /// A singleton implementation of the <see cref="IDictionary"/>
  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 EmptyDictionary : IDictionary
  38. {
  39. #region Private Instance Constructors
  40. /// <summary>
  41. /// Initializes a new instance of the <see cref="EmptyDictionary" /> class.
  42. /// </summary>
  43. /// <remarks>
  44. /// <para>
  45. /// Uses a private access modifier to enforce the singleton pattern.
  46. /// </para>
  47. /// </remarks>
  48. private EmptyDictionary()
  49. {
  50. }
  51. #endregion Private Instance Constructors
  52. #region Public Static Properties
  53. /// <summary>
  54. /// Gets the singleton instance of the <see cref="EmptyDictionary" />.
  55. /// </summary>
  56. /// <returns>The singleton instance of the <see cref="EmptyDictionary" />.</returns>
  57. /// <remarks>
  58. /// <para>
  59. /// Gets the singleton instance of the <see cref="EmptyDictionary" />.
  60. /// </para>
  61. /// </remarks>
  62. public static EmptyDictionary 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 <b>true</b>.
  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. IEnumerator IEnumerable.GetEnumerator()
  148. {
  149. return NullEnumerator.Instance;
  150. }
  151. #endregion Implementation of IEnumerable
  152. #region Implementation of IDictionary
  153. /// <summary>
  154. /// Adds an element with the provided key and value to the
  155. /// <see cref="EmptyDictionary" />.
  156. /// </summary>
  157. /// <param name="key">The <see cref="object" /> to use as the key of the element to add.</param>
  158. /// <param name="value">The <see cref="object" /> to use as the value of the element to add.</param>
  159. /// <remarks>
  160. /// <para>
  161. /// As the collection is empty no new values can be added. A <see cref="InvalidOperationException"/>
  162. /// is thrown if this method is called.
  163. /// </para>
  164. /// </remarks>
  165. /// <exception cref="InvalidOperationException">This dictionary is always empty and cannot be modified.</exception>
  166. public void Add(object key, object value)
  167. {
  168. throw new InvalidOperationException();
  169. }
  170. /// <summary>
  171. /// Removes all elements from the <see cref="EmptyDictionary" />.
  172. /// </summary>
  173. /// <remarks>
  174. /// <para>
  175. /// As the collection is empty no values can be removed. A <see cref="InvalidOperationException"/>
  176. /// is thrown if this method is called.
  177. /// </para>
  178. /// </remarks>
  179. /// <exception cref="InvalidOperationException">This dictionary is always empty and cannot be modified.</exception>
  180. public void Clear()
  181. {
  182. throw new InvalidOperationException();
  183. }
  184. /// <summary>
  185. /// Determines whether the <see cref="EmptyDictionary" /> contains an element
  186. /// with the specified key.
  187. /// </summary>
  188. /// <param name="key">The key to locate in the <see cref="EmptyDictionary" />.</param>
  189. /// <returns><c>false</c></returns>
  190. /// <remarks>
  191. /// <para>
  192. /// As the collection is empty the <see cref="Contains"/> method always returns <c>false</c>.
  193. /// </para>
  194. /// </remarks>
  195. public bool Contains(object key)
  196. {
  197. return false;
  198. }
  199. /// <summary>
  200. /// Returns an enumerator that can iterate through a collection.
  201. /// </summary>
  202. /// <returns>
  203. /// An <see cref="IEnumerator"/> that can be used to
  204. /// iterate through the collection.
  205. /// </returns>
  206. /// <remarks>
  207. /// <para>
  208. /// As the collection is empty a <see cref="NullEnumerator"/> is returned.
  209. /// </para>
  210. /// </remarks>
  211. public IDictionaryEnumerator GetEnumerator()
  212. {
  213. return NullDictionaryEnumerator.Instance;
  214. }
  215. /// <summary>
  216. /// Removes the element with the specified key from the <see cref="EmptyDictionary" />.
  217. /// </summary>
  218. /// <param name="key">The key of the element to remove.</param>
  219. /// <remarks>
  220. /// <para>
  221. /// As the collection is empty no values can be removed. A <see cref="InvalidOperationException"/>
  222. /// is thrown if this method is called.
  223. /// </para>
  224. /// </remarks>
  225. /// <exception cref="InvalidOperationException">This dictionary is always empty and cannot be modified.</exception>
  226. public void Remove(object key)
  227. {
  228. throw new InvalidOperationException();
  229. }
  230. /// <summary>
  231. /// Gets a value indicating whether the <see cref="EmptyDictionary" /> has a fixed size.
  232. /// </summary>
  233. /// <value><c>true</c></value>
  234. /// <remarks>
  235. /// <para>
  236. /// As the collection is empty <see cref="IsFixedSize"/> always returns <c>true</c>.
  237. /// </para>
  238. /// </remarks>
  239. public bool IsFixedSize
  240. {
  241. get { return true; }
  242. }
  243. /// <summary>
  244. /// Gets a value indicating whether the <see cref="EmptyDictionary" /> is read-only.
  245. /// </summary>
  246. /// <value><c>true</c></value>
  247. /// <remarks>
  248. /// <para>
  249. /// As the collection is empty <see cref="IsReadOnly"/> always returns <c>true</c>.
  250. /// </para>
  251. /// </remarks>
  252. public bool IsReadOnly
  253. {
  254. get { return true; }
  255. }
  256. /// <summary>
  257. /// Gets an <see cref="ICollection" /> containing the keys of the <see cref="EmptyDictionary" />.
  258. /// </summary>
  259. /// <value>An <see cref="ICollection" /> containing the keys of the <see cref="EmptyDictionary" />.</value>
  260. /// <remarks>
  261. /// <para>
  262. /// As the collection is empty a <see cref="EmptyCollection"/> is returned.
  263. /// </para>
  264. /// </remarks>
  265. public System.Collections.ICollection Keys
  266. {
  267. get { return EmptyCollection.Instance; }
  268. }
  269. /// <summary>
  270. /// Gets an <see cref="ICollection" /> containing the values of the <see cref="EmptyDictionary" />.
  271. /// </summary>
  272. /// <value>An <see cref="ICollection" /> containing the values of the <see cref="EmptyDictionary" />.</value>
  273. /// <remarks>
  274. /// <para>
  275. /// As the collection is empty a <see cref="EmptyCollection"/> is returned.
  276. /// </para>
  277. /// </remarks>
  278. public System.Collections.ICollection Values
  279. {
  280. get { return EmptyCollection.Instance; }
  281. }
  282. /// <summary>
  283. /// Gets or sets the element with the specified key.
  284. /// </summary>
  285. /// <param name="key">The key of the element to get or set.</param>
  286. /// <value><c>null</c></value>
  287. /// <remarks>
  288. /// <para>
  289. /// As the collection is empty no values can be looked up or stored.
  290. /// If the index getter is called then <c>null</c> is returned.
  291. /// A <see cref="InvalidOperationException"/> is thrown if the setter is called.
  292. /// </para>
  293. /// </remarks>
  294. /// <exception cref="InvalidOperationException">This dictionary is always empty and cannot be modified.</exception>
  295. public object this[object key]
  296. {
  297. get { return null; }
  298. set { throw new InvalidOperationException(); }
  299. }
  300. #endregion Implementation of IDictionary
  301. #region Private Static Fields
  302. /// <summary>
  303. /// The singleton instance of the empty dictionary.
  304. /// </summary>
  305. private readonly static EmptyDictionary s_instance = new EmptyDictionary();
  306. #endregion Private Static Fields
  307. }
  308. }