ConcurrentDictionary<TKey, TValue> Exceptions to ThrowHelper (#7079)
authorBen Adams <thundercat@illyriad.co.uk>
Wed, 7 Sep 2016 15:51:49 +0000 (16:51 +0100)
committerJan Kotas <jkotas@microsoft.com>
Wed, 7 Sep 2016 15:51:49 +0000 (08:51 -0700)
src/mscorlib/src/System/Collections/Concurrent/ConcurrentDictionary.cs
src/mscorlib/src/System/ThrowHelper.cs

index d805dc8..c704ea3 100644 (file)
@@ -229,7 +229,7 @@ namespace System.Collections.Concurrent
         public ConcurrentDictionary(IEnumerable<KeyValuePair<TKey, TValue>> collection, IEqualityComparer<TKey> comparer)
             : this(comparer)
         {
-            if (collection == null) throw new ArgumentNullException("collection");
+            if (collection == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
 
             InitializeFromCollection(collection);
         }
@@ -259,8 +259,8 @@ namespace System.Collections.Concurrent
             int concurrencyLevel, IEnumerable<KeyValuePair<TKey, TValue>> collection, IEqualityComparer<TKey> comparer)
             : this(concurrencyLevel, DEFAULT_CAPACITY, false, comparer)
         {
-            if (collection == null) throw new ArgumentNullException("collection");
-            if (comparer == null) throw new ArgumentNullException("comparer");
+            if (collection == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
+            if (comparer == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.comparer);
 
             InitializeFromCollection(collection);
         }
@@ -270,11 +270,11 @@ namespace System.Collections.Concurrent
             TValue dummy;
             foreach (KeyValuePair<TKey, TValue> pair in collection)
             {
-                if (pair.Key == null) throw new ArgumentNullException("key");
+                if (pair.Key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
 
                 if (!TryAddInternal(pair.Key, pair.Value, false, false, out dummy))
                 {
-                    throw new ArgumentException(GetResource("ConcurrentDictionary_SourceContainsDuplicateKeys"));
+                    ThrowHelper.ThrowArgumentException(ExceptionResource.ConcurrentDictionary_SourceContainsDuplicateKeys);
                 }
             }
 
@@ -312,13 +312,13 @@ namespace System.Collections.Concurrent
         {
             if (concurrencyLevel < 1)
             {
-                throw new ArgumentOutOfRangeException("concurrencyLevel", GetResource("ConcurrentDictionary_ConcurrencyLevelMustBePositive"));
+                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.concurrencyLevel, ExceptionResource.ConcurrentDictionary_ConcurrencyLevelMustBePositive);
             }
             if (capacity < 0)
             {
-                throw new ArgumentOutOfRangeException("capacity", GetResource("ConcurrentDictionary_CapacityMustNotBeNegative"));
+                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.capacity, ExceptionResource.ConcurrentDictionary_CapacityMustNotBeNegative);
             }
-            if (comparer == null) throw new ArgumentNullException("comparer");
+            if (comparer == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.comparer);
 
             // The capacity should be at least as large as the concurrency level. Otherwise, we would have locks that don't guard
             // any buckets.
@@ -358,7 +358,7 @@ namespace System.Collections.Concurrent
         /// contains too many elements.</exception>
         public bool TryAdd(TKey key, TValue value)
         {
-            if (key == null) throw new ArgumentNullException("key");
+            if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
             TValue dummy;
             return TryAddInternal(key, value, false, true, out dummy);
         }
@@ -375,7 +375,7 @@ namespace System.Collections.Concurrent
         /// (Nothing in Visual Basic).</exception>
         public bool ContainsKey(TKey key)
         {
-            if (key == null) throw new ArgumentNullException("key");
+            if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
 
             TValue throwAwayValue;
             return TryGetValue(key, out throwAwayValue);
@@ -395,7 +395,7 @@ namespace System.Collections.Concurrent
         /// (Nothing in Visual Basic).</exception>
         public bool TryRemove(TKey key, out TValue value)
         {
-            if (key == null) throw new ArgumentNullException("key");
+            if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
 
             return TryRemoveInternal(key, out value, false, default(TValue));
         }
@@ -486,7 +486,7 @@ namespace System.Collections.Concurrent
         [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "Reviewed for thread safety")]
         public bool TryGetValue(TKey key, out TValue value)
         {
-            if (key == null) throw new ArgumentNullException("key");
+            if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
 
             int bucketNo, lockNoUnused;
 
@@ -531,7 +531,7 @@ namespace System.Collections.Concurrent
         [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "Reviewed for thread safety")]
         public bool TryUpdate(TKey key, TValue newValue, TValue comparisonValue)
         {
-            if (key == null) throw new ArgumentNullException("key");
+            if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
 
             IEqualityComparer<TValue> valueComparer = EqualityComparer<TValue>.Default;
 
@@ -642,8 +642,8 @@ namespace System.Collections.Concurrent
         [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "ConcurrencyCop just doesn't know about these locks")]
         void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int index)
         {
-            if (array == null) throw new ArgumentNullException("array");
-            if (index < 0) throw new ArgumentOutOfRangeException("index", GetResource("ConcurrentDictionary_IndexIsNegative"));
+            if (array == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
+            if (index < 0) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ConcurrentDictionary_IndexIsNegative);
 
             int locksAcquired = 0;
             try
@@ -659,7 +659,7 @@ namespace System.Collections.Concurrent
 
                 if (array.Length - count < index || count < 0) //"count" itself or "count + index" can overflow
                 {
-                    throw new ArgumentException(GetResource("ConcurrentDictionary_ArrayNotLargeEnough"));
+                    ThrowHelper.ThrowArgumentException(ExceptionResource.ConcurrentDictionary_ArrayNotLargeEnough);
                 }
 
                 CopyToPairs(array, index);
@@ -956,13 +956,13 @@ namespace System.Collections.Concurrent
                 TValue value;
                 if (!TryGetValue(key, out value))
                 {
-                    throw new KeyNotFoundException();
+                    ThrowHelper.ThrowKeyNotFoundException();
                 }
                 return value;
             }
             set
             {
-                if (key == null) throw new ArgumentNullException("key");
+                if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
                 TValue dummy;
                 TryAddInternal(key, value, true, true, out dummy);
             }
@@ -1026,8 +1026,8 @@ namespace System.Collections.Concurrent
         /// if the key was not in the dictionary.</returns>
         public TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory)
         {
-            if (key == null) throw new ArgumentNullException("key");
-            if (valueFactory == null) throw new ArgumentNullException("valueFactory");
+            if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
+            if (valueFactory == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.valueFactory);
 
             TValue resultingValue;
             if (TryGetValue(key, out resultingValue))
@@ -1052,7 +1052,7 @@ namespace System.Collections.Concurrent
         /// key is already in the dictionary, or the new value if the key was not in the dictionary.</returns>
         public TValue GetOrAdd(TKey key, TValue value)
         {
-            if (key == null) throw new ArgumentNullException("key");
+            if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
 
             TValue resultingValue;
             TryAddInternal(key, value, false, true, out resultingValue);
@@ -1080,9 +1080,9 @@ namespace System.Collections.Concurrent
         /// absent) or the result of updateValueFactory (if the key was present).</returns>
         public TValue AddOrUpdate(TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
         {
-            if (key == null) throw new ArgumentNullException("key");
-            if (addValueFactory == null) throw new ArgumentNullException("addValueFactory");
-            if (updateValueFactory == null) throw new ArgumentNullException("updateValueFactory");
+            if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
+            if (addValueFactory == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.addValueFactory);
+            if (updateValueFactory == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.updateValueFactory);
 
             TValue newValue, resultingValue;
             while (true)
@@ -1127,8 +1127,8 @@ namespace System.Collections.Concurrent
         /// absent) or the result of updateValueFactory (if the key was present).</returns>
         public TValue AddOrUpdate(TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
         {
-            if (key == null) throw new ArgumentNullException("key");
-            if (updateValueFactory == null) throw new ArgumentNullException("updateValueFactory");
+            if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
+            if (updateValueFactory == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.updateValueFactory);
             TValue newValue, resultingValue;
             while (true)
             {
@@ -1207,7 +1207,7 @@ namespace System.Collections.Concurrent
         {
             if (!TryAdd(key, value))
             {
-                throw new ArgumentException(GetResource("ConcurrentDictionary_KeyAlreadyExisted"));
+                ThrowHelper.ThrowArgumentException(ExceptionResource.ConcurrentDictionary_KeyAlreadyExisted);
             }
         }
 
@@ -1340,8 +1340,7 @@ namespace System.Collections.Concurrent
         /// name="keyValuePair"/> is a null reference (Nothing in Visual Basic).</exception>
         bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> keyValuePair)
         {
-            if (keyValuePair.Key == null) throw new ArgumentNullException(GetResource("ConcurrentDictionary_ItemKeyIsNull"));
-
+            if (keyValuePair.Key == null) ThrowHelper.ThrowArgumentNullException(ExceptionResource.ConcurrentDictionary_ItemKeyIsNull);
             TValue throwAwayValue;
             return TryRemoveInternal(keyValuePair.Key, out throwAwayValue, true, keyValuePair.Value);
         }
@@ -1387,17 +1386,17 @@ namespace System.Collections.Concurrent
         /// </exception>
         void IDictionary.Add(object key, object value)
         {
-            if (key == null) throw new ArgumentNullException("key");
-            if (!(key is TKey)) throw new ArgumentException(GetResource("ConcurrentDictionary_TypeOfKeyIncorrect"));
+            if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
+            if (!(key is TKey)) ThrowHelper.ThrowArgumentException(ExceptionResource.ConcurrentDictionary_TypeOfKeyIncorrect);
 
-            TValue typedValue;
+            TValue typedValue = default(TValue);
             try
             {
                 typedValue = (TValue)value;
             }
             catch (InvalidCastException)
             {
-                throw new ArgumentException(GetResource("ConcurrentDictionary_TypeOfValueIncorrect"));
+                ThrowHelper.ThrowArgumentException(ExceptionResource.ConcurrentDictionary_TypeOfValueIncorrect);
             }
 
             ((IDictionary<TKey, TValue>)this).Add((TKey)key, typedValue);
@@ -1415,7 +1414,7 @@ namespace System.Collections.Concurrent
         /// (Nothing in Visual Basic).</exception>
         bool IDictionary.Contains(object key)
         {
-            if (key == null) throw new ArgumentNullException("key");
+            if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
 
             return (key is TKey) && ((ConcurrentDictionary<TKey, TValue>)this).ContainsKey((TKey)key);
         }
@@ -1475,7 +1474,7 @@ namespace System.Collections.Concurrent
         /// (Nothing in Visual Basic).</exception>
         void IDictionary.Remove(object key)
         {
-            if (key == null) throw new ArgumentNullException("key");
+            if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
 
             TValue throwAwayValue;
             if (key is TKey)
@@ -1517,7 +1516,7 @@ namespace System.Collections.Concurrent
         {
             get
             {
-                if (key == null) throw new ArgumentNullException("key");
+                if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
 
                 TValue value;
                 if (key is TKey && this.TryGetValue((TKey)key, out value))
@@ -1529,10 +1528,10 @@ namespace System.Collections.Concurrent
             }
             set
             {
-                if (key == null) throw new ArgumentNullException("key");
+                if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
 
-                if (!(key is TKey)) throw new ArgumentException(GetResource("ConcurrentDictionary_TypeOfKeyIncorrect"));
-                if (!(value is TValue)) throw new ArgumentException(GetResource("ConcurrentDictionary_TypeOfValueIncorrect"));
+                if (!(key is TKey)) ThrowHelper.ThrowArgumentException(ExceptionResource.ConcurrentDictionary_TypeOfKeyIncorrect);
+                if (!(value is TValue)) ThrowHelper.ThrowArgumentException(ExceptionResource.ConcurrentDictionary_TypeOfValueIncorrect);
 
                 ((ConcurrentDictionary<TKey, TValue>)this)[(TKey)key] = (TValue)value;
             }
@@ -1563,8 +1562,8 @@ namespace System.Collections.Concurrent
         [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "ConcurrencyCop just doesn't know about these locks")]
         void ICollection.CopyTo(Array array, int index)
         {
-            if (array == null) throw new ArgumentNullException("array");
-            if (index < 0) throw new ArgumentOutOfRangeException("index", GetResource("ConcurrentDictionary_IndexIsNegative"));
+            if (array == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
+            if (index < 0) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ConcurrentDictionary_IndexIsNegative);
 
             int locksAcquired = 0;
             try
@@ -1581,7 +1580,7 @@ namespace System.Collections.Concurrent
 
                 if (array.Length - count < index || count < 0) //"count" itself or "count + index" can overflow
                 {
-                    throw new ArgumentException(GetResource("ConcurrentDictionary_ArrayNotLargeEnough"));
+                    ThrowHelper.ThrowArgumentException(ExceptionResource.ConcurrentDictionary_ArrayNotLargeEnough);
                 }
 
                 // To be consistent with the behavior of ICollection.CopyTo() in Dictionary<TKey,TValue>,
@@ -1611,7 +1610,7 @@ namespace System.Collections.Concurrent
                     return;
                 }
 
-                throw new ArgumentException(GetResource("ConcurrentDictionary_ArrayIncorrectType"), "array");
+                ThrowHelper.ThrowArgumentException(ExceptionResource.ConcurrentDictionary_ArrayIncorrectType, ExceptionArgument.array);
             }
             finally
             {
@@ -1641,7 +1640,8 @@ namespace System.Collections.Concurrent
         {
             get
             {
-                throw new NotSupportedException(Environment.GetResourceString("ConcurrentCollection_SyncRoot_NotSupported"));
+                ThrowHelper.ThrowNotSupportedException(ExceptionResource.ConcurrentCollection_SyncRoot_NotSupported);
+                return default(object);
             }
         }
 
@@ -1977,18 +1977,6 @@ namespace System.Collections.Concurrent
         }
 
         /// <summary>
-        /// A helper function to obtain the string for a particular resource key.
-        /// </summary>
-        /// <param name="key"></param>
-        /// <returns></returns>
-        private string GetResource(string key)
-        {
-            Assert(key != null);
-
-            return Environment.GetResourceString(key);
-        }
-
-        /// <summary>
         /// A node in a singly-linked list representing a particular hash table bucket.
         /// </summary>
         private class Node
index 3105d56..d3f06ac 100644 (file)
@@ -84,6 +84,11 @@ namespace System {
             throw new ArgumentNullException(GetArgumentName(argument));
         }
 
+        internal static void ThrowArgumentNullException(ExceptionResource resource)
+        {
+            throw new ArgumentNullException(Environment.GetResourceString(GetResourceName(resource)));
+        }
+
         internal static void ThrowArgumentOutOfRangeException(ExceptionArgument argument) {
             throw new ArgumentOutOfRangeException(GetArgumentName(argument));
         }
@@ -247,6 +252,10 @@ namespace System {
         beginMethod,
         continuationOptions,
         continuationAction,
+        valueFactory,
+        addValueFactory,
+        updateValueFactory,
+        concurrencyLevel,
 
     }
 
@@ -341,6 +350,17 @@ namespace System {
         TaskCompletionSourceT_TrySetException_NullException,
         TaskCompletionSourceT_TrySetException_NoExceptions,
         InvalidOperation_WrongAsyncResultOrEndCalledMultiple,
+        ConcurrentDictionary_ConcurrencyLevelMustBePositive,
+        ConcurrentDictionary_CapacityMustNotBeNegative,
+        ConcurrentDictionary_TypeOfValueIncorrect,
+        ConcurrentDictionary_TypeOfKeyIncorrect,
+        ConcurrentDictionary_SourceContainsDuplicateKeys,
+        ConcurrentDictionary_KeyAlreadyExisted,
+        ConcurrentDictionary_ItemKeyIsNull,
+        ConcurrentDictionary_IndexIsNegative,
+        ConcurrentDictionary_ArrayNotLargeEnough,
+        ConcurrentDictionary_ArrayIncorrectType,
+        ConcurrentCollection_SyncRoot_NotSupported,
 
     }
 }