| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | // Copied from https://github.com/aspnet/AspNetCore/tree/master/src/Http/Headers/src |
| | 5 | |
|
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Collections.ObjectModel; |
| | 9 | |
|
| | 10 | | #pragma warning disable IDE0008 // Use explicit type |
| | 11 | | #pragma warning disable IDE1006 // Missing s_ prefix |
| | 12 | |
|
| | 13 | | namespace Azure.Core.Http.Multipart |
| | 14 | | { |
| | 15 | | // List<T> allows 'null' values to be added. This is not what we want so we use a custom Collection<T> derived |
| | 16 | | // type to throw if 'null' gets added. Collection<T> internally uses List<T> which comes at some cost. In addition |
| | 17 | | // Collection<T>.Add() calls List<T>.InsertItem() which is an O(n) operation (compared to O(1) for List<T>.Add()). |
| | 18 | | // This type is only used for very small collections (1-2 items) to keep the impact of using Collection<T> small. |
| | 19 | | internal sealed class ObjectCollection<T> : Collection<T> |
| | 20 | | { |
| 0 | 21 | | internal static readonly Action<T> DefaultValidator = CheckNotNull; |
| 0 | 22 | | internal static readonly ObjectCollection<T> EmptyReadOnlyCollection |
| 0 | 23 | | = new ObjectCollection<T>(DefaultValidator, isReadOnly: true); |
| | 24 | |
|
| | 25 | | private readonly Action<T> _validator; |
| | 26 | |
|
| | 27 | | // We need to create a 'read-only' inner list for Collection<T> to do the right |
| | 28 | | // thing. |
| | 29 | | private static IList<T> CreateInnerList(bool isReadOnly, IEnumerable<T> other = null) |
| | 30 | | { |
| 0 | 31 | | var list = other == null ? new List<T>() : new List<T>(other); |
| 0 | 32 | | if (isReadOnly) |
| | 33 | | { |
| 0 | 34 | | return new ReadOnlyCollection<T>(list); |
| | 35 | | } |
| | 36 | | else |
| | 37 | | { |
| 0 | 38 | | return list; |
| | 39 | | } |
| | 40 | | } |
| | 41 | |
|
| | 42 | | public ObjectCollection() |
| 0 | 43 | | : this(DefaultValidator) |
| | 44 | | { |
| 0 | 45 | | } |
| | 46 | |
|
| | 47 | | public ObjectCollection(Action<T> validator, bool isReadOnly = false) |
| 0 | 48 | | : base(CreateInnerList(isReadOnly)) |
| | 49 | | { |
| 0 | 50 | | _validator = validator; |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | public ObjectCollection(IEnumerable<T> other, bool isReadOnly = false) |
| 0 | 54 | | : base(CreateInnerList(isReadOnly, other)) |
| | 55 | | { |
| 0 | 56 | | _validator = DefaultValidator; |
| 0 | 57 | | foreach (T item in Items) |
| | 58 | | { |
| 0 | 59 | | _validator(item); |
| | 60 | | } |
| 0 | 61 | | } |
| | 62 | |
|
| 0 | 63 | | public bool IsReadOnly => ((ICollection<T>)this).IsReadOnly; |
| | 64 | |
|
| | 65 | | protected override void InsertItem(int index, T item) |
| | 66 | | { |
| 0 | 67 | | _validator(item); |
| 0 | 68 | | base.InsertItem(index, item); |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | protected override void SetItem(int index, T item) |
| | 72 | | { |
| 0 | 73 | | _validator(item); |
| 0 | 74 | | base.SetItem(index, item); |
| 0 | 75 | | } |
| | 76 | |
|
| | 77 | | private static void CheckNotNull(T item) |
| | 78 | | { |
| | 79 | | // null values cannot be added to the collection. |
| 0 | 80 | | if (item == null) |
| | 81 | | { |
| 0 | 82 | | throw new ArgumentNullException(nameof(item)); |
| | 83 | | } |
| 0 | 84 | | } |
| | 85 | | } |
| | 86 | | } |