2 // System.Collections.DictionaryBase.cs
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) Ximian, Inc. http://www.ximian.com
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 namespace System
.Collections
{
38 /// An abstract class that provides a simple way to monitor changes to a
39 /// Hashtable. Derived classes overwrite one or more of the `On' methods
40 /// to track the changes to the Hashtable.
44 /// This class is a base class that can simplify the development of
45 /// strongly typed collections. The idea being that the insertion of elements
46 /// into the Hashtable can be forced to be of a given type.
48 /// The `On' members are protected and designed to be used only by derived
52 public abstract class DictionaryBase
: IDictionary
, ICollection
, IEnumerable
{
56 protected DictionaryBase ()
58 hashtable
= new Hashtable ();
62 /// Clears the contents of the dictionary
72 /// Returns the number of items in the dictionary
76 return hashtable
.Count
;
81 /// The collection contained as an IDictionary
83 protected IDictionary Dictionary
{
90 /// The internal Hashtable representation for this dictionary
92 protected Hashtable InnerHashtable
{
99 /// Copies the contents of the Dictionary into the target array
101 /// <param name="array">
102 /// The array to copy the contents of the dictionary to. The
103 /// array must have a zero-based indexing
105 /// <param name="index">
106 /// Starting index within the array where to copy the objects
109 public void CopyTo (Array array
, int index
)
112 throw new ArgumentNullException ("array");
114 throw new ArgumentOutOfRangeException ("index must be possitive");
116 throw new ArgumentException ("array is multidimensional");
117 int size
= array
.Length
;
119 throw new ArgumentException ("index is larger than array size");
120 if (index
+ Count
> size
)
121 throw new ArgumentException ("Copy will overlflow array");
123 DoCopy (array
, index
);
127 /// Internal routine called by CopyTo to perform the actual
128 /// copying of the data
130 private void DoCopy (Array array
, int index
)
132 foreach (DictionaryEntry de
in hashtable
)
133 array
.SetValue (de
, index
++);
137 /// Returns an enumerator for the dictionary
139 public IDictionaryEnumerator
GetEnumerator ()
141 return hashtable
.GetEnumerator ();
145 /// Hook invoked before the clear operation
146 /// is performed on the DictionaryBase
148 protected virtual void OnClear ()
153 /// Hook invoked after the clear operation
154 /// is performed on the DictionaryBase
158 /// The default implementation does nothing, derived classes
159 /// can override this method to be notified of changes
161 protected virtual void OnClearComplete ()
166 /// Hook invoked while fetching data from the DictionaryBase.
170 /// This method is provided as a simple way to override the values
171 /// returned by the DictionaryBase.
174 /// <param name="key">Key of the object to retrieve</param>
175 /// <param name="current_value">Current value of the object associated with
176 /// <paramref name="key"/></param>
177 protected virtual object OnGet (object key
, object current_value
)
179 return current_value
;
183 /// Hook invoked before inserting data into the DictionaryBase.
187 /// Derived classes can override this method and perform some
188 /// action before the <paramref name="current_value"/> is inserted
189 /// into the dictionary.
191 /// The default implementation does nothing, derived classes
192 /// can override this method to be notified of changes
195 /// <param name="key">Key of the object to insert</param>
196 /// <param name="current_value">Current value of the object associated with
197 /// <paramref name="key"/></param>
198 protected virtual void OnInsert (object key
, object current_value
)
203 /// Hook invoked after inserting the data into the DictionaryBase
207 /// The default implementation does nothing, derived classes
208 /// can override this method to be notified of changes
211 /// <param name="key">Key of the object to insert</param>
212 /// <param name="current_value">Current value of the object associated with
213 /// <paramref name="key"/></param>
214 protected virtual void OnInsertComplete (object key
, object current_value
)
219 /// Hook invoked before changing a value for a key in the DictionaryBase.
223 /// Derived classes can override this method and perform some
224 /// action before the <paramref name="current_value"/> is changed
225 /// in the dictionary.
228 /// <param name="key">Key of the object to change</param>
229 /// <param name="current_value">Current value of the object associated with
230 /// <paramref name="key"/></param>
231 protected virtual void OnSet (object key
, object current_value
, object new_value
)
236 /// Hook invoked after changing a value for a key in the DictionaryBase.
240 /// The default implementation does nothing, derived classes
241 /// can override this method to be notified of changes
244 /// <param name="key">Key of the object to change</param>
245 /// <param name="current_value">Current value of the object associated with
246 /// <paramref name="key"/></param>
247 protected virtual void OnSetComplete (object key
, object current_value
, object new_value
)
252 /// Hook invoked before removing a key/value from the DictionaryBase.
256 /// Derived classes can override this method and perform some
257 /// action before the <paramref name="current_value"/> is removed
258 /// from the dictionary.
261 /// <param name="key">Key of the object to remove</param>
262 /// <param name="current_value">Current value of the object associated with
263 /// <paramref name="key"/></param>
264 protected virtual void OnRemove (object key
, object current_value
)
269 /// Hook invoked after removing a key/value from the DictionaryBase.
273 /// The default implementation does nothing, derived classes
274 /// can override this method to be notified of changes.
277 /// <param name="key">Key of the object to remove</param>
278 /// <param name="current_value">Current value of the object associated with
279 /// <paramref name="key"/></param>
280 protected virtual void OnRemoveComplete (object key
, object current_value
)
285 /// Hook invoked after the value has been validated
289 /// The default implementation does nothing, derived classes
290 /// can override this method to monitor the DictionaryBase.
293 /// <param name="key">Key of the object to retrieve</param>
294 /// <param name="current_value">Current value of the object associated with
295 /// <paramref name="key"/></param>
296 protected virtual void OnValidate (object key
, object current_value
)
300 bool IDictionary
.IsFixedSize
{
306 bool IDictionary
.IsReadOnly
{
312 object IDictionary
.this [object key
] {
314 OnGet (key
, hashtable
[key
]);
315 object value = hashtable
[key
];
320 OnValidate (key
, value);
321 object current_value
= hashtable
[key
];
322 OnSet (key
, current_value
, value);
323 hashtable
[key
] = value;
325 OnSetComplete (key
, current_value
, value);
327 hashtable
[key
] = current_value
;
333 ICollection IDictionary
.Keys
{
335 return hashtable
.Keys
;
339 ICollection IDictionary
.Values
{
341 return hashtable
.Values
;
346 /// Adds a key/value pair to the dictionary.
348 void IDictionary
.Add (object key
, object value)
350 OnValidate (key
, value);
351 OnInsert (key
, value);
352 hashtable
.Add (key
, value);
354 OnInsertComplete (key
, value);
356 hashtable
.Remove (key
);
362 /// Removes a Dictionary Entry based on its key
364 void IDictionary
.Remove (object key
)
366 object value = hashtable
[key
];
367 OnValidate (key
, value);
368 OnRemove (key
, value);
369 hashtable
.Remove (key
);
370 OnRemoveComplete (key
, value);
374 /// Tests whether the dictionary contains an entry
376 bool IDictionary
.Contains (object key
)
378 return hashtable
.Contains (key
);
381 bool ICollection
.IsSynchronized
{
383 return hashtable
.IsSynchronized
;
387 object ICollection
.SyncRoot
{
389 return hashtable
.SyncRoot
;
393 IEnumerator IEnumerable
.GetEnumerator ()
395 return hashtable
.GetEnumerator ();