Skip to content

Commit

Permalink
Collection<T>: Validate parameters in the public methods (dotnet/core…
Browse files Browse the repository at this point in the history
…clr#23166)

I was looking over the new Range methods on `Collection<T>` and it occurred to me that for all existing methods, parameter validation is done in the *public* methods before calling the *protected virtual* methods. This changes the new Range methods to do the same.

Commit migrated from dotnet/coreclr@5231179
  • Loading branch information
justinvp authored and stephentoub committed Mar 15, 2019
1 parent f302497 commit 6ed2db0
Showing 1 changed file with 72 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,25 @@ public void Insert(int index, T item)
InsertItem(index, item);
}

public void InsertRange(int index, IEnumerable<T> collection) => InsertItemsRange(index, collection);
public void InsertRange(int index, IEnumerable<T> collection)
{
if (items.IsReadOnly)
{
ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
}

if (collection == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.list);
}

if ((uint)index > (uint)items.Count)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_ListInsert);
}

InsertItemsRange(index, collection);
}

public bool Remove(T item)
{
Expand All @@ -131,9 +149,60 @@ public bool Remove(T item)
return true;
}

public void RemoveRange(int index, int count) => RemoveItemsRange(index, count);
public void RemoveRange(int index, int count)
{
if (items.IsReadOnly)
{
ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
}

if ((uint)index > (uint)items.Count)
{
ThrowHelper.ThrowArgumentOutOfRange_IndexException();
}

if (count < 0)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
}

if (index > items.Count - count)
{
ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
}

RemoveItemsRange(index, count);
}

public void ReplaceRange(int index, int count, IEnumerable<T> collection)
{
if (items.IsReadOnly)
{
ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
}

public void ReplaceRange(int index, int count, IEnumerable<T> collection) => ReplaceItemsRange(index, count, collection);
if ((uint)index > (uint)items.Count)
{
ThrowHelper.ThrowArgumentOutOfRange_IndexException();
}

if (count < 0)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
}

if (index > items.Count - count)
{
ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
}

if (collection == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.list);
}

ReplaceItemsRange(index, count, collection);
}

public void RemoveAt(int index)
{
Expand Down Expand Up @@ -172,21 +241,6 @@ protected virtual void SetItem(int index, T item)

protected virtual void InsertItemsRange(int index, IEnumerable<T> collection)
{
if (items.IsReadOnly)
{
ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
}

if (collection == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.list);
}

if ((uint)index > (uint)items.Count)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_ListInsert);
}

if (GetType() == typeof(Collection<T>) && items is List<T> list)
{
list.InsertRange(index, collection);
Expand All @@ -202,26 +256,6 @@ protected virtual void InsertItemsRange(int index, IEnumerable<T> collection)

protected virtual void RemoveItemsRange(int index, int count)
{
if (items.IsReadOnly)
{
ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
}

if ((uint)index > (uint)items.Count)
{
ThrowHelper.ThrowArgumentOutOfRange_IndexException();
}

if (count < 0)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
}

if (index > items.Count - count)
{
ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
}

if (GetType() == typeof(Collection<T>) && items is List<T> list)
{
list.RemoveRange(index, count);
Expand Down

0 comments on commit 6ed2db0

Please sign in to comment.