2010-06-21 Atsushi Enomoto <atsushi@ximian.com>
[mcs.git] / class / corlib / System.Collections / DictionaryBase.cs
blobe44a60ca10c3b89b1b7587ec639964b4cdafd476
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 //
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:
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.
33 using System;
34 using System.Runtime.InteropServices;
36 namespace System.Collections {
38 [ComVisible(true)]
39 [Serializable]
40 public abstract class DictionaryBase : IDictionary, ICollection, IEnumerable {
42 Hashtable hashtable;
44 protected DictionaryBase ()
46 hashtable = new Hashtable ();
49 public void Clear ()
51 OnClear ();
52 hashtable.Clear ();
53 OnClearComplete ();
56 public int Count {
57 get {
58 return hashtable.Count;
62 protected IDictionary Dictionary {
63 get {
64 return this;
68 protected Hashtable InnerHashtable {
69 get {
70 return hashtable;
74 public void CopyTo (Array array, int index)
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");
88 DoCopy (array, index);
91 private void DoCopy (Array array, int index)
93 foreach (DictionaryEntry de in hashtable)
94 array.SetValue (de, index++);
97 public IDictionaryEnumerator GetEnumerator ()
99 return hashtable.GetEnumerator ();
102 protected virtual void OnClear ()
106 protected virtual void OnClearComplete ()
110 protected virtual object OnGet (object key, object currentValue)
112 return currentValue;
115 protected virtual void OnInsert (object key, object value)
119 protected virtual void OnInsertComplete (object key, object value)
123 protected virtual void OnSet (object key, object oldValue, object newValue)
127 protected virtual void OnSetComplete (object key, object oldValue, object newValue)
131 protected virtual void OnRemove (object key, object value)
135 protected virtual void OnRemoveComplete (object key, object value)
139 protected virtual void OnValidate (object key, object value)
143 bool IDictionary.IsFixedSize {
144 get {
145 return false;
149 bool IDictionary.IsReadOnly {
150 get {
151 return false;
155 object IDictionary.this [object key] {
156 get {
157 object value = hashtable [key];
158 OnGet (key, value);
159 return value;
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;
175 ICollection IDictionary.Keys {
176 get {
177 return hashtable.Keys;
181 ICollection IDictionary.Values {
182 get {
183 return hashtable.Values;
187 void IDictionary.Add (object key, object value)
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;
200 void IDictionary.Remove (object key)
202 if (!hashtable.Contains (key))
203 return;
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;
217 bool IDictionary.Contains (object key)
219 return hashtable.Contains (key);
222 bool ICollection.IsSynchronized {
223 get {
224 return hashtable.IsSynchronized;
228 object ICollection.SyncRoot {
229 get {
230 return hashtable.SyncRoot;
234 IEnumerator IEnumerable.GetEnumerator ()
236 return hashtable.GetEnumerator ();