Mon Dec 5 15:14:59 CET 2005 Paolo Molaro <lupus@ximian.com>
[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 #if NET_2_0
39         [ComVisible(true)]
40 #endif
41         [Serializable]
42         public abstract class DictionaryBase : IDictionary, ICollection, IEnumerable {
43
44                 Hashtable hashtable;
45                 
46                 protected DictionaryBase ()
47                 {
48                         hashtable = new Hashtable ();
49                 }
50
51                 public void Clear ()
52                 {
53                         OnClear ();
54                         hashtable.Clear ();
55                         OnClearComplete ();
56                 }
57
58                 public int Count {
59                         get {
60                                 return hashtable.Count;
61                         }
62                 }
63
64                 protected IDictionary Dictionary {
65                         get {
66                                 return this;
67                         }
68                 }
69
70                 protected Hashtable InnerHashtable {
71                         get {
72                                 return hashtable;
73                         }
74                 }
75
76                 public void CopyTo (Array array, int index)
77                 {
78                         if (array == null)
79                                 throw new ArgumentNullException ("array");
80                         if (index < 0)
81                                 throw new ArgumentOutOfRangeException ("index must be possitive");
82                         if (array.Rank > 1)
83                                 throw new ArgumentException ("array is multidimensional");
84                         int size = array.Length;
85                         if (index > size)
86                                 throw new ArgumentException ("index is larger than array size");
87                         if (index + Count > size)
88                                 throw new ArgumentException ("Copy will overlflow array");
89
90                         DoCopy (array, index);
91                 }
92
93                 private void DoCopy (Array array, int index)
94                 {
95                         foreach (DictionaryEntry de in hashtable)
96                                 array.SetValue (de, index++);
97                 }
98
99                 public IDictionaryEnumerator GetEnumerator ()
100                 {
101                         return hashtable.GetEnumerator ();
102                 }
103
104                 protected virtual void OnClear ()
105                 {
106                 }
107
108                 protected virtual void OnClearComplete ()
109                 {
110                 }
111
112                 protected virtual object OnGet (object key, object current_value)
113                 {
114                         return current_value;
115                 }
116
117                 protected virtual void OnInsert (object key, object current_value)
118                 {
119                 }
120                 
121                 protected virtual void OnInsertComplete (object key, object current_value)
122                 {
123                 }
124
125                 protected virtual void OnSet (object key, object current_value, object new_value)
126                 {
127                 }
128                 
129                 protected virtual void OnSetComplete (object key, object current_value, object new_value)
130                 {
131                 }
132
133                 protected virtual void OnRemove (object key, object current_value)
134                 {
135                 }
136                 
137                 protected virtual void OnRemoveComplete (object key, object current_value)
138                 {
139                 }
140                 
141                 protected virtual void OnValidate (object key, object current_value)
142                 {
143                 }
144
145                 bool IDictionary.IsFixedSize {
146                         get {
147                                 return false;
148                         }
149                 }
150
151                 bool IDictionary.IsReadOnly {
152                         get {
153                                 return false;
154                         }
155                 }
156
157                 object IDictionary.this [object key] {
158                         get {
159                                 OnGet (key, hashtable [key]);
160                                 object value = hashtable [key];
161                                 return value;
162                         }
163
164                         set {
165                                 OnValidate (key, value);
166                                 object current_value = hashtable [key];
167                                 OnSet (key, current_value, value);
168                                 hashtable [key] = value;
169                                 try {
170                                         OnSetComplete (key, current_value, value);
171                                 } catch {
172                                         hashtable [key] = current_value;
173                                         throw;
174                                 }
175                         }
176                 }
177
178                 ICollection IDictionary.Keys {
179                         get {
180                                 return hashtable.Keys;
181                         }
182                 }
183
184                 ICollection IDictionary.Values {
185                         get {
186                                 return hashtable.Values;
187                         }
188                 }
189
190                 void IDictionary.Add (object key, object value)
191                 {
192                         OnValidate (key, value);
193                         OnInsert (key, value);
194                         hashtable.Add (key, value);
195                         try {
196                                 OnInsertComplete (key, value);
197                         } catch {
198                                 hashtable.Remove (key);
199                                 throw;
200                         }
201                 }
202
203                 void IDictionary.Remove (object key)
204                 {
205                         object value = hashtable [key];
206                         OnValidate (key, value);
207                         OnRemove (key, value);
208                         hashtable.Remove (key);
209                         OnRemoveComplete (key, value);
210                 }
211
212                 bool IDictionary.Contains (object key)
213                 {
214                         return hashtable.Contains (key);
215                 }
216
217                 bool ICollection.IsSynchronized {
218                         get {
219                                 return hashtable.IsSynchronized;
220                         }
221                 }
222
223                 object ICollection.SyncRoot {
224                         get {
225                                 return hashtable.SyncRoot;
226                         }
227                 }
228
229                 IEnumerator IEnumerable.GetEnumerator ()
230                 {
231                         return hashtable.GetEnumerator ();
232                 }
233         }
234 }