Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

Commit

Permalink
Fixes #1296 - ItemCollection should be ICollection
Browse files Browse the repository at this point in the history
A little bit more kindness for consumers.

I didn't add any tests because these are all just trivial forwards.
  • Loading branch information
rynowak committed Jun 6, 2017
1 parent d5c1c63 commit 6664efb
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 1 deletion.
63 changes: 63 additions & 0 deletions src/Microsoft.AspNetCore.Razor.Language/DefaultItemCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,68 @@ public override object this[object key]
_items[key] = value;
}
}

public override int Count => _items.Count;

public override bool IsReadOnly => false;

public override void Add(KeyValuePair<object, object> item)
{
if (item.Key == null)
{
throw new ArgumentException(Resources.KeyMustNotBeNull, nameof(item));
}

((ICollection<KeyValuePair<object, object>>)_items).Add(item);
}

public override void Clear()
{
_items.Clear();
}

public override bool Contains(KeyValuePair<object, object> item)
{
if (item.Key == null)
{
throw new ArgumentException(Resources.KeyMustNotBeNull, nameof(item));
}

return ((ICollection<KeyValuePair<object, object>>)_items).Contains(item);
}

public override void CopyTo(KeyValuePair<object, object>[] array, int arrayIndex)
{
if (array == null)
{
throw new ArgumentNullException(nameof(array));
}

if (arrayIndex < 0 || arrayIndex > array.Length)
{
throw new ArgumentOutOfRangeException(nameof(arrayIndex));
}
else if (array.Length - arrayIndex < Count)
{
throw new ArgumentOutOfRangeException(nameof(arrayIndex));
}

((ICollection<KeyValuePair<object, object>>)_items).CopyTo(array, arrayIndex);
}

public override IEnumerator<KeyValuePair<object, object>> GetEnumerator()
{
return _items.GetEnumerator();
}

public override bool Remove(KeyValuePair<object, object> item)
{
if (item.Key == null)
{
throw new ArgumentException(Resources.KeyMustNotBeNull, nameof(item));
}

return ((ICollection<KeyValuePair<object, object>>)_items).Remove(item);
}
}
}
26 changes: 25 additions & 1 deletion src/Microsoft.AspNetCore.Razor.Language/ItemCollection.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections;
using System.Collections.Generic;

namespace Microsoft.AspNetCore.Razor.Language
{
public abstract class ItemCollection
public abstract class ItemCollection : ICollection<KeyValuePair<object, object>>
{
public abstract object this[object key] { get; set; }

public abstract int Count { get; }

public abstract bool IsReadOnly { get; }

public abstract void Add(KeyValuePair<object, object> item);

public abstract void Clear();

public abstract bool Contains(KeyValuePair<object, object> item);

public abstract void CopyTo(KeyValuePair<object, object>[] array, int arrayIndex);

public abstract IEnumerator<KeyValuePair<object, object>> GetEnumerator();

public abstract bool Remove(KeyValuePair<object, object> item);

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions src/Microsoft.AspNetCore.Razor.Language/ReadonlyItemCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;

namespace Microsoft.AspNetCore.Razor.Language
{
Expand All @@ -14,5 +16,68 @@ public override object this[object key]
get => null;
set => throw new NotSupportedException();
}

public override int Count => 0;

public override bool IsReadOnly => true;

public override void Add(KeyValuePair<object, object> item)
{
if (item.Key == null)
{
throw new ArgumentException(Resources.KeyMustNotBeNull, nameof(item));
}

throw new NotSupportedException();
}

public override void Clear()
{
throw new NotSupportedException();
}

public override bool Contains(KeyValuePair<object, object> item)
{
if (item.Key == null)
{
throw new ArgumentException(Resources.KeyMustNotBeNull, nameof(item));
}

return false;
}

public override void CopyTo(KeyValuePair<object, object>[] array, int arrayIndex)
{
if (array == null)
{
throw new ArgumentNullException(nameof(array));
}

if (arrayIndex < 0 || arrayIndex > array.Length)
{
throw new ArgumentOutOfRangeException(nameof(arrayIndex));
}
else if (array.Length - arrayIndex < Count)
{
throw new ArgumentOutOfRangeException(nameof(arrayIndex));
}

// Do nothing.
}

public override IEnumerator<KeyValuePair<object, object>> GetEnumerator()
{
return Enumerable.Empty<KeyValuePair<object, object>>().GetEnumerator();
}

public override bool Remove(KeyValuePair<object, object> item)
{
if (item.Key == null)
{
throw new ArgumentException(Resources.KeyMustNotBeNull, nameof(item));
}

throw new NotSupportedException();
}
}
}
3 changes: 3 additions & 0 deletions src/Microsoft.AspNetCore.Razor.Language/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,7 @@
<data name="DirectiveMustAppearAtStartOfLine" xml:space="preserve">
<value>The '{0}` directive must appear at the start of the line.</value>
</data>
<data name="KeyMustNotBeNull" xml:space="preserve">
<value>The key must not be null.</value>
</data>
</root>

0 comments on commit 6664efb

Please sign in to comment.