[corlib] Hunting down rare Task.WaitAll race
[mono.git] / mcs / class / corlib / System.Collections / DictionaryBase.cs
1 //
2 // System.Collections.DictionaryBase.cs
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) Ximian, Inc.  http://www.ximian.com
8 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
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:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
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.
31 //
32
33 using System;
34 using System.Runtime.InteropServices;
35
36 namespace System.Collections {
37
38         [ComVisible(true)]
39         [Serializable]
40         public abstract class DictionaryBase : IDictionary, ICollection, IEnumerable {
41
42                 Hashtable hashtable;
43                 
44                 protected DictionaryBase ()
45                 {
46                         hashtable = new Hashtable ();
47                 }
48
49                 public void Clear ()
50                 {
51                         OnClear ();
52                         hashtable.Clear ();
53                         OnClearComplete ();
54                 }
55
56                 public int Count {
57                         get {
58                                 return hashtable.Count;
59                         }
60                 }
61
62                 protected IDictionary Dictionary {
63                         get {
64                                 return this;
65                         }
66                 }
67
68                 protected Hashtable InnerHashtable {
69                         get {
70                                 return hashtable;
71                         }
72                 }
73
74                 public void CopyTo (Array array, int index)
75                 {
76                         if (array == null)
77                                 throw new ArgumentNullException ("array");
78                         if (index < 0)
79                                 throw new ArgumentOutOfRangeException ("index must be possitive");
80                         if (array.Rank > 1)
81                                 throw new ArgumentException ("array is multidimensional");
82                         int size = array.Length;
83                         if (index > size)
84                                 throw new ArgumentException ("index is larger than array size");
85                         if (index + Count > size)
86                                 throw new ArgumentException ("Copy will overlflow array");
87
88                         DoCopy (array, index);
89                 }
90
91                 private void DoCopy (Array array, int index)
92                 {
93                         foreach (DictionaryEntry de in hashtable)
94                                 array.SetValue (de, index++);
95                 }
96
97                 public IDictionaryEnumerator GetEnumerator ()
98                 {
99                         return hashtable.GetEnumerator ();
100                 }
101
102                 protected virtual void OnClear ()
103                 {
104                 }
105
106                 protected virtual void OnClearComplete ()
107                 {
108                 }
109
110                 protected virtual object OnGet (object key, object currentValue)
111                 {
112                         return currentValue;
113                 }
114
115                 protected virtual void OnInsert (object key, object value)
116                 {
117                 }
118                 
119                 protected virtual void OnInsertComplete (object key, object value)
120                 {
121                 }
122
123                 protected virtual void OnSet (object key, object oldValue, object newValue)
124                 {
125                 }
126                 
127                 protected virtual void OnSetComplete (object key, object oldValue, object newValue)
128                 {
129                 }
130
131                 protected virtual void OnRemove (object key, object value)
132                 {
133                 }
134                 
135                 protected virtual void OnRemoveComplete (object key, object value)
136                 {
137                 }
138                 
139                 protected virtual void OnValidate (object key, object value)
140                 {
141                 }
142
143                 bool IDictionary.IsFixedSize {
144                         get {
145                                 return false;
146                         }
147                 }
148
149                 bool IDictionary.IsReadOnly {
150                         get {
151                                 return false;
152                         }
153                 }
154
155                 object IDictionary.this [object key] {
156                         get {
157                                 object value = hashtable [key];
158                                 OnGet (key, value);
159                                 return value;
160                         }
161                         set {
162                                 OnValidate (key, value);
163                                 object current_value = hashtable [key];
164                                 OnSet (key, current_value, value);
165                                 hashtable [key] = value;
166                                 try {
167                                         OnSetComplete (key, current_value, value);
168                                 } catch {
169                                         hashtable [key] = current_value;
170                                         throw;
171                                 }
172                         }
173                 }
174
175                 ICollection IDictionary.Keys {
176                         get {
177                                 return hashtable.Keys;
178                         }
179                 }
180
181                 ICollection IDictionary.Values {
182                         get {
183                                 return hashtable.Values;
184                         }
185                 }
186
187                 void IDictionary.Add (object key, object value)
188                 {
189                         OnValidate (key, value);
190                         OnInsert (key, value);
191                         hashtable.Add (key, value);
192                         try {
193                                 OnInsertComplete (key, value);
194                         } catch {
195                                 hashtable.Remove (key);
196                                 throw;
197                         }
198                 }
199
200                 void IDictionary.Remove (object key)
201                 {
202                         if (!hashtable.Contains (key))
203                                 return;
204
205                         object value = hashtable [key];
206                         OnValidate (key, value);
207                         OnRemove (key, value);
208                         hashtable.Remove (key);
209                         try {
210                                 OnRemoveComplete (key, value);
211                         } catch {
212                                 hashtable [key] = value;
213                                 throw;
214                         }
215                 }
216
217                 bool IDictionary.Contains (object key)
218                 {
219                         return hashtable.Contains (key);
220                 }
221
222                 bool ICollection.IsSynchronized {
223                         get {
224                                 return hashtable.IsSynchronized;
225                         }
226                 }
227
228                 object ICollection.SyncRoot {
229                         get {
230                                 return hashtable.SyncRoot;
231                         }
232                 }
233
234                 IEnumerator IEnumerable.GetEnumerator ()
235                 {
236                         return hashtable.GetEnumerator ();
237                 }
238         }
239 }