Improve exception throwing implementation in collections
This commit is contained in:
parent
4e0d05138a
commit
d9334ea8d9
@ -30,14 +30,13 @@
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
#endregion
|
||||
|
||||
using Google.Protobuf.Compatibility;
|
||||
using Google.Protobuf.Reflection;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Google.Protobuf.Compatibility;
|
||||
|
||||
namespace Google.Protobuf.Collections
|
||||
{
|
||||
@ -113,7 +112,7 @@ namespace Google.Protobuf.Collections
|
||||
// Validation of arguments happens in ContainsKey and the indexer
|
||||
if (ContainsKey(key))
|
||||
{
|
||||
throw new ArgumentException("Key already exists in map", "key");
|
||||
throw new ArgumentException("Key already exists in map", nameof(key));
|
||||
}
|
||||
this[key] = value;
|
||||
}
|
||||
@ -125,7 +124,7 @@ namespace Google.Protobuf.Collections
|
||||
/// <returns><c>true</c> if the map contains the given key; <c>false</c> otherwise.</returns>
|
||||
public bool ContainsKey(TKey key)
|
||||
{
|
||||
ProtoPreconditions.CheckNotNullUnconstrained(key, "key");
|
||||
ProtoPreconditions.CheckNotNullUnconstrained(key, nameof(key));
|
||||
return map.ContainsKey(key);
|
||||
}
|
||||
|
||||
@ -142,7 +141,7 @@ namespace Google.Protobuf.Collections
|
||||
/// <returns><c>true</c> if the map contained the given key before the entry was removed; <c>false</c> otherwise.</returns>
|
||||
public bool Remove(TKey key)
|
||||
{
|
||||
ProtoPreconditions.CheckNotNullUnconstrained(key, "key");
|
||||
ProtoPreconditions.CheckNotNullUnconstrained(key, nameof(key));
|
||||
LinkedListNode<KeyValuePair<TKey, TValue>> node;
|
||||
if (map.TryGetValue(key, out node))
|
||||
{
|
||||
@ -190,7 +189,7 @@ namespace Google.Protobuf.Collections
|
||||
{
|
||||
get
|
||||
{
|
||||
ProtoPreconditions.CheckNotNullUnconstrained(key, "key");
|
||||
ProtoPreconditions.CheckNotNullUnconstrained(key, nameof(key));
|
||||
TValue value;
|
||||
if (TryGetValue(key, out value))
|
||||
{
|
||||
@ -200,11 +199,11 @@ namespace Google.Protobuf.Collections
|
||||
}
|
||||
set
|
||||
{
|
||||
ProtoPreconditions.CheckNotNullUnconstrained(key, "key");
|
||||
ProtoPreconditions.CheckNotNullUnconstrained(key, nameof(key));
|
||||
// value == null check here is redundant, but avoids boxing.
|
||||
if (value == null)
|
||||
{
|
||||
ProtoPreconditions.CheckNotNullUnconstrained(value, "value");
|
||||
ProtoPreconditions.CheckNotNullUnconstrained(value, nameof(value));
|
||||
}
|
||||
LinkedListNode<KeyValuePair<TKey, TValue>> node;
|
||||
var pair = new KeyValuePair<TKey, TValue>(key, value);
|
||||
@ -236,7 +235,7 @@ namespace Google.Protobuf.Collections
|
||||
/// <param name="entries">The entries to add to the map.</param>
|
||||
public void Add(IDictionary<TKey, TValue> entries)
|
||||
{
|
||||
ProtoPreconditions.CheckNotNull(entries, "entries");
|
||||
ProtoPreconditions.CheckNotNull(entries, nameof(entries));
|
||||
foreach (var pair in entries)
|
||||
{
|
||||
Add(pair.Key, pair.Value);
|
||||
@ -315,7 +314,7 @@ namespace Google.Protobuf.Collections
|
||||
{
|
||||
if (item.Key == null)
|
||||
{
|
||||
throw new ArgumentException("Key is null", "item");
|
||||
throw new ArgumentException("Key is null", nameof(item));
|
||||
}
|
||||
LinkedListNode<KeyValuePair<TKey, TValue>> node;
|
||||
if (map.TryGetValue(item.Key, out node) &&
|
||||
@ -503,7 +502,7 @@ namespace Google.Protobuf.Collections
|
||||
|
||||
void IDictionary.Remove(object key)
|
||||
{
|
||||
ProtoPreconditions.CheckNotNull(key, "key");
|
||||
ProtoPreconditions.CheckNotNull(key, nameof(key));
|
||||
if (!(key is TKey))
|
||||
{
|
||||
return;
|
||||
@ -532,7 +531,7 @@ namespace Google.Protobuf.Collections
|
||||
{
|
||||
get
|
||||
{
|
||||
ProtoPreconditions.CheckNotNull(key, "key");
|
||||
ProtoPreconditions.CheckNotNull(key, nameof(key));
|
||||
if (!(key is TKey))
|
||||
{
|
||||
return null;
|
||||
@ -714,11 +713,11 @@ namespace Google.Protobuf.Collections
|
||||
{
|
||||
if (arrayIndex < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("arrayIndex");
|
||||
throw new ArgumentOutOfRangeException(nameof(arrayIndex));
|
||||
}
|
||||
if (arrayIndex + Count >= array.Length)
|
||||
{
|
||||
throw new ArgumentException("Not enough space in the array", "array");
|
||||
throw new ArgumentException("Not enough space in the array", nameof(array));
|
||||
}
|
||||
foreach (var item in this)
|
||||
{
|
||||
@ -745,11 +744,11 @@ namespace Google.Protobuf.Collections
|
||||
{
|
||||
if (index < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("index");
|
||||
throw new ArgumentOutOfRangeException(nameof(index));
|
||||
}
|
||||
if (index + Count >= array.Length)
|
||||
{
|
||||
throw new ArgumentException("Not enough space in the array", "array");
|
||||
throw new ArgumentException("Not enough space in the array", nameof(array));
|
||||
}
|
||||
foreach (var item in this)
|
||||
{
|
||||
|
@ -34,7 +34,6 @@ using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace Google.Protobuf.Collections
|
||||
{
|
||||
@ -227,10 +226,7 @@ namespace Google.Protobuf.Collections
|
||||
/// <param name="item">The item to add.</param>
|
||||
public void Add(T item)
|
||||
{
|
||||
if (item == null)
|
||||
{
|
||||
throw new ArgumentNullException("item");
|
||||
}
|
||||
ProtoPreconditions.CheckNotNullUnconstrained(item, nameof(item));
|
||||
EnsureSize(count + 1);
|
||||
array[count++] = item;
|
||||
}
|
||||
@ -300,10 +296,7 @@ namespace Google.Protobuf.Collections
|
||||
/// <param name="values">The values to add to this collection.</param>
|
||||
public void Add(RepeatedField<T> values)
|
||||
{
|
||||
if (values == null)
|
||||
{
|
||||
throw new ArgumentNullException("values");
|
||||
}
|
||||
ProtoPreconditions.CheckNotNull(values, nameof(values));
|
||||
EnsureSize(count + values.count);
|
||||
// We know that all the values will be valid, because it's a RepeatedField.
|
||||
Array.Copy(values.array, 0, array, count, values.count);
|
||||
@ -316,10 +309,7 @@ namespace Google.Protobuf.Collections
|
||||
/// <param name="values">The values to add to this collection.</param>
|
||||
public void Add(IEnumerable<T> values)
|
||||
{
|
||||
if (values == null)
|
||||
{
|
||||
throw new ArgumentNullException("values");
|
||||
}
|
||||
ProtoPreconditions.CheckNotNull(values, nameof(values));
|
||||
// TODO: Check for ICollection and get the Count, to optimize?
|
||||
foreach (T item in values)
|
||||
{
|
||||
@ -418,10 +408,7 @@ namespace Google.Protobuf.Collections
|
||||
/// <returns>The zero-based index of the item, or -1 if it is not found.</returns>
|
||||
public int IndexOf(T item)
|
||||
{
|
||||
if (item == null)
|
||||
{
|
||||
throw new ArgumentNullException("item");
|
||||
}
|
||||
ProtoPreconditions.CheckNotNullUnconstrained(item, nameof(item));
|
||||
EqualityComparer<T> comparer = EqualityComparer<T>.Default;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
@ -440,13 +427,10 @@ namespace Google.Protobuf.Collections
|
||||
/// <param name="item">The item to insert.</param>
|
||||
public void Insert(int index, T item)
|
||||
{
|
||||
if (item == null)
|
||||
{
|
||||
throw new ArgumentNullException("item");
|
||||
}
|
||||
ProtoPreconditions.CheckNotNullUnconstrained(item, nameof(item));
|
||||
if (index < 0 || index > count)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("index");
|
||||
throw new ArgumentOutOfRangeException(nameof(index));
|
||||
}
|
||||
EnsureSize(count + 1);
|
||||
Array.Copy(array, index, array, index + 1, count - index);
|
||||
@ -462,7 +446,7 @@ namespace Google.Protobuf.Collections
|
||||
{
|
||||
if (index < 0 || index >= count)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("index");
|
||||
throw new ArgumentOutOfRangeException(nameof(index));
|
||||
}
|
||||
Array.Copy(array, index + 1, array, index, count - index - 1);
|
||||
count--;
|
||||
@ -494,7 +478,7 @@ namespace Google.Protobuf.Collections
|
||||
{
|
||||
if (index < 0 || index >= count)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("index");
|
||||
throw new ArgumentOutOfRangeException(nameof(index));
|
||||
}
|
||||
return array[index];
|
||||
}
|
||||
@ -502,12 +486,9 @@ namespace Google.Protobuf.Collections
|
||||
{
|
||||
if (index < 0 || index >= count)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("index");
|
||||
}
|
||||
if (value == null)
|
||||
{
|
||||
throw new ArgumentNullException("value");
|
||||
throw new ArgumentOutOfRangeException(nameof(index));
|
||||
}
|
||||
ProtoPreconditions.CheckNotNullUnconstrained(value, nameof(value));
|
||||
array[index] = value;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user