From: Ben Adams Date: Wed, 7 Sep 2016 15:51:49 +0000 (+0100) Subject: ConcurrentDictionary Exceptions to ThrowHelper (#7079) X-Git-Tag: accepted/tizen/base/20180629.140029~3634 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b8b530f8a928b6419ab9d944937e3480ec6c256a;p=platform%2Fupstream%2Fcoreclr.git ConcurrentDictionary Exceptions to ThrowHelper (#7079) --- diff --git a/src/mscorlib/src/System/Collections/Concurrent/ConcurrentDictionary.cs b/src/mscorlib/src/System/Collections/Concurrent/ConcurrentDictionary.cs index d805dc8..c704ea3 100644 --- a/src/mscorlib/src/System/Collections/Concurrent/ConcurrentDictionary.cs +++ b/src/mscorlib/src/System/Collections/Concurrent/ConcurrentDictionary.cs @@ -229,7 +229,7 @@ namespace System.Collections.Concurrent public ConcurrentDictionary(IEnumerable> collection, IEqualityComparer 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> collection, IEqualityComparer 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 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. 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). 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). 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 valueComparer = EqualityComparer.Default; @@ -642,8 +642,8 @@ namespace System.Collections.Concurrent [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "ConcurrencyCop just doesn't know about these locks")] void ICollection>.CopyTo(KeyValuePair[] 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. public TValue GetOrAdd(TKey key, Func 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. 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). public TValue AddOrUpdate(TKey key, Func addValueFactory, Func 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). public TValue AddOrUpdate(TKey key, TValue addValue, Func 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). bool ICollection>.Remove(KeyValuePair 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 /// 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)this).Add((TKey)key, typedValue); @@ -1415,7 +1414,7 @@ namespace System.Collections.Concurrent /// (Nothing in Visual Basic). bool IDictionary.Contains(object key) { - if (key == null) throw new ArgumentNullException("key"); + if (key == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key); return (key is TKey) && ((ConcurrentDictionary)this).ContainsKey((TKey)key); } @@ -1475,7 +1474,7 @@ namespace System.Collections.Concurrent /// (Nothing in Visual Basic). 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)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, @@ -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 } /// - /// A helper function to obtain the string for a particular resource key. - /// - /// - /// - private string GetResource(string key) - { - Assert(key != null); - - return Environment.GetResourceString(key); - } - - /// /// A node in a singly-linked list representing a particular hash table bucket. /// private class Node diff --git a/src/mscorlib/src/System/ThrowHelper.cs b/src/mscorlib/src/System/ThrowHelper.cs index 3105d56..d3f06ac 100644 --- a/src/mscorlib/src/System/ThrowHelper.cs +++ b/src/mscorlib/src/System/ThrowHelper.cs @@ -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, } }