DictionaryBase.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. //
  2. // System.Collections.DictionaryBase.cs
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. // (C) Ximian, Inc. http://www.ximian.com
  8. //
  9. //
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. namespace System.Collections {
  33. /// <summary>
  34. /// An abstract class that provides a simple way to monitor changes to a
  35. /// Hashtable. Derived classes overwrite one or more of the `On' methods
  36. /// to track the changes to the Hashtable.
  37. /// </summary>
  38. ///
  39. /// <remarks>
  40. /// This class is a base class that can simplify the development of
  41. /// strongly typed collections. The idea being that the insertion of elements
  42. /// into the Hashtable can be forced to be of a given type.
  43. ///
  44. /// The `On' members are protected and designed to be used only by derived
  45. /// classes.
  46. /// </remarks>
  47. [Serializable]
  48. public abstract class DictionaryBase : IDictionary, ICollection, IEnumerable {
  49. Hashtable dictionary;
  50. protected DictionaryBase ()
  51. {
  52. dictionary = new Hashtable ();
  53. }
  54. /// <summary>
  55. /// Clears the contents of the dictionary
  56. /// </summary>
  57. public void Clear ()
  58. {
  59. OnClear ();
  60. dictionary.Clear ();
  61. OnClearComplete ();
  62. }
  63. /// <summary>
  64. /// Returns the number of items in the dictionary
  65. /// </summary>
  66. public int Count {
  67. get {
  68. return dictionary.Count;
  69. }
  70. }
  71. /// <summary>
  72. /// The collection contained as an IDictionary
  73. /// </summary>
  74. protected IDictionary Dictionary {
  75. get {
  76. return this;
  77. }
  78. }
  79. /// <summary>
  80. /// The internal Hashtable representation for this dictionary
  81. /// </summary>
  82. protected Hashtable InnerHashtable {
  83. get {
  84. return dictionary;
  85. }
  86. }
  87. /// <summary>
  88. /// Copies the contents of the Dictionary into the target array
  89. /// </summary>
  90. /// <param name="array">
  91. /// The array to copy the contents of the dictionary to. The
  92. /// array must have a zero-based indexing
  93. /// </param>
  94. /// <param name="index">
  95. /// Starting index within the array where to copy the objects
  96. /// to.
  97. /// </param>
  98. public void CopyTo (Array array, int index)
  99. {
  100. if (array == null)
  101. throw new ArgumentNullException ("array");
  102. if (index < 0)
  103. throw new ArgumentOutOfRangeException ("index must be possitive");
  104. if (array.Rank > 1)
  105. throw new ArgumentException ("array is multidimensional");
  106. int size = array.Length;
  107. if (index > size)
  108. throw new ArgumentException ("index is larger than array size");
  109. if (index + Count > size)
  110. throw new ArgumentException ("Copy will overlflow array");
  111. DoCopy (array, index);
  112. }
  113. /// <summary>
  114. /// Internal routine called by CopyTo to perform the actual
  115. /// copying of the data
  116. /// </summary>
  117. private void DoCopy (Array array, int index)
  118. {
  119. foreach (DictionaryEntry de in dictionary)
  120. array.SetValue (de, index++);
  121. }
  122. /// <summary>
  123. /// Returns an enumerator for the dictionary
  124. /// </summary>
  125. public IDictionaryEnumerator GetEnumerator ()
  126. {
  127. return dictionary.GetEnumerator ();
  128. }
  129. /// <summary>
  130. /// Hook invoked before the clear operation
  131. /// is performed on the DictionaryBase
  132. /// </summary>
  133. protected virtual void OnClear ()
  134. {
  135. }
  136. /// <summary>
  137. /// Hook invoked after the clear operation
  138. /// is performed on the DictionaryBase
  139. /// </summary>
  140. ///
  141. /// <remarks>
  142. /// The default implementation does nothing, derived classes
  143. /// can override this method to be notified of changes
  144. /// </remarks>
  145. protected virtual void OnClearComplete ()
  146. {
  147. }
  148. /// <summary>
  149. /// Hook invoked while fetching data from the DictionaryBase.
  150. /// </summary>
  151. ///
  152. /// <remarks>
  153. /// This method is provided as a simple way to override the values
  154. /// returned by the DictionaryBase.
  155. /// </remarks>
  156. ///
  157. /// <param name="key">Key of the object to retrieve</param>
  158. /// <param name="current_value">Current value of the object associated with
  159. /// <paramref name="key"/></param>
  160. protected virtual object OnGet (object key, object current_value)
  161. {
  162. return current_value;
  163. }
  164. /// <summary>
  165. /// Hook invoked before inserting data into the DictionaryBase.
  166. /// </summary>
  167. ///
  168. /// <remarks>
  169. /// Derived classes can override this method and perform some
  170. /// action before the <paramref name="current_value"/> is inserted
  171. /// into the dictionary.
  172. ///
  173. /// The default implementation does nothing, derived classes
  174. /// can override this method to be notified of changes
  175. /// </remarks>
  176. ///
  177. /// <param name="key">Key of the object to insert</param>
  178. /// <param name="current_value">Current value of the object associated with
  179. /// <paramref name="key"/></param>
  180. protected virtual void OnInsert (object key, object current_value)
  181. {
  182. }
  183. /// <summary>
  184. /// Hook invoked after inserting the data into the DictionaryBase
  185. /// </summary>
  186. ///
  187. /// <remarks>
  188. /// The default implementation does nothing, derived classes
  189. /// can override this method to be notified of changes
  190. /// </remarks>
  191. ///
  192. /// <param name="key">Key of the object to insert</param>
  193. /// <param name="current_value">Current value of the object associated with
  194. /// <paramref name="key"/></param>
  195. protected virtual void OnInsertComplete (object key, object current_value)
  196. {
  197. }
  198. /// <summary>
  199. /// Hook invoked before changing a value for a key in the DictionaryBase.
  200. /// </summary>
  201. ///
  202. /// <remarks>
  203. /// Derived classes can override this method and perform some
  204. /// action before the <paramref name="current_value"/> is changed
  205. /// in the dictionary.
  206. /// </remarks>
  207. ///
  208. /// <param name="key">Key of the object to change</param>
  209. /// <param name="current_value">Current value of the object associated with
  210. /// <paramref name="key"/></param>
  211. protected virtual void OnSet (object key, object current_value, object new_value)
  212. {
  213. }
  214. /// <summary>
  215. /// Hook invoked after changing a value for a key in the DictionaryBase.
  216. /// </summary>
  217. ///
  218. /// <remarks>
  219. /// The default implementation does nothing, derived classes
  220. /// can override this method to be notified of changes
  221. /// </remarks>
  222. ///
  223. /// <param name="key">Key of the object to change</param>
  224. /// <param name="current_value">Current value of the object associated with
  225. /// <paramref name="key"/></param>
  226. protected virtual void OnSetComplete (object key, object current_value, object new_value)
  227. {
  228. }
  229. /// <summary>
  230. /// Hook invoked before removing a key/value from the DictionaryBase.
  231. /// </summary>
  232. ///
  233. /// <remarks>
  234. /// Derived classes can override this method and perform some
  235. /// action before the <paramref name="current_value"/> is removed
  236. /// from the dictionary.
  237. /// </remarks>
  238. ///
  239. /// <param name="key">Key of the object to remove</param>
  240. /// <param name="current_value">Current value of the object associated with
  241. /// <paramref name="key"/></param>
  242. protected virtual void OnRemove (object key, object current_value)
  243. {
  244. }
  245. /// <summary>
  246. /// Hook invoked after removing a key/value from the DictionaryBase.
  247. /// </summary>
  248. ///
  249. /// <remarks>
  250. /// The default implementation does nothing, derived classes
  251. /// can override this method to be notified of changes.
  252. /// </remarks>
  253. ///
  254. /// <param name="key">Key of the object to remove</param>
  255. /// <param name="current_value">Current value of the object associated with
  256. /// <paramref name="key"/></param>
  257. protected virtual void OnRemoveComplete (object key, object current_value)
  258. {
  259. }
  260. /// <summary>
  261. /// Hook invoked after the value has been validated
  262. /// </summary>
  263. ///
  264. /// <remarks>
  265. /// The default implementation does nothing, derived classes
  266. /// can override this method to monitor the DictionaryBase.
  267. /// </remarks>
  268. ///
  269. /// <param name="key">Key of the object to retrieve</param>
  270. /// <param name="current_value">Current value of the object associated with
  271. /// <paramref name="key"/></param>
  272. protected virtual void OnValidate (object key, object current_value)
  273. {
  274. }
  275. bool IDictionary.IsFixedSize {
  276. get {
  277. return false;
  278. }
  279. }
  280. bool IDictionary.IsReadOnly {
  281. get {
  282. return false;
  283. }
  284. }
  285. object IDictionary.this [object key] {
  286. get {
  287. OnGet (key, dictionary[key]);
  288. object value = dictionary [key];
  289. return value;
  290. }
  291. set {
  292. OnValidate (key, value);
  293. object current_value = dictionary [key];
  294. OnSet (key, current_value, value);
  295. dictionary [key] = value;
  296. try {
  297. OnSetComplete (key, current_value, value);
  298. } catch {
  299. dictionary [key] = current_value;
  300. throw;
  301. }
  302. }
  303. }
  304. ICollection IDictionary.Keys {
  305. get {
  306. return dictionary.Keys;
  307. }
  308. }
  309. ICollection IDictionary.Values {
  310. get {
  311. return dictionary.Values;
  312. }
  313. }
  314. /// <summary>
  315. /// Adds a key/value pair to the dictionary.
  316. /// </summary>
  317. void IDictionary.Add (object key, object value)
  318. {
  319. OnValidate (key, value);
  320. OnInsert (key, value);
  321. dictionary.Add (key, value);
  322. try {
  323. OnInsertComplete (key, value);
  324. } catch {
  325. dictionary.Remove(key);
  326. throw;
  327. }
  328. }
  329. /// <summary>
  330. /// Removes a Dictionary Entry based on its key
  331. /// </summary>
  332. void IDictionary.Remove (object key)
  333. {
  334. object value = dictionary [key];
  335. OnValidate (key, value);
  336. OnRemove (key, value);
  337. dictionary.Remove (key);
  338. OnRemoveComplete (key, value);
  339. }
  340. /// <summary>
  341. /// Tests whether the dictionary contains an entry
  342. /// </summary>
  343. bool IDictionary.Contains (object key)
  344. {
  345. return dictionary.Contains (key);
  346. }
  347. bool ICollection.IsSynchronized {
  348. get {
  349. return dictionary.IsSynchronized;
  350. }
  351. }
  352. object ICollection.SyncRoot {
  353. get {
  354. return dictionary.SyncRoot;
  355. }
  356. }
  357. IEnumerator IEnumerable.GetEnumerator ()
  358. {
  359. return dictionary.GetEnumerator ();
  360. }
  361. }
  362. }