Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

ConcurrentDictionary<TKey, TValue> Exceptions to ThrowHelper #7079

Merged
merged 1 commit into from
Sep 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 44 additions & 56 deletions src/mscorlib/src/System/Collections/Concurrent/ConcurrentDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public ConcurrentDictionary(IEqualityComparer<TKey> comparer) : this(DefaultConc
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);
}
Expand Down Expand Up @@ -259,8 +259,8 @@ public ConcurrentDictionary(
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);
}
Expand All @@ -270,11 +270,11 @@ private void InitializeFromCollection(IEnumerable<KeyValuePair<TKey, TValue>> co
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);
}
}

Expand Down Expand Up @@ -312,13 +312,13 @@ internal ConcurrentDictionary(int concurrencyLevel, int capacity, bool growLockA
{
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.
Expand Down Expand Up @@ -358,7 +358,7 @@ internal ConcurrentDictionary(int concurrencyLevel, int capacity, bool growLockA
/// 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);
}
Expand All @@ -375,7 +375,7 @@ public bool TryAdd(TKey key, TValue value)
/// (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);
Expand All @@ -395,7 +395,7 @@ public bool ContainsKey(TKey key)
/// (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));
}
Expand Down Expand Up @@ -486,7 +486,7 @@ private bool TryRemoveInternal(TKey key, out TValue value, bool matchValue, TVal
[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;

Expand Down Expand Up @@ -531,7 +531,7 @@ public bool TryGetValue(TKey key, out TValue value)
[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;

Expand Down Expand Up @@ -642,8 +642,8 @@ public void Clear()
[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
Expand All @@ -659,7 +659,7 @@ void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[]

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);
Expand Down Expand Up @@ -956,13 +956,13 @@ public TValue this[TKey key]
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);
}
Expand Down Expand Up @@ -1026,8 +1026,8 @@ public int Count
/// 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))
Expand All @@ -1052,7 +1052,7 @@ public TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory)
/// 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);
Expand Down Expand Up @@ -1080,9 +1080,9 @@ public TValue GetOrAdd(TKey key, TValue value)
/// 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)
Expand Down Expand Up @@ -1127,8 +1127,8 @@ public TValue AddOrUpdate(TKey key, Func<TKey, TValue> addValueFactory, Func<TKe
/// 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)
{
Expand Down Expand Up @@ -1207,7 +1207,7 @@ void IDictionary<TKey, TValue>.Add(TKey key, TValue value)
{
if (!TryAdd(key, value))
{
throw new ArgumentException(GetResource("ConcurrentDictionary_KeyAlreadyExisted"));
ThrowHelper.ThrowArgumentException(ExceptionResource.ConcurrentDictionary_KeyAlreadyExisted);
}
}

Expand Down Expand Up @@ -1340,8 +1340,7 @@ bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly
/// 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);
}
Expand Down Expand Up @@ -1387,17 +1386,17 @@ IEnumerator IEnumerable.GetEnumerator()
/// </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);
Expand All @@ -1415,7 +1414,7 @@ void IDictionary.Add(object key, object value)
/// (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);
}
Expand Down Expand Up @@ -1475,7 +1474,7 @@ ICollection IDictionary.Keys
/// (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)
Expand Down Expand Up @@ -1517,7 +1516,7 @@ object IDictionary.this[object key]
{
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))
Expand All @@ -1529,10 +1528,10 @@ object IDictionary.this[object key]
}
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;
}
Expand Down Expand Up @@ -1563,8 +1562,8 @@ object IDictionary.this[object key]
[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
Expand All @@ -1581,7 +1580,7 @@ void ICollection.CopyTo(Array array, int index)

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>,
Expand Down Expand Up @@ -1611,7 +1610,7 @@ void ICollection.CopyTo(Array array, int index)
return;
}

throw new ArgumentException(GetResource("ConcurrentDictionary_ArrayIncorrectType"), "array");
ThrowHelper.ThrowArgumentException(ExceptionResource.ConcurrentDictionary_ArrayIncorrectType, ExceptionArgument.array);
}
finally
{
Expand Down Expand Up @@ -1641,7 +1640,8 @@ object ICollection.SyncRoot
{
get
{
throw new NotSupportedException(Environment.GetResourceString("ConcurrentCollection_SyncRoot_NotSupported"));
ThrowHelper.ThrowNotSupportedException(ExceptionResource.ConcurrentCollection_SyncRoot_NotSupported);
return default(object);
}
}

Expand Down Expand Up @@ -1976,18 +1976,6 @@ private void Assert(bool condition)
Contract.Assert(condition);
}

/// <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>
Expand Down
20 changes: 20 additions & 0 deletions src/mscorlib/src/System/ThrowHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ internal static void ThrowArgumentNullException(ExceptionArgument argument) {
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));
}
Expand Down Expand Up @@ -247,6 +252,10 @@ internal enum ExceptionArgument {
beginMethod,
continuationOptions,
continuationAction,
valueFactory,
addValueFactory,
updateValueFactory,
concurrencyLevel,

}

Expand Down Expand Up @@ -341,6 +350,17 @@ internal enum ExceptionResource {
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,

}
}
Expand Down