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.
34 using System.Runtime.InteropServices;
36 namespace System.Collections {
42 public abstract class DictionaryBase : IDictionary, ICollection, IEnumerable {
46 protected DictionaryBase ()
48 hashtable = new Hashtable ();
60 return hashtable.Count;
64 protected IDictionary Dictionary {
70 protected Hashtable InnerHashtable {
76 public void CopyTo (Array array, int index)
79 throw new ArgumentNullException ("array");
81 throw new ArgumentOutOfRangeException ("index must be possitive");
83 throw new ArgumentException ("array is multidimensional");
84 int size = array.Length;
86 throw new ArgumentException ("index is larger than array size");
87 if (index + Count > size)
88 throw new ArgumentException ("Copy will overlflow array");
90 DoCopy (array, index);
93 private void DoCopy (Array array, int index)
95 foreach (DictionaryEntry de in hashtable)
96 array.SetValue (de, index++);
99 public IDictionaryEnumerator GetEnumerator ()
101 return hashtable.GetEnumerator ();
104 protected virtual void OnClear ()
108 protected virtual void OnClearComplete ()
112 protected virtual object OnGet (object key, object current_value)
114 return current_value;
117 protected virtual void OnInsert (object key, object current_value)
121 protected virtual void OnInsertComplete (object key, object current_value)
125 protected virtual void OnSet (object key, object current_value, object new_value)
129 protected virtual void OnSetComplete (object key, object current_value, object new_value)
133 protected virtual void OnRemove (object key, object current_value)
137 protected virtual void OnRemoveComplete (object key, object current_value)
141 protected virtual void OnValidate (object key, object current_value)
145 bool IDictionary.IsFixedSize {
151 bool IDictionary.IsReadOnly {
157 object IDictionary.this [object key] {
159 OnGet (key, hashtable [key]);
160 object value = hashtable [key];
165 OnValidate (key, value);
166 object current_value = hashtable [key];
167 OnSet (key, current_value, value);
168 hashtable [key] = value;
170 OnSetComplete (key, current_value, value);
172 hashtable [key] = current_value;
178 ICollection IDictionary.Keys {
180 return hashtable.Keys;
184 ICollection IDictionary.Values {
186 return hashtable.Values;
190 void IDictionary.Add (object key, object value)
192 OnValidate (key, value);
193 OnInsert (key, value);
194 hashtable.Add (key, value);
196 OnInsertComplete (key, value);
198 hashtable.Remove (key);
203 void IDictionary.Remove (object key)
205 object value = hashtable [key];
206 OnValidate (key, value);
207 OnRemove (key, value);
208 hashtable.Remove (key);
209 OnRemoveComplete (key, value);
212 bool IDictionary.Contains (object key)
214 return hashtable.Contains (key);
217 bool ICollection.IsSynchronized {
219 return hashtable.IsSynchronized;
223 object ICollection.SyncRoot {
225 return hashtable.SyncRoot;
229 IEnumerator IEnumerable.GetEnumerator ()
231 return hashtable.GetEnumerator ();