2 // System.Collections.DictionaryBase.cs
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) Ximian, Inc. http://www.ximian.com
12 namespace System.Collections {
15 /// An abstract class that provides a simple way to monitor changes to a
16 /// Hashtable. Derived classes overwrite one or more of the `On' methods
17 /// to track the changes to the Hashtable.
21 /// This class is a base class that can simplify the development of
22 /// strongly typed collections. The idea being that the insertion of elements
23 /// into the Hashtable can be forced to be of a given type.
25 /// The `On' members are protected and designed to be used only by derived
28 public abstract class DictionaryBase : IDictionary, ICollection, IEnumerable {
32 protected DictionaryBase ()
34 dictionary = new Hashtable ();
38 /// Clears the contents of the dictionary
48 /// Returns the number of items in the dictionary
52 return dictionary.Count;
57 /// The collection contained as an IDictionary
59 protected IDictionary Dictionary {
66 /// The internal Hashtable representation for this dictionary
68 protected Hashtable InnerHashtable {
75 /// Copies the contents of the Dictionary into the target array
77 /// <param name="array">
78 /// The array to copy the contents of the dictionary to. The
79 /// array must have a zero-based indexing
81 /// <param name="index">
82 /// Starting index within the array where to copy the objects
85 public void CopyTo (Array array, int index)
88 throw new ArgumentNullException ("array");
90 throw new ArgumentOutOfRangeException ("index must be possitive");
92 throw new ArgumentException ("array is multidimensional");
93 int size = array.Length;
95 throw new ArgumentException ("index is larger than array size");
96 if (index + Count > size)
97 throw new ArgumentException ("Copy will overlflow array");
99 DoCopy (array, index);
103 /// Internal routine called by CopyTo to perform the actual
104 /// copying of the data
106 virtual protected void DoCopy (Array array, int index)
108 foreach (DictionaryEntry de in dictionary)
109 array.SetValue (de, index++);
113 /// Returns an enumerator for the dictionary
115 public IDictionaryEnumerator GetEnumerator ()
117 return dictionary.GetEnumerator ();
121 /// Hook invoked before the clear operation
122 /// is performed on the DictionaryBase
124 protected virtual void OnClear ()
129 /// Hook invoked after the clear operation
130 /// is performed on the DictionaryBase
134 /// The default implementation does nothing, derived classes
135 /// can override this method to be notified of changes
137 protected virtual void OnClearComplete ()
142 /// Hook invoked while fetching data from the DictionaryBase.
146 /// This method is provided as a simple way to override the values
147 /// returned by the DictionaryBase.
150 /// <param name="key">Key of the object to retrieve</param>
151 /// <param name="current_value">Current value of the object associated with
152 /// <paramref name="key"/></param>
153 protected virtual object OnGet (object key, object current_value)
155 return current_value;
159 /// Hook invoked before inserting data into the DictionaryBase.
163 /// Derived classes can override this method and perform some
164 /// action before the <paramref name="current_value"/> is inserted
165 /// into the dictionary.
167 /// The default implementation does nothing, derived classes
168 /// can override this method to be notified of changes
171 /// <param name="key">Key of the object to insert</param>
172 /// <param name="current_value">Current value of the object associated with
173 /// <paramref name="key"/></param>
174 protected virtual void OnInsert (object key, object current_value)
179 /// Hook invoked after inserting the data into the DictionaryBase
183 /// The default implementation does nothing, derived classes
184 /// can override this method to be notified of changes
187 /// <param name="key">Key of the object to insert</param>
188 /// <param name="current_value">Current value of the object associated with
189 /// <paramref name="key"/></param>
190 protected virtual void OnInsertComplete (object key, object current_value)
195 /// Hook invoked before changing a value for a key in the DictionaryBase.
199 /// Derived classes can override this method and perform some
200 /// action before the <paramref name="current_value"/> is changed
201 /// in the dictionary.
204 /// <param name="key">Key of the object to change</param>
205 /// <param name="current_value">Current value of the object associated with
206 /// <paramref name="key"/></param>
207 protected virtual void OnSet (object key, object current_value)
212 /// Hook invoked after changing a value for a key in the DictionaryBase.
216 /// The default implementation does nothing, derived classes
217 /// can override this method to be notified of changes
220 /// <param name="key">Key of the object to change</param>
221 /// <param name="current_value">Current value of the object associated with
222 /// <paramref name="key"/></param>
223 protected virtual void OnSetComplete (object key, object current_value)
228 /// Hook invoked before removing a key/value from the DictionaryBase.
232 /// Derived classes can override this method and perform some
233 /// action before the <paramref name="current_value"/> is removed
234 /// from the dictionary.
237 /// <param name="key">Key of the object to remove</param>
238 /// <param name="current_value">Current value of the object associated with
239 /// <paramref name="key"/></param>
240 protected virtual void OnRemove (object key, object current_value)
245 /// Hook invoked after removing a key/value from the DictionaryBase.
249 /// The default implementation does nothing, derived classes
250 /// can override this method to be notified of changes.
253 /// <param name="key">Key of the object to remove</param>
254 /// <param name="current_value">Current value of the object associated with
255 /// <paramref name="key"/></param>
256 protected virtual void OnRemoveComplete (object key, object current_value)
261 /// Hook invoked after the value has been validated
265 /// The default implementation does nothing, derived classes
266 /// can override this method to monitor the DictionaryBase.
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)
276 bool IDictionary.IsFixedSize {
282 bool IDictionary.IsReadOnly {
288 object IDictionary.this [object key] {
290 object value = dictionary [key];
297 if (dictionary.ContainsKey (key)){
299 dictionary [key] = value;
300 OnSetComplete (key, value);
302 OnInsert (key, value);
303 dictionary [key] = value;
304 OnInsertComplete (key, value);
309 ICollection IDictionary.Keys {
311 return dictionary.Keys;
315 ICollection IDictionary.Values {
317 return dictionary.Values;
322 /// Adds a key/value pair to the dictionary.
324 void IDictionary.Add (object key, object value)
326 OnInsert (key, value);
327 dictionary.Add (key, value);
328 OnInsertComplete (key, value);
332 /// Removes a Dictionary Entry based on its key
334 void IDictionary.Remove (object key)
336 if (! dictionary.ContainsKey (key))
339 object value = dictionary [key];
340 OnRemove (key, value);
341 dictionary.Remove (key);
342 OnRemoveComplete (key, value);
346 /// Tests whether the dictionary contains an entry
348 bool IDictionary.Contains (object key)
350 return dictionary.Contains (key);
353 bool ICollection.IsSynchronized {
355 return dictionary.IsSynchronized;
359 object ICollection.SyncRoot {
361 return dictionary.SyncRoot;
365 IEnumerator IEnumerable.GetEnumerator ()
367 return dictionary.GetEnumerator ();