}
+ void CheckKey (TKey key)
+ {
+ if (key == null)
+ throw new ArgumentNullException ("key");
+ }
+
void Add (TKey key, TValue value)
{
while (!TryAdd (key, value));
public bool TryAdd (TKey key, TValue value)
{
+ CheckKey (key);
return internalDictionary.Insert (Hash (key), key, Make (key, value));
}
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)),
TValue AddOrUpdate (TKey key, TValue addValue, TValue updateValue)
{
+ CheckKey (key);
return internalDictionary.InsertOrUpdate (Hash (key),
key,
Make (key, addValue),
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;
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));
}
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;
public bool ContainsKey (TKey key)
{
+ CheckKey (key);
KeyValuePair<TKey, TValue> dummy;
return internalDictionary.Find (Hash (key), key, out dummy);
}
foreach (var id in ids)
Assert.IsFalse (dict.TryGetValue (id, out result), id.ToString () + " (second)");
}
+
+ [Test]
+ public void NullArgumentsTest ()
+ {
+ AssertThrowsArgumentNullException (() => { var x = map[null]; });
+ AssertThrowsArgumentNullException (() => map[null] = 0);
+ AssertThrowsArgumentNullException (() => map.AddOrUpdate (null, k => 0, (k, v) => v));
+ AssertThrowsArgumentNullException (() => map.AddOrUpdate ("", null, (k, v) => v));
+ AssertThrowsArgumentNullException (() => map.AddOrUpdate ("", k => 0, null));
+ AssertThrowsArgumentNullException (() => map.AddOrUpdate (null, 0, (k, v) => v));
+ AssertThrowsArgumentNullException (() => map.AddOrUpdate ("", 0, null));
+ AssertThrowsArgumentNullException (() => map.ContainsKey (null));
+ AssertThrowsArgumentNullException (() => map.GetOrAdd (null, 0));
+ int value;
+ AssertThrowsArgumentNullException (() => map.TryGetValue (null, out value));
+ AssertThrowsArgumentNullException (() => map.TryRemove (null, out value));
+ AssertThrowsArgumentNullException (() => map.TryUpdate (null, 0, 0));
+ }
+
+ void AssertThrowsArgumentNullException (Action action)
+ {
+ try {
+ action ();
+ Assert.Fail ("Expected ArgumentNullException.");
+ } catch (ArgumentNullException ex) {
+ }
+ }
}
}
#endif