Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / class / corlib / System.Collections.Concurrent / ConcurrentDictionary.cs
index 97f16c660c2ac1015a975bd09883e02aa2148bed..2bfb952af7d33c0aaeb4fe7c33e2246e4b819528 100644 (file)
@@ -50,8 +50,6 @@ namespace System.Collections.Concurrent
                public ConcurrentDictionary (IEnumerable<KeyValuePair<TKey, TValue>> collection)
                        : this (collection, EqualityComparer<TKey>.Default)
                {
-                       foreach (KeyValuePair<TKey, TValue> pair in collection)
-                               Add (pair.Key, pair.Value);
                }
 
                public ConcurrentDictionary (IEqualityComparer<TKey> comparer)
@@ -89,6 +87,12 @@ namespace System.Collections.Concurrent
 
                }
 
+               void CheckKey (TKey key)
+               {
+                       if (key == null)
+                               throw new ArgumentNullException ("key");
+               }
+
                void Add (TKey key, TValue value)
                {
                        while (!TryAdd (key, value));
@@ -101,6 +105,7 @@ namespace System.Collections.Concurrent
 
                public bool TryAdd (TKey key, TValue value)
                {
+                       CheckKey (key);
                        return internalDictionary.Insert (Hash (key), key, Make (key, value));
                }
 
@@ -111,6 +116,11 @@ namespace System.Collections.Concurrent
 
                public TValue AddOrUpdate (TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
                {
+                       CheckKey (key);
+                       if (addValueFactory == null)
+                               throw new ArgumentNullException ("addValueFactory");
+                       if (updateValueFactory == null)
+                               throw new ArgumentNullException ("updateValueFactory");
                        return internalDictionary.InsertOrUpdate (Hash (key),
                                                                  key,
                                                                  () => Make (key, addValueFactory (key)),
@@ -124,6 +134,7 @@ namespace System.Collections.Concurrent
 
                TValue AddOrUpdate (TKey key, TValue addValue, TValue updateValue)
                {
+                       CheckKey (key);
                        return internalDictionary.InsertOrUpdate (Hash (key),
                                                                  key,
                                                                  Make (key, addValue),
@@ -140,6 +151,7 @@ namespace System.Collections.Concurrent
 
                public bool TryGetValue (TKey key, out TValue value)
                {
+                       CheckKey (key);
                        KeyValuePair<TKey, TValue> pair;
                        bool result = internalDictionary.Find (Hash (key), key, out pair);
                        value = pair.Value;
@@ -149,6 +161,7 @@ namespace System.Collections.Concurrent
 
                public bool TryUpdate (TKey key, TValue newValue, TValue comparisonValue)
                {
+                       CheckKey (key);
                        return internalDictionary.CompareExchange (Hash (key), key, Make (key, newValue), (e) => e.Value.Equals (comparisonValue));
                }
 
@@ -163,16 +176,19 @@ namespace System.Collections.Concurrent
 
                public TValue GetOrAdd (TKey key, Func<TKey, TValue> valueFactory)
                {
+                       CheckKey (key);
                        return internalDictionary.InsertOrGet (Hash (key), key, Make (key, default(TValue)), () => Make (key, valueFactory (key))).Value;
                }
 
                public TValue GetOrAdd (TKey key, TValue value)
                {
+                       CheckKey (key);
                        return internalDictionary.InsertOrGet (Hash (key), key, Make (key, value), null).Value;
                }
 
                public bool TryRemove (TKey key, out TValue value)
                {
+                       CheckKey (key);
                        KeyValuePair<TKey, TValue> data;
                        bool result = internalDictionary.Delete (Hash (key), key, out data);
                        value = data.Value;
@@ -198,6 +214,7 @@ namespace System.Collections.Concurrent
 
                public bool ContainsKey (TKey key)
                {
+                       CheckKey (key);
                        KeyValuePair<TKey, TValue> dummy;
                        return internalDictionary.Find (Hash (key), key, out dummy);
                }